Frame.purs 5.38 KB
Newer Older
1 2
module Gargantext.Components.Nodes.Frame where

Karen Konou's avatar
Karen Konou committed
3 4
import Gargantext.Prelude

5 6
import DOM.Simple as DOM
import Data.Either (Either)
7
import Data.Eq.Generic (genericEq)
8
import Data.Generic.Rep (class Generic)
9
import Data.Maybe (Maybe(..))
10
import Data.Newtype (class Newtype)
11
import Data.Nullable (Nullable, null, toMaybe)
12
import Data.Show.Generic (genericShow)
James Laver's avatar
James Laver committed
13
import Effect.Aff (Aff)
14 15 16 17
import Reactix as R
import Reactix.DOM.HTML as H
import Simple.JSON as JSON
import Toestand as T
18
import Web.URL as WURL
19

20
import Gargantext.Components.FolderView as FV
Karen Konou's avatar
Karen Konou committed
21
import Gargantext.Components.Node (NodePoly(..))
22
import Gargantext.Config.REST (RESTError)
23 24 25
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Routes (SessionRoute(NodeAPI))
import Gargantext.Sessions (Session, get, sessionId)
26
import Gargantext.Types (NodeType(..))
27
import Gargantext.Utils.JitsiMeet as JM
28
import Gargantext.Utils.Reactix as R2
29
import Gargantext.Utils.Toestand as T2
30

James Laver's avatar
James Laver committed
31 32
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Frame"
33

34
newtype Hyperdata = Hyperdata { base :: String, frame_id :: String }
35
derive instance Generic Hyperdata _
36 37 38 39 40
derive instance Newtype Hyperdata _
instance Eq Hyperdata where eq = genericEq
instance Show Hyperdata where show = genericShow
derive newtype instance JSON.ReadForeign Hyperdata
derive newtype instance JSON.WriteForeign Hyperdata
41 42

type Props =
43 44
  ( nodeId   :: Int
  , nodeType :: NodeType
45
  , session  :: Session
46 47 48
  )

type KeyProps =
James Laver's avatar
James Laver committed
49
  ( key      :: String
50
  | Props
51 52
  )

James Laver's avatar
James Laver committed
53
frameLayout :: R2.Leaf Props
54
frameLayout props = R.createElement frameLayoutCpt props []
55
frameLayoutCpt :: R.Component Props
James Laver's avatar
James Laver committed
56
frameLayoutCpt = here.component "frameLayout" cpt where
57 58 59 60
  cpt { nodeId, nodeType, session } _ = do
    pure $ frameLayoutWithKey { key, nodeId, nodeType, session }
      where
        key = show (sessionId session) <> "-" <> show nodeId
61

James Laver's avatar
James Laver committed
62
frameLayoutWithKey :: R2.Leaf KeyProps
63 64
frameLayoutWithKey props = R.createElement frameLayoutWithKeyCpt props []
frameLayoutWithKeyCpt :: R.Component KeyProps
James Laver's avatar
James Laver committed
65 66
frameLayoutWithKeyCpt = here.component "frameLayoutWithKey" cpt where
  cpt { nodeId, session, nodeType} _ = do
67 68
    reload <- T.useBox T2.newReload
    reload' <- T.useLive T.unequal reload
69 70 71 72 73 74
    useLoader { errorHandler
              , loader: loadframeWithReload
              , path: {nodeId, reload: reload', session}
              , render: \frame -> frameLayoutView {frame, nodeId, reload, session, nodeType} }
    where
      errorHandler err = here.log2 "[frameLayoutWithKey] RESTError" err
75 76

type ViewProps =
77 78
  ( frame    :: NodePoly Hyperdata
  , reload   :: T2.ReloadS
James Laver's avatar
James Laver committed
79 80 81
  , nodeId   :: Int
  , session  :: Session
  , nodeType :: NodeType
82 83 84
  )

type Base = String
James Laver's avatar
James Laver committed
85

86 87
type FrameId = String

88
hframeUrl :: NodeType -> Base -> FrameId -> String
James Laver's avatar
James Laver committed
89
hframeUrl NodeFrameNotebook _ frame_id = frame_id  -- Temp fix : frame_id is currently the whole url created
90 91
hframeUrl NodeFrameCalc  base frame_id = base <> "/" <> frame_id
hframeUrl NodeFrameVisio base frame_id = base <> "/" <> frame_id
James Laver's avatar
James Laver committed
92
hframeUrl _ base frame_id = base <> "/" <> frame_id <> "?view" -- "?both"
93

94 95
frameLayoutView :: R2.Leaf ViewProps
frameLayoutView props  = R.createElement frameLayoutViewCpt props []
96
frameLayoutViewCpt :: R.Component ViewProps
James Laver's avatar
James Laver committed
97
frameLayoutViewCpt = here.component "frameLayoutView" cpt
98
  where
99
    cpt { frame: NodePoly { hyperdata: Hyperdata { base, frame_id }}
100
        , nodeType
101
        , reload } _ = do
102 103 104 105 106 107 108 109 110 111
      case nodeType of
        NodeFrameVisio ->
          case WURL.fromAbsolute base of
            Nothing  -> pure $ H.div {} [ H.text $ "Wrong base url: " <> base ]
            Just url -> pure $ nodeFrameVisio { frame_id, reload, url }
        _              ->
          pure $ H.div{} [
            FV.backButton
            , H.div { className : "frame"
                    , rows: "100%,*" }
112
              [ -- H.script { src: "https://visio.gargantext.org/external_api.js"} [],
113
                H.iframe { src: hframeUrl nodeType base frame_id
114 115 116
                         , width: "100%"
                         , height: "100%"
                         } []
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
              ]
            ]

type NodeFrameVisioProps =
  ( frame_id  :: String
  , reload    :: T2.ReloadS
  , url       :: WURL.URL
  )

nodeFrameVisio :: R2.Leaf NodeFrameVisioProps
nodeFrameVisio props = R.createElement nodeFrameVisioCpt props []
nodeFrameVisioCpt :: R.Component NodeFrameVisioProps
nodeFrameVisioCpt = here.component "nodeFrameVisio" cpt
  where
    cpt { frame_id
        , url } _  = do
      ref <- R.useRef (null :: Nullable DOM.Element)

      R.useEffect' $ do
        here.log2 "[nodeFrameVisio] ref" $ R.readRef ref
137 138 139 140 141 142
        here.log2 "[nodeFrameVisio] JM.api" JM._api
        case toMaybe (R.readRef ref) of
          Nothing -> pure unit
          Just r  -> do
            api <- JM.jitsiMeetAPI (WURL.host url) { parentNode: r, roomName: frame_id }
            here.log2 "[nodeFrameVisio] api" api
143

144
      pure $ H.div { ref } [ H.text $ WURL.host url ]
145

146 147
type LoadProps   = ( nodeId  :: Int
                   , session :: Session )
James Laver's avatar
James Laver committed
148

149 150 151
type ReloadProps = ( nodeId  :: Int
                   , reload :: T2.Reload
                   , session :: Session )
152

153
loadframe' :: Record LoadProps -> Aff (Either RESTError (NodePoly Hyperdata))
James Laver's avatar
James Laver committed
154
loadframe' { nodeId, session } = get session $ NodeAPI Node (Just nodeId) ""
155 156

-- Just to make reloading effective
157
loadframeWithReload :: Record ReloadProps -> Aff (Either RESTError (NodePoly Hyperdata))
James Laver's avatar
James Laver committed
158
loadframeWithReload { nodeId, session } = loadframe' { nodeId, session }