Forest.purs 2.89 KB
Newer Older
1 2
module Gargantext.Components.Forest where

3 4
import Gargantext.Prelude

5
import Data.Maybe (Maybe(..))
6 7
import Data.Set as Set
import Data.Tuple (fst)
8
import Data.Tuple.Nested ((/\))
9 10 11
import Reactix as R
import Reactix.DOM.HTML as H

12
import Gargantext.AsyncTasks as GAT
13
import Gargantext.Components.Forest.Tree (treeView)
14 15
import Gargantext.Ends (Frontends)
import Gargantext.Routes (AppRoute)
16
import Gargantext.Sessions (Session(..), Sessions, OpenNodes, unSessions)
17
import Gargantext.Types (Reload, Handed(..))
18 19 20
import Gargantext.Utils.Reactix as R2

type Props =
21
  ( frontends :: Frontends
22 23
  , handed    :: Handed
  , reload    :: R.State Int
24
  , route     :: AppRoute
25
  , sessions  :: Sessions
26 27
  , showLogin :: R2.Setter Boolean
  )
28 29 30 31 32

forest :: Record Props -> R.Element
forest props = R.createElement forestCpt props []

forestCpt :: R.Component Props
33
forestCpt = R.hooksComponent "G.C.Forest.forest" cpt where
34
  cpt { frontends, handed, reload: extReload, route, sessions, showLogin } _ = do
35
    -- NOTE: this is a hack to reload the tree view on demand
36 37
    reload     <- R.useState' (0 :: Reload)
    openNodes  <- R2.useLocalStorageState R2.openNodesKey (Set.empty :: OpenNodes)
38
    asyncTasks <- R2.useLocalStorageState GAT.localStorageKey GAT.empty
39
    R2.useCache
40 41 42 43 44 45 46
      (  frontends
      /\ route
      /\ sessions
      /\ fst openNodes
      /\ fst extReload
      /\ fst reload
      /\ fst asyncTasks
47
      /\ handed
48
      )
49
      (cpt' openNodes asyncTasks reload showLogin)
50
  cpt' openNodes asyncTasks reload showLogin (frontends /\ route /\ sessions /\ _ /\ _ /\ _ /\ _ /\ handed) = do
51
    pure $ R2.row $ [plus handed showLogin] <> trees
52
    where
53 54
      trees = tree <$> unSessions sessions
      tree s@(Session {treeId}) =
55 56 57
        treeView { root: treeId
                 , asyncTasks
                 , frontends
58
                 , handed
59 60 61
                 , mCurrentRoute: Just route
                 , openNodes
                 , reload
62
                 , session: s
63
                 }
64

65 66
plus :: Handed -> R2.Setter Boolean -> R.Element
plus handed showLogin = H.div {className: if handed == RightHanded
67
                                             then "flex-start"  -- TODO we should use lefthanded SASS class here
68 69
                                             else "flex-end"
                              } [
70 71
  H.button { title: "Add or remove connections to the server(s)."
           , on: {click}
72
           , className: "btn btn-default"
73 74 75 76 77
           }
          [ H.div { "type": ""
                  , className: "fa fa-universal-access fa-lg"
                  } [H.text " Log "]
          , H.div {} [H.text "    "]
78 79
  --, H.div { "type": "", className: "fa fa-plus-circle fa-lg"} []
  --, H.div { "type": "", className: "fa fa-minus-circle fa-lg"} []
80
          ]
81
          ]
82 83
  -- TODO same as the one in the Login Modal (same CSS)
  -- [ H.i { className: "material-icons md-36"} [] ]
84 85
  where
    click _ = do
86
      showLogin $ const true