App.purs 2.74 KB
Newer Older
James Laver's avatar
James Laver committed
1
module Gargantext.Components.App (app) where
2

3 4
import Gargantext.Prelude

5
import Data.Tuple.Nested ((/\))
6
import Gargantext.AsyncTasks as GAT
arturo's avatar
arturo committed
7
import Gargantext.Components.App.Store as AppStore
James Laver's avatar
James Laver committed
8 9
import Gargantext.Components.Router (router)
import Gargantext.Hooks (useHashRouter)
10
import Gargantext.Hooks.FirstEffect (useFirstEffect')
James Laver's avatar
James Laver committed
11
import Gargantext.Router as Router
12
import Gargantext.Sessions as Sessions
13 14
import Gargantext.Types (CacheParams, defaultCacheParams)
import Gargantext.Utils (getter)
15
import Gargantext.Utils.Reactix as R2
arturo's avatar
arturo committed
16
import Reactix as R
17
import Record as Record
arturo's avatar
arturo committed
18
import Toestand as T
James Laver's avatar
James Laver committed
19

20 21
here :: R2.Here
here = R2.here "Gargantext.Components.App"
22

23 24 25

app :: R2.Leaf ()
app = R2.leaf appCpt
26
appCpt :: R.Component ()
27 28 29 30 31 32 33 34 35 36 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
appCpt = here.component "container" cpt where
  cpt _ _ = do
    -- | States
    -- |

    cache' /\ cache <- R2.useBox' (defaultCacheParams :: CacheParams)

    -- | Hooks
    -- |

    -- load Local Storage cache (if exists)
    useFirstEffect' $
      R2.loadLocalStorageState R2.appParamsKey cache

    -- | Render
    -- |
    pure $

      hydrateStore
      { cacheParams: cache'
      }

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

type HydrateStoreProps =
  ( cacheParams :: CacheParams
  )

hydrateStore :: R2.Leaf HydrateStoreProps
hydrateStore = R2.leaf hydrateStoreCpt
hydrateStoreCpt :: R.Component HydrateStoreProps
hydrateStoreCpt = here.component "hydrateStore" cpt where
  cpt { cacheParams
      } _ = do
    -- | Computed
    -- |
    (state :: Record AppStore.State) <- pure $
      -- (cache options)
      { expandTableEdition: getter _.expandTableEdition cacheParams
66
      , showTree: getter _.showTree cacheParams
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      -- (default options)
      } `Record.merge` AppStore.options

    -- | Render
    -- |
    pure $

      AppStore.provide
      state
      [
        mainApp
        {}
      ]

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

mainApp :: R2.Leaf ()
mainApp = R2.leaf mainAppCpt

mainAppCpt :: R.Component ()
mainAppCpt = here.component "main" cpt where
88
  cpt _ _ = do
arturo's avatar
arturo committed
89
    boxes <- AppStore.use
90
    -- tasks   <- T.useBox Nothing             -- storage for asynchronous tasks reductor
91
    R.useEffectOnce' $ do
92
      void $ Sessions.load boxes.sessions
93 94 95 96
    -- tasks <- GAT.useTasks boxes.reloadRoot boxes.reloadForest
    R.useEffectOnce' $ do
      tasksStorage <- GAT.getAsyncTasks
      T.write_ tasksStorage boxes.tasks
97 98
    -- R.useEffectOnce' $ do
    --   T.write (Just tasksReductor) tasks
99 100 101
    R.useEffectOnce' $ do
      R2.loadLocalStorageState R2.openNodesKey boxes.forestOpen
      T.listen (R2.listenLocalStorageState R2.openNodesKey) boxes.forestOpen
102
    useHashRouter Router.router boxes.route -- Install router to window
103
    pure $ router { boxes }          -- Render router component