Commit a243ea35 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Fix link sharing routing (garg issue #375)

This commit fixes the routing for share URL when the user is connected
to more than one instance.
parent afc63092
Pipeline #6477 passed with stages
in 23 minutes and 54 seconds
...@@ -6,6 +6,7 @@ import Data.Tuple.Nested ((/\)) ...@@ -6,6 +6,7 @@ import Data.Tuple.Nested ((/\))
import Gargantext.AsyncTasks as GAT import Gargantext.AsyncTasks as GAT
import Gargantext.Components.App.Store as AppStore import Gargantext.Components.App.Store as AppStore
import Gargantext.Components.Router (router) import Gargantext.Components.Router (router)
import Gargantext.Config as Config
import Gargantext.Hooks (useHashRouter) import Gargantext.Hooks (useHashRouter)
import Gargantext.Hooks.FirstEffect (useFirstEffect') import Gargantext.Hooks.FirstEffect (useFirstEffect')
import Gargantext.Router as Router import Gargantext.Router as Router
...@@ -99,5 +100,13 @@ mainAppCpt = here.component "main" cpt where ...@@ -99,5 +100,13 @@ mainAppCpt = here.component "main" cpt where
R.useEffectOnce' $ do R.useEffectOnce' $ do
R2.loadLocalStorageState R2.openNodesKey boxes.forestOpen R2.loadLocalStorageState R2.openNodesKey boxes.forestOpen
T.listen (R2.listenLocalStorageState R2.openNodesKey) boxes.forestOpen T.listen (R2.listenLocalStorageState R2.openNodesKey) boxes.forestOpen
-- Store in the box the current backend, as
-- derived from the href, before the window gets
-- injected with the router.
R.useEffectOnce' $ do
mLoc <- Config.matchCurrentLocation
T.write_ mLoc boxes.backend
useHashRouter Router.router boxes.route -- Install router to window useHashRouter Router.router boxes.route -- Install router to window
pure $ router { boxes } -- Render router component pure $ router { boxes } -- Render router component
...@@ -12,6 +12,7 @@ import Data.Array as A ...@@ -12,6 +12,7 @@ import Data.Array as A
import Data.Foldable (intercalate) import Data.Foldable (intercalate)
import Data.Map as M import Data.Map as M
import Data.Maybe (Maybe(..), fromMaybe, maybe) import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.String (contains, Pattern(..))
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Data.UUID (UUID) import Data.UUID (UUID)
import Data.UUID as UUID import Data.UUID as UUID
...@@ -39,15 +40,15 @@ import Gargantext.Components.Nodes.Texts as Texts ...@@ -39,15 +40,15 @@ import Gargantext.Components.Nodes.Texts as Texts
import Gargantext.Components.Tile (tileBlock) import Gargantext.Components.Tile (tileBlock)
import Gargantext.Components.TopBar as TopBar import Gargantext.Components.TopBar as TopBar
import Gargantext.Components.TreeSearch (treeSearch) import Gargantext.Components.TreeSearch (treeSearch)
import Gargantext.Config (defaultFrontends, defaultBackends) import Gargantext.Config (defaultFrontends, defaultBackends, matchCurrentLocation)
import Gargantext.Context.Session as SessionContext import Gargantext.Context.Session as SessionContext
import Gargantext.Ends (Backend) import Gargantext.Ends (Backend(..))
import Gargantext.Hooks.LinkHandler (useLinkHandler) import Gargantext.Hooks.LinkHandler (useLinkHandler)
import Gargantext.Hooks.Resize (ResizeType(..), useResizeHandler) import Gargantext.Hooks.Resize (ResizeType(..), useResizeHandler)
import Gargantext.Hooks.Session (useSession) import Gargantext.Hooks.Session (useSession)
import Gargantext.Routes (AppRoute(..), Tile) import Gargantext.Routes (AppRoute(..), Tile)
import Gargantext.Routes as GR import Gargantext.Routes as GR
import Gargantext.Sessions (Session, sessionId, unSessions) import Gargantext.Sessions (Session, sessionId, unSessions, sessionUrl)
import Gargantext.Sessions as Sessions import Gargantext.Sessions as Sessions
import Gargantext.Types (CorpusId, Handed(..), ListId, NodeID, NodeType(..), SessionId, SidePanelState(..), ID) import Gargantext.Types (CorpusId, Handed(..), ListId, NodeID, NodeType(..), SessionId, SidePanelState(..), ID)
import Gargantext.Utils ((?)) import Gargantext.Utils ((?))
...@@ -779,19 +780,25 @@ type ShareProps = (nodeType :: String, nodeId :: ID | Props) ...@@ -779,19 +780,25 @@ type ShareProps = (nodeType :: String, nodeId :: ID | Props)
share :: R2.Component ShareProps share :: R2.Component ShareProps
share = R.createElement shareCpt share = R.createElement shareCpt
-- | Ensures that we rewrite the Share URL into something that points
-- to the correct user in the correct session, see Gargantext#357.
findSuitableSession :: Maybe Backend -> Session -> Boolean
findSuitableSession Nothing _ = false
findSuitableSession (Just (Backend be)) s = (Pattern $ be.baseUrl) `contains` sessionUrl s ""
shareCpt :: R.Component ShareProps shareCpt :: R.Component ShareProps
shareCpt = here.component "share" cpt where shareCpt = here.component "share" cpt where
cpt { nodeType, nodeId, boxes} _ = do cpt { nodeType, nodeId, boxes} _ = do
{ goToRoute } <- useLinkHandler { goToRoute } <- useLinkHandler
sessions' <- T.useLive T.unequal boxes.sessions sessions' <- T.useLive T.unequal boxes.sessions
backend <- T.useLive T.unequal boxes.backend
case unSessions sessions' of case unSessions sessions' of
[] -> do [] -> do
R.useEffect' $ T.write_ true boxes.showLogin R.useEffect' $ T.write_ true boxes.showLogin
R.useEffect' $ T.write_ (Just (Tuple nodeType nodeId)) boxes.loginRedirect R.useEffect' $ T.write_ (Just (Tuple nodeType nodeId)) boxes.loginRedirect
R.useEffect' $ goToRoute Login R.useEffect' $ goToRoute Login
pure $ H.text "no session" pure $ H.text "no session"
s -> case head s of s -> case A.findIndex (findSuitableSession backend) s >>= A.index s of
Just s' -> do Just s' -> do
R.useEffect' $ goToRoute $ fromMaybe Login $ GR.nodeTypeAppRoute (fromMaybe Node $ read nodeType) (sessionId s') nodeId R.useEffect' $ goToRoute $ fromMaybe Login $ GR.nodeTypeAppRoute (fromMaybe Node $ read nodeType) (sessionId s') nodeId
pure $ H.text $ "session route: " <> (show $ fromMaybe Login $ GR.nodeTypeAppRoute (fromMaybe Node $ read nodeType) (sessionId s') nodeId) pure $ H.text $ "session route: " <> (show $ fromMaybe Login $ GR.nodeTypeAppRoute (fromMaybe Node $ read nodeType) (sessionId s') nodeId)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment