Created
May 6, 2020 12:33
-
-
Save sibelius/f281f16a184a632a68bbb24ba15ded3c to your computer and use it in GitHub Desktop.
Revisions
-
sibelius created this gist
May 6, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ import React, { useContext, useCallback } from 'react'; export const FeatureFlagContext = React.createContext<string[]>([]); export const useFeatureFlag = () => { const features = useContext<string[]>(FeatureFlagContext); const hasFeature = useCallback( (feature: string) => { return features.includes(feature); }, [features] ); return { hasFeature }; }; type Props = { feature: string; children: React.ReactNode; fallbackComponent?: React.ReactNode; }; export const FeatureFlag = ({ feature, fallbackComponent = null, children }: Props) => { const { hasFeature } = useFeatureFlag(); if (!hasFeature(feature)) { return fallbackComponent; } return children; }; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ export const AppFeatureFlags = { TEMP: 'TEMP' // special feature flag that should wrap all WIP code // TODO - add more features as you need }; export const getFeatureFlags = () => { if (process.env.NODE_ENV === 'development') { return [AppFeatureFlags.TEMP]; } return []; }; const Providers = ({ children }) => { const features = useMemo(() => getFeatureFlags(), []); return ( <FeatureFlagContext.Provider value={features}> {children} </FeatureFlagContext.Provider> ) }