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
module Gargantext.Components.GraphExplorer.Sidebar.DocList
( docListWrapper
) where
import Gargantext.Prelude
import Data.Array (catMaybes, head)
import Data.Array as A
import Data.Foldable (intercalate)
import Data.Map as Map
import Data.Maybe (Maybe(..))
import Data.Sequence as Seq
import Data.Set as Set
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (Variant(..))
import Gargantext.Components.FacetsTable (DocumentsView(..), Rows(..), initialPageGQL, loadPageGQL, publicationDate)
import Gargantext.Components.GraphExplorer.Store as GraphStore
import Gargantext.Components.GraphExplorer.Types (CorpusId, DocId, GraphSideDoc(..), ListId)
import Gargantext.Components.GraphExplorer.Types as GET
import Gargantext.Config (defaultFrontends)
import Gargantext.Config.REST (RESTError(..))
import Gargantext.Ends (Frontends, url)
import Gargantext.Hooks.Loader (useLoaderEffect)
import Gargantext.Hooks.Session (useSession)
import Gargantext.Hooks.Sigmax.Types as SigmaxT
import Gargantext.Hooks.UpdateEffect (useUpdateEffect1')
import Gargantext.Routes as Routes
import Gargantext.Sessions (Session, sessionId)
import Gargantext.Utils ((?))
import Gargantext.Utils.Reactix as R2
import React.SyntheticEvent as SE
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T
here :: R2.Here
here = R2.here "Gargantext.Components.GraphExplorer.Sidebar.DocList"
type Props =
( metaData :: GET.MetaData
)
docListWrapper :: R2.Leaf Props
docListWrapper = R2.leaf docListWrapperCpt
docListWrapperCpt :: R.Component Props
docListWrapperCpt = here.component "wrapper" cpt where
cpt { metaData: GET.MetaData metaData
} _ = do
-- | States
-- |
session <- useSession
{ showDoc
, graph
, selectedNodeIds
} <- GraphStore.use
graph' <- R2.useLive' graph
selectedNodeIds' <- R2.useLive' selectedNodeIds
selectedNgramsTerms <- T.useBox []
-- | Helpers
-- |
let
nodesMap = SigmaxT.nodesGraphMap graph'
-- | Hooks
-- |
R.useEffect1' selectedNodeIds' $ do
T.write_ (catMaybes $ (\id -> _.label <$> Map.lookup id nodesMap) <$> Set.toUnfoldable selectedNodeIds') selectedNgramsTerms
-- | Render
-- |
pure $
R.fragment
[
case (head metaData.corpusId) /\ (Set.isEmpty selectedNodeIds') of
(Just corpusId) /\ false ->
docList
{ corpusId
, frontends: defaultFrontends
, listId: metaData.list.listId
, selectedNgramsTerms
, session
, showDoc
}
_ /\ _ ->
B.caveat
{}
[
H.text "You can link a corpus to retrieve relative documents about your selection"
]
]
-------------------------------------------------------------------
type ListProps =
( corpusId :: CorpusId
, frontends :: Frontends
, listId :: ListId
, selectedNgramsTerms :: T.Box (Array SigmaxT.Label)
, session :: Session
, showDoc :: T.Box (Maybe GraphSideDoc)
)
docList :: R2.Leaf ListProps
docList = R2.leaf docListCpt
docListCpt :: R.Component ListProps
docListCpt = here.component "main" cpt where
-- | Helpers
-- |
errorHandler err = do
here.warn2 "[docList] RESTError" err
case err of
ReadJSONError err' ->
here.warn2 "[docList] ReadJSONError" $ show err'
_ -> pure unit
-- | Component
-- |
cpt { corpusId: nodeId
, frontends
, listId
, selectedNgramsTerms
, session
, showDoc
} _ = do
-- | States
-- |
-- path' /\ path
-- <- R2.useBox' $ initialPagePath { nodeId, listId, query, session }
selectedNgramsTerms' <- T.useLive T.unequal selectedNgramsTerms
path' /\ path
<- R2.useBox' $ initialPageGQL { corpusId: nodeId
, ngramsTerms: A.fromFoldable selectedNgramsTerms'
, session }
state' /\ state <-
R2.useBox' Nothing
rows' /\ rows <-
R2.useBox' Nothing
showDoc' <-
R2.useLive' showDoc
-- | Hooks
-- |
useLoaderEffect
{ errorHandler
, loader: loadPageGQL
, path: path'
, state
}
-- | Effects
-- |
-- (on query change, reload fetched docs)
--useUpdateEffect1' query $
--flip T.write_ path $ initialPagePath { nodeId, listId, query, session }
useUpdateEffect1' selectedNgramsTerms' $
flip T.write_ path $ initialPageGQL { corpusId: nodeId
, ngramsTerms: A.fromFoldable selectedNgramsTerms'
, session }
-- (on fetch success, extract existing docs)
useUpdateEffect1' state' do
-- here.log2 "[docList] state'" state'
case state' of
Nothing -> T.write_ (Just Seq.empty) rows
Just r -> case r of
Docs { docs } -> T.write_ (Just docs) rows
_ -> T.write_ (Just Seq.empty) rows
-- | Computed
-- |
let
callback :: Maybe GraphSideDoc -> DocId -> Effect Unit
callback
Nothing
new
= setGraphSideDoc new # Just # flip T.write_ showDoc
callback
(Just (GraphSideDoc { docId }))
new
| docId == new = T.write_ Nothing showDoc
| otherwise = setGraphSideDoc new # Just # flip T.write_ showDoc
setGraphSideDoc :: DocId -> GraphSideDoc
setGraphSideDoc docId = GraphSideDoc
{ docId
, listId
, corpusId: nodeId
}
isSelected :: Maybe GraphSideDoc -> DocumentsView -> Boolean
isSelected
(Just (GraphSideDoc { docId }))
(DocumentsView { id })
= docId == id
isSelected
_
_
= false
-- | Render
-- |
pure $
R2.fromMaybe rows' \results ->
R.fragment
[
R2.when (results == Seq.empty) $
B.caveat
{}
[
H.text "Searching relevant document(s)"
]
,
R2.when (not $ eq results Seq.empty) $
H.div {}
[
H.h6 {} [ H.text $ show (Seq.length results) <> " related documents"]
, H.ul
{ className: intercalate " "
[ "graph-doc-list"
, "list-group"
]
} $
Seq.toUnfoldable $ flip Seq.map results \r ->
item
{ documentView: (r :: DocumentsView)
, callback: callback showDoc'
, isSelected: isSelected showDoc' (r :: DocumentsView)
, listId
, corpusId: nodeId
, session
, frontends
}
]
]
---------------------------------------------------------
type ItemProps =
( documentView :: DocumentsView
, callback :: DocId -> Effect Unit
, isSelected :: Boolean
, corpusId :: CorpusId
, listId :: ListId
, session :: Session
, frontends :: Frontends
)
item :: R2.Leaf ItemProps
item = R2.leaf itemCpt
itemCpt :: R.Component ItemProps
itemCpt = here.component "item" cpt where
cpt { documentView: dv@(DocumentsView { id, title, source })
, callback
, isSelected
, listId
, corpusId
, session
, frontends
} _ = do
-- Computed
let
-- Creating a href link
route = Routes.CorpusDocument (sessionId session) corpusId listId id
href = url frontends route
-- Methods
let
onClick ::
SE.SyntheticMouseEvent
-> Effect Unit
onClick event
= R2.externalOpeningFlag event
>>= case _ of
true -> R.nothing
false -> SE.preventDefault event *> callback id
-- Render
pure $
H.a
{ className: intercalate " "
[ "graph-doc-list__item"
, isSelected ? "graph-doc-list__item--selected" $ ""
, "list-group-item"
, "text-decoration-none"
]
, on: { click: onClick }
, href
}
[
B.ripple
{ variant: Dark }
[
H.div
{ className: "graph-doc-list__item__main" }
[
B.div'
{ className: "graph-doc-list__item__title" }
title
,
B.div'
{ className: "graph-doc-list__item__source" }
source
,
B.div'
{ className: "graph-doc-list__item__date" } $
publicationDate dv
]
,
H.div
{ className: "graph-doc-list__item__aside" }
[
B.icon
{ name: "eye-slash"
, className: intercalate " "
[ "text-info"
, isSelected ? "visible" $ "hidden"
]
}
]
]
]