Legend.purs 4.78 KB
Newer Older
arturo's avatar
arturo committed
1
module Gargantext.Components.GraphExplorer.Sidebar.Legend
arturo's avatar
arturo committed
2
  ( Props, legend
James Laver's avatar
James Laver committed
3 4
  ) where

5 6
import Prelude hiding (map)

arturo's avatar
arturo committed
7 8
import Data.Array as A
import Data.Maybe (isJust, maybe)
9
import Data.Sequence (Seq)
arturo's avatar
arturo committed
10 11 12
import Data.Set as Set
import Data.Traversable (foldMap, intercalate)
import Gargantext.Components.Bootstrap as B
13
import Gargantext.Components.GraphExplorer.GraphTypes as GEGT
arturo's avatar
arturo committed
14 15 16 17
import Gargantext.Components.GraphExplorer.Types as GET
import Gargantext.Hooks.Sigmax.Types as ST
import Gargantext.Utils (getter, nbsp, (?))
import Gargantext.Utils.Reactix as R2
James Laver's avatar
James Laver committed
18
import Reactix as R
arturo's avatar
arturo committed
19
import Reactix.DOM.HTML as H
arturo's avatar
arturo committed
20
import Toestand as T
21

arturo's avatar
arturo committed
22
here :: R2.Here
arturo's avatar
arturo committed
23
here = R2.here "Gargantext.Components.GraphExplorer.Sidebar.Legend"
James Laver's avatar
James Laver committed
24

arturo's avatar
arturo committed
25 26
type Props =
  ( legendSeq             :: Seq GET.Legend
27 28
  , extractedNodeList     :: Array GEGT.Node
  , nodeCountList         :: Array GEGT.ClusterCount
arturo's avatar
arturo committed
29 30
  , selectedNodeIds       :: T.Box ST.NodeIds
  )
James Laver's avatar
James Laver committed
31

arturo's avatar
arturo committed
32 33
legend :: R2.Leaf Props
legend = R2.leaf legendCpt
James Laver's avatar
James Laver committed
34 35

legendCpt :: R.Component Props
arturo's avatar
arturo committed
36
legendCpt = here.component "legend" cpt where
arturo's avatar
arturo committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  cpt { legendSeq
      , extractedNodeList
      , nodeCountList
      , selectedNodeIds
      } _ = do
    -- | Hooks
    -- |
    R.useEffectOnce' $ here.info2 "legend" extractedNodeList
    -- | Render
    -- |
    pure $

      H.ul
      { className: "graph-legend" }
      [
        flip foldMap legendSeq \(GET.Legend { id_, label }) ->

          H.li
          { className: "graph-legend__item" }
          [
            H.div
            { className: "graph-legend__code"
            , style: { backgroundColor: GET.intColor id_ }
            }
            []
          ,
            B.wad
            [ "flex-grow-1" ]
            [
              B.div'
              { className: "graph-legend__title" }
              label
            ,
              selectedNodes
              { selectedNodeIds
              , extractedNodeList
              , clusterId: id_
              , nodeCount: getClusterNodeCount nodeCountList id_
              }
            ]
          ]
      ]

80
filterByCluster :: Int -> Array GEGT.Node -> Array GEGT.Node
arturo's avatar
arturo committed
81 82 83 84 85 86 87
filterByCluster id
  =   A.filter
      (   getter _.attributes
      >>> getter _.clustDefault
      >>> eq id
      )

88
getClusterNodeCount :: Array GEGT.ClusterCount -> Int -> Int
arturo's avatar
arturo committed
89 90 91 92 93 94 95 96 97 98 99 100 101
getClusterNodeCount nodeCountList id
  =   nodeCountList
  #   A.find
      (   getter _.id
      >>> eq id
      )
  >>> maybe 0
      (   getter _.count
      )

---------------------------------------------------------

type SelectedNodesProps =
102
  ( extractedNodeList     :: Array GEGT.Node
arturo's avatar
arturo committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  , selectedNodeIds       :: T.Box ST.NodeIds
  , clusterId             :: Int
  , nodeCount             :: Int
  )

selectedNodes :: R2.Leaf SelectedNodesProps
selectedNodes = R2.leaf selectedNodesCpt

selectedNodesCpt :: R.Component SelectedNodesProps
selectedNodesCpt = here.component "selectedNodes" cpt where
  cpt { extractedNodeList
      , selectedNodeIds
      , clusterId
      , nodeCount
      } _ = do
    -- | States
    -- |
    selectedNodeIds' <- R2.useLive' selectedNodeIds
    -- | Computed
    -- |
    let
      isSelected id
        =   selectedNodeIds'
        # A.fromFoldable
        # A.find
            ( eq id
            )
        # isJust

      countValue
        =   extractedNodeList
        #   A.length
        #   (nodeCount - _)

    -- | Behaviors
    -- |
    let
      onBadgeClick id _ = T.write_ (Set.singleton id) selectedNodeIds

    -- | Render
    -- |
    pure $

      H.ul
      { className: "graph-legend-nodes" }
      [
        flip foldMap (filterByCluster clusterId extractedNodeList)
150
        \(GEGT.Node { label: nodeLabel, id_: nodeId }) ->
arturo's avatar
arturo committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

          H.li
          { className: "graph-legend-nodes__item" }
          [
            H.a
            { className: intercalate " "
                [ "graph-legend-nodes__badge"
                , (isSelected nodeId) ?
                    "graph-legend-nodes__badge--selected" $
                    ""
                , "badge badge-light"
                ]
            , on: { click: onBadgeClick nodeId }
            }
            [ H.text nodeLabel ]
          ]

      ,
        R2.when (eq countValue 0) $

          H.li
          { className: intercalate " "
              [ "graph-legend-nodes__item"
              , "graph-legend-nodes__item--count"
              ]
          }
          [
            H.text "0 node"
          ]
      ,
        R2.when (not $ eq countValue 0) $

          H.li
          { className: intercalate " "
              [ "graph-legend-nodes__item"
              , "graph-legend-nodes__item--count"
              ]
arturo's avatar
arturo committed
188
          }
arturo's avatar
arturo committed
189 190 191 192 193 194 195 196 197 198 199 200
          [
            H.text "+"
          ,
            H.text $ nbsp 1
          ,
            H.text $ show countValue
          ,
            H.text $ nbsp 1
          ,
            H.text $ eq countValue 1 ? "node" $ "nodes"
          ]
      ]