Version.purs 1.65 KB
Newer Older
arturo's avatar
arturo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
module Gargantext.Hooks.Version
  ( useVersion
  , Version
  ) where

import Gargantext.Prelude

import DOM.Simple.Console (log2)
import Data.Either (Either(..))
import Data.Maybe (Maybe(..))
import Data.Tuple.Nested ((/\))
import Effect (Effect)
import Effect.Aff (launchAff_)
import Effect.Class (liftEffect)
import Gargantext.Config.REST as REST
import Gargantext.Ends (toUrl)
import Gargantext.Hooks.FirstEffect (useFirstEffect')
import Gargantext.Sessions (Session(..))
import Gargantext.Utils.Reactix as R2
import Reactix as R
import Toestand as T

-- | (ie. Frontend Version)
foreign import version :: Version

type Version = String


type Input   = Maybe R_Input
type R_Input =
  { session :: Session
  }

type Output   = Maybe R_Output
type R_Output =
  { clientVersion   :: String
  , remoteVersion   :: String
  }

-- | Conditional Hooks checking release version match between client and remove
useVersion :: Input -> R.Hooks (Output)
useVersion mInput = do
  -- States
  mOutput /\ mOutputBox <- R2.useBox' (Nothing :: Output)
  -- Methods
  let
    exec :: Session -> Effect Unit
    exec session
        = launchAff_ $ getBackendVersion session
      >>= case _ of
            Left err -> liftEffect $ log2 "[version] error" err
            Right v  -> liftEffect $ flip T.write_ mOutputBox $ Just
              { clientVersion: version
              , remoteVersion: v
              }
  -- Hooks
  useFirstEffect' $ case mInput of
    Nothing          -> R.nothing
    Just { session } -> exec session
  -- Output
  pure mOutput

getBackendVersion :: Session -> REST.AffRESTError Version
getBackendVersion (Session { backend }) = REST.get Nothing (toUrl backend "version")