Texts.purs 6.54 KB
Newer Older
1
module Gargantext.Components.Nodes.Texts where
2

3 4 5
import Prelude
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow)
6
import Data.Maybe (Maybe(..))
7
import Data.Tuple.Nested ((/\))
8
import Effect.Aff (launchAff_)
9
import Reactix as R
10
import Reactix.DOM.HTML as H
11
--------------------------------------------------------
12
import Gargantext.Components.DocsTable as DT
13
import Gargantext.Components.Loader (loader)
14
import Gargantext.Components.NgramsTable.Loader (clearCache)
15
import Gargantext.Components.Node (NodePoly(..))
16
import Gargantext.Components.Nodes.Corpus (loadCorpusWithChild)
17
import Gargantext.Components.Nodes.Corpus.Chart.Histo (histo)
18
import Gargantext.Components.Nodes.Corpus.Types (CorpusData, Hyperdata(..), getCorpusInfo, CorpusInfo(..))
19
import Gargantext.Components.Nodes.Lists.Types as NT
20
import Gargantext.Components.Tab as Tab
21
import Gargantext.Components.Table as Table
James Laver's avatar
James Laver committed
22
import Gargantext.Ends (Frontends)
23
import Gargantext.Sessions (Session, sessionId)
24
import Gargantext.Types (CTabNgramType(..), TabSubType(..), TabType(..))
25 26 27
import Gargantext.Utils.Reactix as R2

thisModule = "Gargantext.Components.Nodes.Texts"
28
--------------------------------------------------------
29

30 31 32 33 34
type Props = (
    frontends :: Frontends
  , nodeId :: Int
  , session :: Session
  )
35 36 37 38

textsLayout :: Record Props -> R.Element
textsLayout props = R.createElement textsLayoutCpt props []

39
------------------------------------------------------------------------
40
textsLayoutCpt :: R.Component Props
41
textsLayoutCpt = R2.hooksComponent thisModule "textsLayout" cpt where
42 43 44 45 46 47 48 49 50 51 52 53 54
  cpt { frontends, nodeId, session } _ = do
    let sid = sessionId session

    pure $ textsLayoutWithKey { frontends, key: show sid <> "-" <> show nodeId, nodeId, session }

type KeyProps = (
  key :: String
  | Props
  )

textsLayoutWithKey :: Record KeyProps -> R.Element
textsLayoutWithKey props = R.createElement textsLayoutWithKeyCpt props []

55
textsLayoutWithKeyCpt :: R.Component KeyProps
56
textsLayoutWithKeyCpt = R2.hooksComponent thisModule "textsLayoutWithKey" cpt
57 58
  where
    cpt { frontends, nodeId, session } _ = do
59 60 61 62 63 64 65 66 67 68
      cacheState <- R.useState' NT.CacheOn

      pure $ loader {session, nodeId} loadCorpusWithChild $
        \corpusData@{ corpusId, corpusNode, defaultListId } -> do
          let NodePoly { name, date, hyperdata: Hyperdata h } = corpusNode
              CorpusInfo { authors, desc, query } = getCorpusInfo h.fields
              tabs' = tabs { corpusData, corpusId, frontends, session }
              title = "Corpus " <> name

          R.fragment [
69
              Table.tableHeaderLayout { afterCacheStateChange: \_ -> launchAff_ $ clearCache unit
70 71 72 73 74 75 76 77
                                      , cacheState
                                      , date
                                      , desc
                                      , query
                                      , title
                                      , user: authors }
            , tabs'
          ]
78 79 80 81 82 83 84 85 86 87 88 89 90 91

data Mode = MoreLikeFav | MoreLikeTrash

derive instance genericMode :: Generic Mode _

instance showMode :: Show Mode where
  show = genericShow

derive instance eqMode :: Eq Mode

modeTabType :: Mode -> CTabNgramType
modeTabType MoreLikeFav    = CTabAuthors  -- TODO
modeTabType MoreLikeTrash  = CTabSources  -- TODO

James Laver's avatar
James Laver committed
92
type TabsProps = ( frontends :: Frontends, session :: Session, corpusId :: Int, corpusData :: CorpusData )
93 94 95 96 97

tabs :: Record TabsProps -> R.Element
tabs props = R.createElement tabsCpt props []

tabsCpt :: R.Component TabsProps
98
tabsCpt = R2.hooksComponent thisModule "tabs" cpt
99
  where
James Laver's avatar
James Laver committed
100
    cpt {frontends, session, corpusId, corpusData} _ = do
101 102 103 104 105
      (selected /\ setSelected) <- R.useState' 0
      pure $ Tab.tabs { tabs: tabs', selected }
      where
        tabs' = [ "Documents"     /\ docs,        "Trash"           /\ trash
                , "More like fav" /\ moreLikeFav, "More like trash" /\ moreLikeTrash ]
James Laver's avatar
James Laver committed
106
        docView' tabType = docView { frontends, session, corpusId, corpusData, tabType }
107 108
        docs = R.fragment [ docsHisto, docView' TabDocs ]
        docsHisto = histo { path, session }
109 110
          where
            path = { corpusId, listId: 0, limit: Nothing, tabType: TabCorpus TabDocs }
111 112 113 114
        moreLikeFav = docView' TabMoreLikeFav
        moreLikeTrash = docView' TabMoreLikeTrash
        trash = docView' TabTrash

James Laver's avatar
James Laver committed
115 116 117 118 119 120
type DocViewProps a =
  ( frontends :: Frontends
  , session :: Session
  , corpusId :: Int
  , corpusData :: CorpusData
  , tabType :: TabSubType a )
121 122 123 124 125

docView :: forall a. Record (DocViewProps a) -> R.Element
docView props = R.createElement docViewCpt props []

docViewCpt :: forall a. R.Component (DocViewProps a)
126
docViewCpt = R2.hooksComponent thisModule "docView" cpt
127
  where
James Laver's avatar
James Laver committed
128
    cpt {frontends, session, corpusId, corpusData: {defaultListId}, tabType} _children = do
129 130 131 132 133 134 135 136 137 138 139 140
      pure $ DT.docViewLayout $ params tabType
      where
        params :: forall b. TabSubType b -> Record DT.LayoutProps
        params TabDocs =
          { nodeId: corpusId
            -- ^ TODO merge nodeId and corpusId in DT
          , chart  : H.div {} []
          , tabType: TabCorpus TabDocs
          , totalRecords: 4737
          , listId: defaultListId
          , corpusId: Just corpusId
          , showSearch: true
James Laver's avatar
James Laver committed
141
          , frontends, session }
142 143 144 145 146 147 148 149 150
        params TabMoreLikeFav =
          { nodeId: corpusId
            -- ^ TODO merge nodeId and corpusId in DT
          , chart  : H.div {} []
          , tabType: TabCorpus TabMoreLikeFav
          , totalRecords: 4737
          , listId: defaultListId
          , corpusId: Just corpusId
          , showSearch: false
James Laver's avatar
James Laver committed
151
          , frontends, session }
152 153 154 155 156 157 158 159 160
        params TabMoreLikeTrash =
          { nodeId: corpusId
            -- ^ TODO merge nodeId and corpusId in DT
          , chart  : H.div {} []
          , tabType: TabCorpus TabMoreLikeTrash
          , totalRecords: 4737
          , listId: defaultListId
          , corpusId: Just corpusId
          , showSearch: false
James Laver's avatar
James Laver committed
161
          , frontends, session }
162 163 164 165 166 167 168 169 170
        params TabTrash =
          { nodeId: corpusId
            -- ^ TODO merge nodeId and corpusId in DT
          , chart  : H.div {} []
          , tabType: TabCorpus TabTrash
          , totalRecords: 4737
          , listId: defaultListId
          , corpusId: Nothing
          , showSearch: true
James Laver's avatar
James Laver committed
171
          , frontends, session }
172 173 174 175 176 177 178 179 180 181
        -- DUMMY
        params _ =
          { nodeId: corpusId
            -- ^ TODO merge nodeId and corpusId in DT
          , chart  : H.div {} []
          , tabType: TabCorpus TabTrash
          , totalRecords: 4737
          , listId: defaultListId
          , corpusId: Nothing
          , showSearch: true
James Laver's avatar
James Laver committed
182
          , frontends, session }