Router.purs 14.3 KB
Newer Older
James Laver's avatar
James Laver committed
1 2 3
module Gargantext.Components.Router (router) where

import Data.Array (fromFoldable)
James Laver's avatar
James Laver committed
4
import Data.Maybe (Maybe(..))
James Laver's avatar
James Laver committed
5
import Reactix as R
6
import Reactix.DOM.HTML as H
7 8
import Record as Record
import Record.Extra as RE
James Laver's avatar
James Laver committed
9
import Toestand as T
10
import Unsafe.Coerce (unsafeCoerce)
James Laver's avatar
James Laver committed
11 12

import Gargantext.Prelude
13 14

import Gargantext.AsyncTasks as GAT
15
import Gargantext.Components.App.Data (Boxes)
James Laver's avatar
James Laver committed
16
import Gargantext.Components.Footer (footer)
James Laver's avatar
James Laver committed
17
import Gargantext.Components.Forest (forestLayout)
18
import Gargantext.Components.GraphExplorer (explorerLayoutLoader)
James Laver's avatar
James Laver committed
19 20 21
import Gargantext.Components.Lang (LandingLang(LL_EN))
import Gargantext.Components.Login (login)
import Gargantext.Components.Nodes.Annuaire (annuaireLayout)
22
import Gargantext.Components.Nodes.Annuaire.User (userLayoutSessionContext)
James Laver's avatar
James Laver committed
23 24 25 26 27 28 29 30 31
import Gargantext.Components.Nodes.Annuaire.User.Contact (contactLayout)
import Gargantext.Components.Nodes.Corpus (corpusLayout)
import Gargantext.Components.Nodes.Corpus.Dashboard (dashboardLayout)
import Gargantext.Components.Nodes.Corpus.Document (documentMainLayout)
import Gargantext.Components.Nodes.File (fileLayout)
import Gargantext.Components.Nodes.Frame (frameLayout)
import Gargantext.Components.Nodes.Home (homeLayout)
import Gargantext.Components.Nodes.Lists as Lists
import Gargantext.Components.Nodes.Texts as Texts
32
import Gargantext.Components.SessionLoader (sessionWrapper)
James Laver's avatar
James Laver committed
33
import Gargantext.Components.SimpleLayout (simpleLayout)
James Laver's avatar
James Laver committed
34
import Gargantext.Config (defaultFrontends, defaultBackends)
James Laver's avatar
James Laver committed
35
import Gargantext.Ends (Backend)
James Laver's avatar
James Laver committed
36
import Gargantext.Routes (AppRoute)
37
import Gargantext.Routes as GR
38
import Gargantext.Sessions (Session)
James Laver's avatar
James Laver committed
39
import Gargantext.Types (CorpusId, ListId, NodeID, NodeType(..), SessionId)
James Laver's avatar
James Laver committed
40 41 42 43 44
import Gargantext.Utils.Reactix as R2

here :: R2.Here
here = R2.here "Gargantext.Components.Router"

45
type Props = ( boxes :: Boxes )
James Laver's avatar
James Laver committed
46

47
type SessionProps = ( session :: R.Context Session, sessionId :: SessionId | Props )
48

49
type SessionNodeProps = ( nodeId :: NodeID | SessionProps )
James Laver's avatar
James Laver committed
50
type Props' = ( route' :: AppRoute, backend :: Backend | Props )
51

James Laver's avatar
James Laver committed
52
router :: R2.Leaf Props
53
router props = R.createElement routerCpt props []
James Laver's avatar
James Laver committed
54 55

routerCpt :: R.Component Props
56
routerCpt = here.component "router" cpt where
57
  cpt props@{ boxes } _ = do
James Laver's avatar
James Laver committed
58
    let session = R.createContext (unsafeCoerce {})
59 60
    let sessionProps sId = Record.merge { session, sessionId: sId } props
    let sessionNodeProps sId nId = Record.merge { nodeId: nId } $ sessionProps sId
61

62
    route' <- T.useLive T.unequal boxes.route
63

64
    pure $ R.fragment
65
      [ loginModal { boxes } []
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
      , case route' of
        GR.Annuaire s n           -> annuaire (sessionNodeProps s n) []
        GR.ContactPage s a n      -> contact (Record.merge { annuaireId: a } $ sessionNodeProps s n) []
        GR.Corpus s n             -> corpus (sessionNodeProps s n) []
        GR.CorpusDocument s c l n -> corpusDocument (Record.merge { corpusId: c, listId: l } $ sessionNodeProps s n) []
        GR.Dashboard s n          -> dashboard (sessionNodeProps s n) []
        GR.Document s l n         -> document (Record.merge { listId: l } $ sessionNodeProps s n) []
        GR.Folder        s n      -> corpus (sessionNodeProps s n) []
        GR.FolderPrivate s n      -> corpus (sessionNodeProps s n) []
        GR.FolderPublic  s n      -> corpus (sessionNodeProps s n) []
        GR.FolderShared  s n      -> corpus (sessionNodeProps s n) []
        GR.Home                   -> home props []
        GR.Lists s n              -> lists (sessionNodeProps s n) []
        GR.Login                  -> login' boxes
        GR.PGraphExplorer s g     -> graphExplorer (sessionNodeProps s g) []
        GR.RouteFile s n          -> routeFile (sessionNodeProps s n) []
        GR.RouteFrameCalc  s n    -> routeFrame (Record.merge { nodeType: NodeFrameCalc } $ sessionNodeProps s n) []
        GR.RouteFrameCode  s n    -> routeFrame (Record.merge { nodeType: NodeFrameNotebook } $ sessionNodeProps s n) []
        GR.RouteFrameWrite s n    -> routeFrame (Record.merge { nodeType: NodeFrameWrite } $ sessionNodeProps s n) []
        GR.Team s n               -> team (sessionNodeProps s n) []
        GR.Texts s n              -> texts (sessionNodeProps s n) []
        GR.UserPage s n           -> user (sessionNodeProps s n) []
      ]
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

type LoginModalProps = (
  boxes :: Boxes
  )

loginModal :: R2.Component LoginModalProps
loginModal = R.createElement loginModalCpt

loginModalCpt :: R.Component LoginModalProps
loginModalCpt = here.component "loginModal" cpt
  where
    cpt { boxes: boxes@{ showLogin } } _ = do
        showLogin' <- T.useLive T.unequal showLogin

        pure $ if showLogin' then login' boxes else H.div {} []

106 107 108 109 110 111
forested :: R2.Component Props
forested = R.createElement forestedCpt

forestedCpt :: R.Component Props
forestedCpt = here.component "forested" cpt
  where
112 113 114 115 116 117 118
    cpt { boxes: { backend
                 , forestOpen
                 , handed
                 , reloadForest
                 , reloadRoot
                 , route
                 , sessions
119 120
                 , showLogin
                 , tasks } } children = do
121
      pure $ forestLayout { backend
122
                          , forestOpen
123 124 125 126 127 128 129 130 131
                          , frontends: defaultFrontends
                          , handed
                          , reloadForest
                          , reloadRoot
                          , route
                          , sessions
                          , showLogin
                          , tasks } children

132
authed :: Record SessionProps -> R.Element -> R.Element
133
authed props@{ boxes: { sessions }, session, sessionId } content =
134 135 136
  sessionWrapper { fallback: home homeProps []
                 , context: session
                 , sessionId
137
                 , sessions } [ content, footer {} [] ]
138 139 140 141 142
    where
      homeProps = RE.pick props :: Record Props

annuaire :: R2.Component SessionNodeProps
annuaire = R.createElement annuaireCpt
James Laver's avatar
James Laver committed
143

144
annuaireCpt :: R.Component SessionNodeProps
James Laver's avatar
James Laver committed
145
annuaireCpt = here.component "annuaire" cpt where
146
  cpt props@{ boxes, nodeId, session, sessionId } _ = do
James Laver's avatar
James Laver committed
147
    let sessionProps = RE.pick props :: Record SessionProps
148
    pure $ authed sessionProps $
149
      forested { boxes } [ annuaireLayout { frontends, nodeId, session } ]
James Laver's avatar
James Laver committed
150
      where frontends = defaultFrontends
151 152 153

corpus :: R2.Component SessionNodeProps
corpus = R.createElement corpusCpt
James Laver's avatar
James Laver committed
154

155
corpusCpt :: R.Component SessionNodeProps
James Laver's avatar
James Laver committed
156
corpusCpt = here.component "corpus" cpt where
157
  cpt props@{ boxes, nodeId, session } _ = do
James Laver's avatar
James Laver committed
158
    let sessionProps = RE.pick props :: Record SessionProps
159
    pure $ authed sessionProps $
160
      forested { boxes } [ corpusLayout { nodeId, session } ]
161

James Laver's avatar
James Laver committed
162 163
type CorpusDocumentProps =
  ( corpusId :: CorpusId
164
  , listId :: ListId
165 166 167 168 169
  | SessionNodeProps
  )

corpusDocument :: R2.Component CorpusDocumentProps
corpusDocument = R.createElement corpusDocumentCpt
James Laver's avatar
James Laver committed
170

171 172 173
corpusDocumentCpt :: R.Component CorpusDocumentProps
corpusDocumentCpt = here.component "corpusDocument" cpt
  where
174
    cpt props@{ boxes, corpusId: corpusId', listId, nodeId, session, sessionId } _ = do
175
      let sessionProps = RE.pick props :: Record SessionProps
176
      pure $ authed sessionProps $
177
        forested { boxes }
178
        [ documentMainLayout { mCorpusId: corpusId, listId: listId, nodeId, session } [] ]
179 180 181 182
        where corpusId = Just corpusId'

dashboard :: R2.Component SessionNodeProps
dashboard = R.createElement dashboardCpt
James Laver's avatar
James Laver committed
183

184 185 186
dashboardCpt :: R.Component SessionNodeProps
dashboardCpt = here.component "dashboard" cpt
  where
187
    cpt props@{ boxes, nodeId, session } _ = do
188
      let sessionProps = RE.pick props :: Record SessionProps
189
      pure $ authed sessionProps $
190
        forested { boxes } [ dashboardLayout { nodeId, session } [] ]
191

James Laver's avatar
James Laver committed
192
type DocumentProps = ( listId :: ListId | SessionNodeProps )
193 194 195

document :: R2.Component DocumentProps
document = R.createElement documentCpt
James Laver's avatar
James Laver committed
196

197
documentCpt :: R.Component DocumentProps
James Laver's avatar
James Laver committed
198
documentCpt = here.component "document" cpt where
199
  cpt props@{ listId, nodeId, session, sessionId, boxes } _ = do
James Laver's avatar
James Laver committed
200
    let sessionProps = RE.pick props :: Record SessionProps
201
    pure $ authed sessionProps $
202
      forested { boxes }
James Laver's avatar
James Laver committed
203 204
      [ documentMainLayout { listId, nodeId, mCorpusId, session } [] ]
      where mCorpusId = Nothing
205 206 207

home :: R2.Component Props
home = R.createElement homeCpt
James Laver's avatar
James Laver committed
208

209
homeCpt :: R.Component Props
James Laver's avatar
James Laver committed
210
homeCpt = here.component "home" cpt where
211 212
  cpt props@{ boxes: boxes@{ sessions, showLogin } } _ = do
    pure $ forested { boxes } [ homeLayout  { lang: LL_EN, sessions, showLogin } ]
213 214 215

lists :: R2.Component SessionNodeProps
lists = R.createElement listsCpt
James Laver's avatar
James Laver committed
216

217
listsCpt :: R.Component SessionNodeProps
James Laver's avatar
James Laver committed
218
listsCpt = here.component "lists" cpt where
219
  cpt props@{ boxes: { backend
220 221 222 223 224 225
                     , forestOpen
                     , handed
                     , reloadForest
                     , reloadRoot
                     , route
                     , sessions
226 227
                     , showLogin
                     , tasks }
James Laver's avatar
James Laver committed
228 229
            , nodeId
            , session
230
            , sessionId } _ = do
James Laver's avatar
James Laver committed
231
    let sessionProps = RE.pick props :: Record SessionProps
232 233
    pure $ authed sessionProps $
      Lists.listsWithForestSessionContext
James Laver's avatar
James Laver committed
234
      { forestProps: { backend
235
                     , forestOpen
James Laver's avatar
James Laver committed
236 237 238 239 240 241 242 243
                     , frontends
                     , handed
                     , reloadForest
                     , reloadRoot
                     , route
                     , sessions
                     , showLogin
                     , tasks }
244 245
      , listsProps: { nodeId
                    , reloadForest
246
                    , reloadRoot
247
                    , session
248 249
                    , sessionUpdate: \_ -> pure unit
                    , tasks }
James Laver's avatar
James Laver committed
250 251
      } []
      where frontends = defaultFrontends
James Laver's avatar
James Laver committed
252

253
login' :: Boxes -> R.Element
James Laver's avatar
James Laver committed
254
login' { backend, sessions, showLogin: visible } =
255 256 257 258
  login { backend
        , backends: fromFoldable defaultBackends
        , sessions
        , visible }
James Laver's avatar
James Laver committed
259

260 261
graphExplorer :: R2.Component SessionNodeProps
graphExplorer = R.createElement graphExplorerCpt
James Laver's avatar
James Laver committed
262

263
graphExplorerCpt :: R.Component SessionNodeProps
James Laver's avatar
James Laver committed
264
graphExplorerCpt = here.component "graphExplorer" cpt where
265
  cpt props@{ boxes: { backend, handed, route, sessions, showLogin, tasks }
266
            , nodeId
267
            , session } _ = do
James Laver's avatar
James Laver committed
268
    let sessionProps = RE.pick props :: Record SessionProps
269
    pure $ authed sessionProps $
James Laver's avatar
James Laver committed
270
      simpleLayout { handed }
271 272 273 274 275 276 277 278 279
      [ explorerLayoutLoader { backend
                             , frontends
                             , graphId: nodeId
                             , handed
                             , route
                             , session
                             , sessions
                             , showLogin
                             , tasks } [] ]
James Laver's avatar
James Laver committed
280
      where frontends = defaultFrontends
281 282 283

routeFile :: R2.Component SessionNodeProps
routeFile = R.createElement routeFileCpt
James Laver's avatar
James Laver committed
284

285
routeFileCpt :: R.Component SessionNodeProps
James Laver's avatar
James Laver committed
286
routeFileCpt = here.component "routeFile" cpt where
287
  cpt props@{ nodeId, session, sessionId, boxes } _ = do
James Laver's avatar
James Laver committed
288
    let sessionProps = RE.pick props :: Record SessionProps
289
    pure $ authed sessionProps $
290
      forested { boxes } [ fileLayout { nodeId, session } ]
291 292 293 294 295 296 297 298

type RouteFrameProps = (
  nodeType :: NodeType
  | SessionNodeProps
  )

routeFrame :: R2.Component RouteFrameProps
routeFrame = R.createElement routeFrameCpt
James Laver's avatar
James Laver committed
299

300
routeFrameCpt :: R.Component RouteFrameProps
James Laver's avatar
James Laver committed
301
routeFrameCpt = here.component "routeFrame" cpt where
302
  cpt props@{ nodeId, nodeType, session, sessionId, boxes } _ = do
James Laver's avatar
James Laver committed
303
    let sessionProps = RE.pick props :: Record SessionProps
304
    pure $ authed sessionProps $
305
      forested { boxes } [ frameLayout { nodeId, nodeType, session } ]
306 307 308

team :: R2.Component SessionNodeProps
team = R.createElement teamCpt
James Laver's avatar
James Laver committed
309

310
teamCpt :: R.Component SessionNodeProps
James Laver's avatar
James Laver committed
311
teamCpt = here.component "team" cpt where
312
  cpt props@{ nodeId, session, sessionId, boxes } _ = do
James Laver's avatar
James Laver committed
313
    let sessionProps = RE.pick props :: Record SessionProps
314
    pure $ authed sessionProps $
315
      forested { boxes } [ corpusLayout { nodeId, session } ]
316 317 318

texts :: R2.Component SessionNodeProps
texts = R.createElement textsCpt
James Laver's avatar
James Laver committed
319

320 321 322
textsCpt :: R.Component SessionNodeProps
textsCpt = here.component "texts" cpt
  where
323
    cpt props@{ boxes: { backend
324 325 326 327 328 329
                       , forestOpen
                       , handed
                       , reloadForest
                       , reloadRoot
                       , route
                       , sessions
330 331
                       , showLogin
                       , tasks }
332 333
              , nodeId
              , session
334
              , sessionId } _ = do
335
      let sessionProps = RE.pick props :: Record SessionProps
336 337
      pure $ authed sessionProps $
        Texts.textsWithForestSessionContext
338
        { forestProps: { backend
339
                       , forestOpen
340 341 342 343 344 345 346 347
                       , frontends
                       , handed
                       , route
                       , reloadForest
                       , reloadRoot
                       , sessions
                       , showLogin
                       , tasks }
348 349
        , textsProps: { frontends
                      , nodeId
350 351 352 353
                      , session }
        } []
        where
          frontends = defaultFrontends
354 355 356

user :: R2.Component SessionNodeProps
user = R.createElement userCpt
James Laver's avatar
James Laver committed
357

358
userCpt :: R.Component SessionNodeProps
James Laver's avatar
James Laver committed
359
userCpt = here.component "user" cpt where
360
  cpt props@{ boxes: boxes@{ reloadForest, reloadRoot, tasks }
361 362
            , nodeId
            , session
363
            , sessionId } _ = do
James Laver's avatar
James Laver committed
364
    let sessionProps = RE.pick props :: Record SessionProps
365
    pure $ authed sessionProps $
366
      forested { boxes }
367 368 369 370 371 372
        [ userLayoutSessionContext { frontends
                                   , nodeId
                                   , reloadForest
                                   , reloadRoot
                                   , session
                                   , tasks } [] ]
James Laver's avatar
James Laver committed
373
      where frontends = defaultFrontends
374

James Laver's avatar
James Laver committed
375
type ContactProps = ( annuaireId :: NodeID | SessionNodeProps )
376 377 378

contact :: R2.Component ContactProps
contact = R.createElement contactCpt
James Laver's avatar
James Laver committed
379

380
contactCpt :: R.Component ContactProps
James Laver's avatar
James Laver committed
381
contactCpt = here.component "contact" cpt where
382 383
  cpt props@{ annuaireId, nodeId, session, sessionId
            , boxes: { reloadForest, reloadRoot, tasks } } _ = do
James Laver's avatar
James Laver committed
384 385
    let sessionProps = RE.pick props :: Record SessionProps
    let forestedProps = RE.pick props :: Record Props
386
    pure $ authed sessionProps $
James Laver's avatar
James Laver committed
387 388 389
      forested forestedProps
      [ contactLayout { annuaireId, frontends, nodeId, reloadForest, reloadRoot, session, tasks } [] ]
      where frontends = defaultFrontends