1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
150
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
module Gargantext.Components.Forest.Tree.Node
( nodeSpan
, blankNodeSpan
) where
import Gargantext.Prelude
import Data.Array.NonEmpty as NArray
import Data.Foldable (intercalate)
import Data.Maybe (Maybe(..), maybe)
import Data.String.Regex as Regex
import Data.Symbol (SProxy(..))
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Effect.Aff (Aff, launchAff)
import Effect.Class (liftEffect)
import Gargantext.AsyncTasks as GAT
import Gargantext.Components.App.Store (Boxes)
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (ComponentStatus(..), Elevation(..), TooltipEffect(..), Variant(..))
import Gargantext.Components.Forest.Tree.Node.Action.Types (Action(..))
import Gargantext.Components.Forest.Tree.Node.Action.Upload (DroppedFile(..), fileTypeView)
import Gargantext.Components.Forest.Tree.Node.Action.Upload.Types (FileType(..), UploadFileBlob(..))
import Gargantext.Components.Forest.Tree.Node.Box (nodePopupView)
import Gargantext.Components.Forest.Tree.Node.Settings (SettingsBox(..), settingsBox)
import Gargantext.Components.Forest.Tree.Node.Tools.Sync (nodeActionsGraph, nodeActionsNodeList)
import Gargantext.Components.GraphExplorer.API as GraphAPI
import Gargantext.Components.Lang (Lang(EN))
import Gargantext.Components.Nodes.Corpus (loadCorpusWithChild)
import Gargantext.Config.REST (logRESTError)
import Gargantext.Context.Progress (asyncContext, asyncProgress)
import Gargantext.Ends (Frontends, url)
import Gargantext.Hooks.Loader (useLoaderEffect)
import Gargantext.Hooks.Version (Version, useVersion)
import Gargantext.Routes as Routes
import Gargantext.Sessions (Session, sessionId)
import Gargantext.Types (ID, Name)
import Gargantext.Types as GT
import Gargantext.Utils (nbsp, textEllipsisBreak, (?))
import Gargantext.Utils.Reactix as R2
import Gargantext.Utils.Toestand as T2
import React.SyntheticEvent as SE
import Reactix as R
import Reactix.DOM.HTML as H
import Record as Record
import Toestand as T
-- (?) never been able to properly declare PureScript Regex...
foreign import nodeUserRegexp :: Regex.Regex
here :: R2.Here
here = R2.here "Gargantext.Components.Forest.Tree.Node"
-- Main Node
type NodeSpanProps =
( boxes :: Boxes
, dispatch :: Action -> Aff Unit
, folderOpen :: T.Box Boolean
, frontends :: Frontends
, id :: ID
, isLeaf :: IsLeaf
, name :: Name
, nodeType :: GT.NodeType
, reload :: T2.ReloadS
, root :: ID
, session :: Session
, isBoxVisible :: T.Box Boolean
)
type IsLeaf = Boolean
nodeSpan :: R2.Leaf NodeSpanProps
nodeSpan = R2.leaf nodeSpanCpt
nodeSpanCpt :: R.Component NodeSpanProps
nodeSpanCpt = here.component "nodeSpan" cpt
where
cpt props@{ boxes: boxes@{ errors
, reloadMainPage
, reloadRoot
, route
, tasks }
, dispatch
, folderOpen
, frontends
, id
, isLeaf
, nodeType
, reload
, session
, isBoxVisible
} _ = do
-- States
route' <- T.useLive T.unequal route
-- only 1 popup at a time is allowed to be opened
droppedFile <- T.useBox (Nothing :: Maybe DroppedFile)
droppedFile' <- T.useLive T.unequal droppedFile
isDragOver <- T.useBox false
isDragOver' <- T.useLive T.unequal isDragOver
currentTasks <- GAT.focus id tasks
currentTasks' <- T.useLive T.unequal currentTasks
folderOpen' <- R2.useLive' folderOpen
-- tasks' <- T.read tasks
-- Computed
let
dropClass :: Maybe DroppedFile -> Boolean -> String
dropClass (Just _) _ = "mainleaf--file-dropped"
dropClass _ true = "mainleaf--file-dropped"
dropClass Nothing _ = ""
name' :: String -> GT.NodeType -> Session -> String
name' _ GT.NodeUser s = show s
name' n _ _ = n
isSelected = Just route' == Routes.nodeTypeAppRoute nodeType (sessionId session) id
SettingsBox {show: showBox} = settingsBox nodeType
href = url frontends $ GT.NodePath (sessionId session) nodeType (Just id)
name = name' props.name nodeType session
-- Methods
dropHandler :: forall event.
SE.SyntheticEvent_ event
-> Effect Unit
dropHandler e = do
-- prevent redirection when file is dropped
SE.preventDefault e
SE.stopPropagation e
blob <- R2.dataTransferFileBlob e
void $ launchAff do
--contents <- readAsText blob
liftEffect $ do
T.write_ (Just
$ DroppedFile { blob: (UploadFileBlob blob)
, fileType: Just CSV
, lang : EN
}) droppedFile
onDragOverHandler :: forall event.
T.Box Boolean
-> SE.SyntheticEvent_ event
-> Effect Unit
onDragOverHandler box e = do
-- prevent redirection when file is dropped
-- https://stackoverflow.com/a/6756680/941471
SE.preventDefault e
SE.stopPropagation e
T.write_ true box
onDragLeave :: forall event.
T.Box Boolean
-> SE.SyntheticEvent_ event
-> Effect Unit
onDragLeave box _ = T.write_ false box
onTaskFinish ::
GT.NodeID
-> GT.AsyncTaskWithType
-> Unit
-> Effect Unit
onTaskFinish id' t _ = do
GAT.finish id' t tasks
if GAT.asyncTaskTTriggersAppReload t then do
here.log2 "reloading root for task" t
T2.reload reloadRoot
else do
if GAT.asyncTaskTTriggersTreeReload t then do
here.log2 "reloading tree for task" t
T2.reload reload
else do
here.log2 "task doesn't trigger a tree reload" t
pure unit
if GAT.asyncTaskTTriggersMainPageReload t then do
here.log2 "reloading main page for task" t
T2.reload reloadMainPage
else do
here.log2 "task doesn't trigger a main page reload" t
pure unit
-- snd tasks $ GAT.Finish id' t
-- mT <- T.read tasks
-- case mT of
-- Just t' -> snd t' $ GAT.Finish id' t
-- Nothing -> pure unit
-- T2.reload reloadRoot
-- NOTE Don't toggle tree if it is not selected
onNodeLinkClick :: Unit -> Effect Unit
onNodeLinkClick _ = when (not isSelected) (T.write_ true folderOpen)
-- Hooks
mVersion <- useVersion $ nodeType == GT.NodeUser ?
Just { session } $
Nothing
host <- R2.getPortalHost
-- Render
pure $
H.span
{ className: intercalate " "
[ "mainleaf"
, dropClass droppedFile' isDragOver'
, isSelected ? "mainleaf--selected" $ ""
]
, on: { dragLeave: onDragLeave isDragOver
, dragOver: onDragOverHandler isDragOver
, drop: dropHandler
}
}
[
-- // Abstract informations //
nodeTooltip
{ id
, nodeType
, name
}
[
case mVersion of
Nothing -> mempty
Just v -> versionComparator v
]
,
R.createPortal
[
fileTypeView
{ dispatch
, droppedFile
, id
, isDragOver
, nodeType
, key: "fileType-" <> show id
}
]
host
,
-- // Leaf informations data //
folderIcon
{ isLeaf
, isOpened: folderOpen'
, callback: const $ T.modify_ (not) folderOpen
}
,
nodeIcon
(
{ nodeType
, isLeaf
, callback: const $ T.modify_ (not) folderOpen
, isSelected
}
)
[
case mVersion of
Nothing -> mempty
Just { clientVersion, remoteVersion} ->
B.iconButton $
{ className: intercalate " "
[ "mainleaf__version-badge"
, clientVersion == remoteVersion ?
"mainleaf__version-badge--matched" $
"mainleaf__version-badge--mismatched"
]
, name: clientVersion == remoteVersion ?
"check-circle" $
"exclamation-circle"
, callback: const $ T.modify_ (not) folderOpen
, overlay: false
}
]
,
nodeLink
{ callback: onNodeLinkClick
, href
, id
, name: name' props.name nodeType session
, type: nodeType
}
,
-- // Leaf action features //
nodeActions
{ id
, nodeType
, refresh: const $ dispatch RefreshTree
, session
} []
,
R2.when (showBox) $
B.iconButton
{ name: "cog"
, className: "mainleaf__settings-icon"
, callback: \_ -> T.write_ true isBoxVisible
, title:
"Each node of the Tree can perform some actions.\n"
<> "Click here to execute one of them."
, variant: Secondary
, elevation: Level1
}
,
R.fragment $ flip map currentTasks' \task ->
asyncProgress
{ asyncTask: task
, errors
, nodeId: id
, onFinish: onTaskFinish id task
, session
}
[
taskProgress
{}
]
,
-- // Modals //
B.baseModal
{ isVisibleBox: isBoxVisible
, noBody: true
, noHeader: true
, modalClassName: "forest-tree-node-modal"
}
[
nodePopupView
{ boxes
, dispatch
, id
, name
, nodeType
, closeCallback: \_ -> T.write_ false isBoxVisible
, session
}
]
]
---------------------------------------------------------
type NodeIconProps =
( nodeType :: GT.NodeType
, callback :: Unit -> Effect Unit
, isLeaf :: Boolean
, isSelected :: Boolean
)
nodeIcon :: R2.Component NodeIconProps
nodeIcon = R2.component nodeIconCpt
nodeIconCpt :: R.Component NodeIconProps
nodeIconCpt = here.component "nodeIcon" cpt where
cpt { nodeType
, callback
, isLeaf
, isSelected
} children = do
-- Render
pure $
H.span
{ className: "mainleaf__node-icon" } $
[
B.iconButton
{ name: GT.getIcon nodeType true
, callback
, status: isLeaf ? Idled $ Enabled
, variant: isSelected ? Primary $ Dark
, overlay: false
}
]
<> children
-----------------------------------------------
type FolderIconProps =
( isOpened :: Boolean
, callback :: Unit -> Effect Unit
, isLeaf :: Boolean
)
folderIcon :: R2.Leaf FolderIconProps
folderIcon = R2.leaf folderIconCpt
folderIconCpt :: R.Component FolderIconProps
folderIconCpt = here.component "folderIcon" cpt where
cpt { isLeaf: true } _ = pure $
B.icon
{ className: intercalate " "
["mainleaf__folder-icon"
, "mainleaf__folder-icon--leaf"
]
, name: "caret-right"
}
cpt { callback, isOpened } _ = pure $
B.iconButton
{ className: "mainleaf__folder-icon"
, name: isOpened ? "caret-down" $ "caret-right"
, overlay: false
, callback
}
-----------------------------------------------
type NodeLinkProps =
( callback :: Unit -> Effect Unit
, href :: String
, id :: Int
, name :: GT.Name
, type :: GT.NodeType
)
nodeLink :: R2.Leaf NodeLinkProps
nodeLink = R2.leaf nodeLinkCpt
nodeLinkCpt :: R.Component NodeLinkProps
nodeLinkCpt = here.component "nodeLink" cpt where
cpt { callback
, href
, id
, name
, type: nodeType
} _ = do
-- Computed
let
tid = tooltipId name id
aProps =
{ href
} `Record.merge` B.tooltipBind tid
-- Render
pure $
H.div
{ className: "mainleaf__node-link"
, on: { click: const $ callback unit }
}
[
H.a
aProps
[
B.span_ $ nodeLinkText nodeType name
]
]
nodeLinkText :: GT.NodeType -> String -> String
nodeLinkText GT.NodeUser s = s # (truncateNodeUser)
>>> maybe s identity
nodeLinkText _ s = textEllipsisBreak 15 s
truncateNodeUser :: String -> Maybe String
truncateNodeUser = Regex.match (nodeUserRegexp) >=> flip NArray.index 1 >>> join
---------------------------------------------------
type NodeTooltipProps =
( name :: String
, id :: GT.NodeID
, nodeType :: GT.NodeType
)
nodeTooltip :: R2.Component NodeTooltipProps
nodeTooltip = R2.component nodeTooltipCpt
nodeTooltipCpt :: R.Component NodeTooltipProps
nodeTooltipCpt = here.component "nodeTooltip" cpt where
cpt { name, id, nodeType } children = pure $
B.tooltip
{ id: tooltipId name id
, effect: FloatEffect
, delayShow: 600
} $
[
H.b
{}
[
B.icon
{ name: GT.getIcon nodeType true }
,
B.span_ $
GT.prettyNodeType nodeType
]
,
B.div_ $
name
]
<> children
tooltipId :: String -> GT.NodeID -> String
tooltipId name id = name <> "-node-link-" <> show id
---------------------------------------------------------
blankNodeSpan :: R2.Leaf ()
blankNodeSpan = R2.leaf blankNodeSpanCpt
blankNodeSpanCpt :: R.Component ()
blankNodeSpanCpt = here.component "__blank__" cpt where
cpt _ _ = pure $
H.div { className: "mainleaf mainleaf--blank" }
[
B.icon { className: "mainleaf__folder-icon", name: "caret-right"}
,
H.span { className: "mainleaf__node-icon" }
[
B.icon { name: "circle" }
]
,
H.div { className: "mainleaf__node-link"} [ H.text $ nbsp 1 ]
]
-----------------------------------------------
-- START nodeActions
type NodeActionsCommon =
( id :: ID
, refresh :: Unit -> Aff Unit
, session :: Session
)
type NodeActionsProps = ( nodeType :: GT.NodeType | NodeActionsCommon )
nodeActions :: R2.Component NodeActionsProps
nodeActions = R.createElement nodeActionsCpt
nodeActionsCpt :: R.Component NodeActionsProps
nodeActionsCpt = here.component "nodeActions" cpt where
cpt props _ = pure (child props.nodeType)
where
nodeActionsP = SProxy :: SProxy "nodeType"
childProps = Record.delete nodeActionsP props
child GT.NodeList = listNodeActions childProps
child GT.Graph = graphNodeActions childProps
child _ = mempty
graphNodeActions :: R2.Leaf NodeActionsCommon
graphNodeActions = R2.leafComponent graphNodeActionsCpt
graphNodeActionsCpt :: R.Component NodeActionsCommon
graphNodeActionsCpt = here.component "graphNodeActions" cpt where
cpt { id, session, refresh } _ = do
-- States
state /\ stateBox <- R2.useBox' Nothing
-- Hooks
useLoaderEffect
{ errorHandler
, loader: graphVersions session
, path: id
, state: stateBox
}
-- Render
pure $ R2.fromMaybe state \gv ->
nodeActionsGraph
{ graphVersions: gv, session, id, refresh }
[]
graphVersions session graphId = GraphAPI.graphVersions { graphId, session }
errorHandler = logRESTError here "[graphNodeActions]"
listNodeActions :: R2.Leaf NodeActionsCommon
listNodeActions = R2.leafComponent listNodeActionsCpt
listNodeActionsCpt :: R.Component NodeActionsCommon
listNodeActionsCpt = here.component "listNodeActions" cpt where
cpt { id, session, refresh } _ = do
-- States
state /\ stateBox <- R2.useBox' Nothing
-- Hooks
useLoaderEffect
{ errorHandler
, loader: loadCorpusWithChild
, path: { nodeId: id, session }
, state: stateBox
}
-- Render
pure $ R2.fromMaybe state \{ corpusId } ->
nodeActionsNodeList
{ listId: id
, nodeId: corpusId
, session
, refresh: refresh
, nodeType: GT.TabNgramType GT.CTabTerms
}
where
errorHandler = logRESTError here "[listNodeActions]"
-----------------------------------------------
type VersionComparatorProps =
( clientVersion :: Version
, remoteVersion :: Version
)
versionComparator :: R2.Leaf VersionComparatorProps
versionComparator = R2.leaf versionComparatorCpt
versionComparatorCpt :: R.Component VersionComparatorProps
versionComparatorCpt = here.component "versionComparator" cpt where
cpt { clientVersion, remoteVersion } _
| clientVersion == remoteVersion = pure $
B.caveat
{ variant: Success
, className: "mainleaf__version-comparator"
}
[
B.b_ "Versions match"
,
content clientVersion remoteVersion
]
| otherwise = pure $
B.caveat
{ variant: Warning
, className: "mainleaf__version-comparator"
}
[
B.b_ "Versions mismatch"
,
content clientVersion remoteVersion
]
content :: Version -> Version -> R.Element
content clientVersion remoteVersion =
H.ul
{}
[
H.li
{}
[
B.span_ "frontend: "
,
H.text $ nbsp 1
,
B.code_ clientVersion
]
,
H.li
{}
[
B.span_ "backend: "
,
H.text $ nbsp 1
,
B.code_ remoteVersion
]
]
-------------------------------------------------------
taskProgress :: R2.Leaf ()
taskProgress = R2.leaf taskProgressCpt
taskProgressCpt :: R.Component ()
taskProgressCpt = here.component "progress" cpt where
cpt _ _ = do
-- Context
asyncProgressContext <- R.useContext asyncContext
-- Render
pure $
H.span
{ className: "mainleaf__progress-bar" }
[
B.progressBar
{ value: asyncProgressContext
, variant: Info
}
]