Router.purs 593 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
module Gargantext.Hooks.Router (useHashRouter) where

import Prelude (($), bind, discard, const, pure)
import Data.Tuple.Nested ((/\))
import Reactix as R
import Routing.Match (Match)
import Routing.Hash (matches)

-- | Ties the hash router to a state hook of routes
-- | Note: if it gets sent to an unrecognised url, it will quietly drop the change
useHashRouter :: forall routes. Match routes -> routes -> R.Hooks (R.State routes)
useHashRouter routes init = do
  route@(_ /\ setRoute) <- R.useState' init
  R.useEffectOnce $ matches routes $ \_old new -> setRoute (const new)
  pure route