-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dashboard): add feature flags provider and hook (#7133)
- Loading branch information
Showing
5 changed files
with
724 additions
and
735 deletions.
There are no files selected for viewing
This file contains 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 characters
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { lazy, Suspense } from 'react'; | ||
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk'; | ||
import { LAUNCH_DARKLY_CLIENT_SIDE_ID } from '@/config'; | ||
|
||
const LD_CONFIG = { | ||
clientSideID: LAUNCH_DARKLY_CLIENT_SIDE_ID, | ||
reactOptions: { | ||
useCamelCaseFlagKeys: false, | ||
}, | ||
context: { | ||
kind: 'user', | ||
anonymous: true, | ||
}, | ||
options: { | ||
bootstrap: 'localStorage', | ||
}, | ||
} as const; | ||
|
||
const AsyncFeatureFlagsProvider = lazy(async () => { | ||
if (!LAUNCH_DARKLY_CLIENT_SIDE_ID) { | ||
return { | ||
default: ({ children }: { children: React.ReactNode }) => <>{children}</>, | ||
}; | ||
} | ||
|
||
const LaunchDarklyProvider = await asyncWithLDProvider(LD_CONFIG); | ||
return { | ||
default: ({ children }: { children: React.ReactNode }) => <LaunchDarklyProvider>{children}</LaunchDarklyProvider>, | ||
}; | ||
}); | ||
|
||
export function FeatureFlagsProvider({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<Suspense> | ||
<AsyncFeatureFlagsProvider>{children}</AsyncFeatureFlagsProvider> | ||
</Suspense> | ||
); | ||
} |
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useFlags } from 'launchdarkly-react-client-sdk'; | ||
import { FeatureFlagsKeysEnum, prepareBooleanStringFeatureFlag } from '@novu/shared'; | ||
import { LAUNCH_DARKLY_CLIENT_SIDE_ID } from '../config'; | ||
|
||
function isLaunchDarklyEnabled() { | ||
return !!LAUNCH_DARKLY_CLIENT_SIDE_ID; | ||
} | ||
|
||
export const useFeatureFlag = (key: FeatureFlagsKeysEnum, defaultValue = false): boolean => { | ||
const flags = useFlags(); | ||
|
||
if (!isLaunchDarklyEnabled()) { | ||
const envValue = | ||
// Check if the feature flag is exported as an environment variable | ||
import.meta.env[`VITE_${key}`] ?? | ||
// Then check process.env if process exists | ||
(typeof process !== 'undefined' ? process?.env?.[key] : undefined); | ||
|
||
return prepareBooleanStringFeatureFlag(envValue, defaultValue); | ||
} | ||
|
||
return flags[key] ?? defaultValue; | ||
}; |
This file contains 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 characters
Oops, something went wrong.