Phylo.purs 3.18 KB
Newer Older
arturo's avatar
arturo committed
1
module Gargantext.Components.Nodes.Corpus.Phylo
arturo's avatar
arturo committed
2
  ( node
arturo's avatar
arturo committed
3
  ) where
4 5 6

import Gargantext.Prelude

7
import DOM.Simple (document, querySelector)
arturo's avatar
arturo committed
8
import Data.Maybe (Maybe(..), isJust, maybe)
arturo's avatar
arturo committed
9 10
import Data.Tuple.Nested ((/\))
import Gargantext.Components.Bootstrap as B
11
import Gargantext.Components.PhyloExplorer.API (get)
arturo's avatar
arturo committed
12
import Gargantext.Components.PhyloExplorer.Layout (layout)
arturo's avatar
arturo committed
13
import Gargantext.Components.PhyloExplorer.Store as PhyloStore
arturo's avatar
arturo committed
14
import Gargantext.Components.PhyloExplorer.Types (CacheParams, PhyloSet(..), defaultCacheParams)
15
import Gargantext.Config.REST (logRESTError)
16
import Gargantext.Hooks.FirstEffect (useFirstEffect')
arturo's avatar
arturo committed
17
import Gargantext.Hooks.Loader (useLoaderEffect)
arturo's avatar
arturo committed
18
import Gargantext.Hooks.Session (useSession)
19
import Gargantext.Types (NodeID)
20
import Gargantext.Utils (getter)
21
import Gargantext.Utils.Reactix as R2
Alexandre Delanoë's avatar
Alexandre Delanoë committed
22
import Reactix as R
arturo's avatar
arturo committed
23
import Reactix.DOM.HTML as H
arturo's avatar
arturo committed
24
import Record as Record
25

26 27 28
type MainProps =
  ( nodeId      :: NodeID
  )
arturo's avatar
arturo committed
29

30 31 32
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Corpus.Phylo"

arturo's avatar
arturo committed
33 34
node :: R2.Leaf MainProps
node = R2.leaf nodeCpt
35

arturo's avatar
arturo committed
36 37
nodeCpt :: R.Component MainProps
nodeCpt = here.component "node" cpt where
arturo's avatar
arturo committed
38
  cpt { nodeId } _ = do
arturo's avatar
arturo committed
39 40
    -- | States
    -- |
arturo's avatar
arturo committed
41
    session <- useSession
arturo's avatar
arturo committed
42

arturo's avatar
arturo committed
43
    state' /\ state <- R2.useBox' (Nothing :: Maybe PhyloSet)
44
    cache' /\ cache <- R2.useBox' (defaultCacheParams :: CacheParams)
arturo's avatar
arturo committed
45 46 47

    -- | Computed
    -- |
arturo's avatar
arturo committed
48
    let errorHandler = logRESTError here "[phylo]"
arturo's avatar
arturo committed
49 50 51 52

    -- | Hooks
    -- |

53 54 55 56
    -- load Local Storage cache (if exists)
    useFirstEffect' $
      R2.loadLocalStorageState R2.phyloParamsKey cache

arturo's avatar
arturo committed
57
    useLoaderEffect
58 59 60
      { errorHandler
      , loader: get session
      , path: nodeId
arturo's avatar
arturo committed
61
      , state
62 63
      }

arturo's avatar
arturo committed
64 65
    -- @XXX: reset "main-page__main-route" wrapper margin
    --       see Gargantext.Components.Router) (@TODO?)
arturo's avatar
arturo committed
66 67 68 69 70 71 72
    R.useLayoutEffect1 [] do
      let mEl = querySelector document ".main-page__main-route"
      -- Mount
      mEl >>= maybe R.nothing (flip R2.addClass ["p-0"])
      -- Unmount
      pure $
        mEl >>= maybe R.nothing (flip R2.removeClass ["p-0"])
arturo's avatar
arturo committed
73 74 75 76


    -- | Render
    -- |
77 78
    pure $

arturo's avatar
arturo committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      B.cloak
      { isDisplayed: isJust state'
      , idlingPhaseDuration: Just 150
      , cloakSlot:
          -- mimicking `PhyloExplorer.layout` preloading template
          H.div
          { className: "phylo" }
          [
            H.div
            { className: "phylo__spinner-wrapper" }
            [
              B.spinner
              { className: "phylo__spinner" }
            ]
          ]
      , defaultSlot:
arturo's avatar
arturo committed
95
          R2.fromMaybe state' \(PhyloSet { corpusId, listId, phyloData }) ->
arturo's avatar
arturo committed
96 97 98 99 100

            let
              state_ :: Record PhyloStore.State
              state_ =
                -- Data
arturo's avatar
arturo committed
101 102 103
                { phyloData
                , corpusId
                , listId
arturo's avatar
arturo committed
104
                , phyloId: nodeId
105 106 107
                -- (cache params)
                , expandSelection: getter _.expandSelection cache'
                , expandNeighborhood: getter _.expandNeighborhood cache'
arturo's avatar
arturo committed
108 109 110 111 112 113 114 115 116 117
                -- (default options)
                } `Record.merge` PhyloStore.options

            in
              PhyloStore.provide
              state_
              [
                layout
                {}
              ]
arturo's avatar
arturo committed
118
      }