Commit 7d8ac564 authored by Alexandre Delanoë's avatar Alexandre Delanoë

fix merge

parents 59e5c3fe 514d1318
......@@ -40,6 +40,7 @@
"http-proxy-middleware": "^3.0.3",
"immer": "~9.0.5",
"isomorphic-unfetch": "~3.1.0",
"js-cookie": "^3.0.5",
"jszip": "^3.10.1",
"markdown-it": "~13.0.1",
"minify": "^11.3.0",
......@@ -7815,6 +7816,14 @@
"license": "MIT",
"peer": true
},
"node_modules/js-cookie": {
"version": "3.0.5",
"resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz",
"integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==",
"engines": {
"node": ">=14"
}
},
"node_modules/js-tokens": {
"version": "4.0.0",
"license": "MIT"
......
'use strict';
import { useEffect, useState } from 'react';
import Cookies from 'js-cookie';
export const _useFeatureFlag = function useFeatureFlag(cookieKeys) {
const checkFeatureFlag = () =>
cookieKeys.some((key) => Cookies.get(key) === 'true');
const [isFeatureEnabled, setIsFeatureEnabled] = useState(checkFeatureFlag);
useEffect(() => {
const hasEnabledFlag = checkFeatureFlag();
isFeatureEnabled && console.error("Warning: feature flags are in use");
if (isFeatureEnabled !== hasEnabledFlag) {
setIsFeatureEnabled(hasEnabledFlag);
}
}, [cookieKeys, isFeatureEnabled]);
return isFeatureEnabled;
};
module Gargantext.Hooks.UseFeatureFlag where
import Prelude
import Effect.Uncurried (runEffectFn1, EffectFn1)
import Gargantext.Components.CodeEditor (previewPostProcess)
import Gargantext.Utils.Reactix as R2
import Reactix (hooksComponent)
import Reactix as R
import Reactix.DOM.HTML as H
foreign import _useFeatureFlag :: EffectFn1 (Array String) Boolean
here :: R2.Here
here = R2.here "Gargantext.Hooks.UseFeatureFlag"
{-
useFeatureFlag is a custom React hook that determines whether a feature flag is enabled based on the presence and value of specific cookies.
It checks the cookies provided as keys and returns true if any of them are set to 'true'. Otherwise, it returns false.
This hook is useful for conditional rendering of features based on user-specific feature toggles managed via cookies.
Usage :
parentCpt :: R.Component ()
parentCpt = here.component "container" cpt
where
cpt _ _ = do
shouldBeVisible <- useFeatureFlag ["keyOne", "keyTwo", "keyThree"]
pure $
H.div {} [if shouldBeVisible then H.text "I should be visible" else H.text "Im not visible"]
-}
useFeatureFlag :: Array String -> R.Hooks Boolean
useFeatureFlag keys =
R.unsafeHooksEffect $ runEffectFn1 _useFeatureFlag keys
type FeatureProps =
( render :: Boolean -> R.Element
, keys :: Array String
)
{-
Use feature component two wrap your component behind a feature flag.
It might be cleaner to use it instead of directly using the useFeatureFlag hook
Usage:
feature { keys: [ "keyOne" ], render: \shouldRender -> if shouldRender then H.text "hello" else H.text "you cant see me" }
-}
feature :: R2.Leaf FeatureProps
feature = R2.renderLeaf "" $
( \{ keys, render } _ -> do
shouldRender <- useFeatureFlag keys
pure $
render shouldRender
)
hideBehind :: forall a. Array String -> R2.Component a -> R2.Component a
hideBehind keys cpt = R2.renderComponent "" \props children -> do
shouldRender <- useFeatureFlag keys
pure $
if shouldRender then cpt props children
else H.text ""
......@@ -72,6 +72,14 @@ component
-> R.Element
component cpt props children = R.createElement cpt props children
renderComponent :: forall a. String -> R.HooksComponent a -> (Record a -> Array R.Element -> R.Element)
renderComponent name cpt props children =
R.createElement (R.hooksComponent name cpt) props children
renderLeaf :: forall a. String -> R.HooksComponent a -> Leaf a
renderLeaf name cpt props =
renderComponent name cpt props []
leaf
:: forall cpt p
. R.IsComponent cpt p (Array R.Element)
......
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