Session.purs 757 Bytes
Newer Older
arturo's avatar
arturo committed
1 2 3 4
module Gargantext.Hooks.Session
  ( useSession
  ) where

arturo's avatar
arturo committed
5 6 7
import Gargantext.Prelude

import Data.Maybe (Maybe(..))
arturo's avatar
arturo committed
8 9
import Gargantext.Context.Session as SessionContext
import Gargantext.Sessions (Session)
arturo's avatar
arturo committed
10 11
import Gargantext.Utils.Reactix as R2
import Partial.Unsafe (unsafeCrashWith)
arturo's avatar
arturo committed
12 13
import Reactix as R

arturo's avatar
arturo committed
14 15 16
here :: R2.Here
here = R2.here "Gargantext.Hooks.Session"

arturo's avatar
arturo committed
17
useSession :: R.Hooks (Session)
arturo's avatar
arturo committed
18 19 20 21 22 23 24 25
useSession = R.useContext SessionContext.context >>= case _ of
  Nothing -> do
    R.unsafeHooksEffect $ here.error $
      "Trying to access to current `Session` but no one could " <>
      "be found. It could be an issue where a component uses this hook " <>
      "without a prior `R.provideContext`"
    unsafeCrashWith "no session found"
  Just s  -> pure s