Skip to content

Commit

Permalink
feat: add mechanism for feature flags (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
rangoo94 authored Aug 7, 2023
1 parent 34b371a commit 6fa69b8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions paths.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"@constants/*": ["src/constants/*"],
"@hooks": ["src/hooks/"],
"@hooks/*": ["src/hooks/*"],
"@features": ["src/features/"],
"@features/*": ["src/features/*"],
"@feature-flags": ["src/feature-flags/"],
"@feature-flags/*": ["src/feature-flags/*"],
"@redux": ["src/redux/"],
"@redux/*": ["src/redux/*"],
"@permissions": ["src/permissions/"],
Expand Down
3 changes: 3 additions & 0 deletions src/AppRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import FingerprintJS from '@fingerprintjs/fingerprintjs';
import {ConfigContext, DashboardContext, MainContext} from '@contexts';
import {ModalHandler, ModalOutletProvider} from '@contexts/ModalContext';

import {FeatureFlagsProvider} from '@feature-flags';

import {useAxiosInterceptors} from '@hooks/useAxiosInterceptors';
import {useLastCallback} from '@hooks/useLastCallback';

Expand Down Expand Up @@ -110,6 +112,7 @@ const AppRoot: React.FC = () => {
const plugins: Plugin[] = useMemo(() => [createAiInsightsPlugin()], []);

return composeProviders()
.append(FeatureFlagsProvider, {})
.append(ConfigContext.Provider, {value: config})
.append(DashboardContext.Provider, {value: dashboardValue})
.append(PermissionsProvider, {scope: permissionsScope, resolver: permissionsResolver})
Expand Down
10 changes: 10 additions & 0 deletions src/feature-flags/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {FC, PropsWithChildren, createContext, useState} from 'react';
import {useEvent} from 'react-use';

export const FeatureFlagsContext = createContext<Record<string, boolean>>({});

export const FeatureFlagsProvider: FC<PropsWithChildren<{}>> = ({children}) => {
const [flags, setFlags] = useState({});
useEvent('feature-flags', ({data}) => setFlags({...flags, ...data}));
return <FeatureFlagsContext.Provider value={flags}>{children}</FeatureFlagsContext.Provider>;
};
8 changes: 8 additions & 0 deletions src/feature-flags/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {useContext} from 'react';

import {FeatureFlagsContext} from './context';

export const useFeatureFlag = (name: string): boolean => {
const flags = useContext(FeatureFlagsContext);
return Boolean(flags[name]);
};
2 changes: 2 additions & 0 deletions src/feature-flags/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './hooks';
export {FeatureFlagsProvider} from './context';

0 comments on commit 6fa69b8

Please sign in to comment.