-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2e525c
commit f72fa5d
Showing
3 changed files
with
90 additions
and
29 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
82 changes: 82 additions & 0 deletions
82
apps/nextjs/src/components/ContextProviders/FeatureFlagProvider.tsx
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,82 @@ | ||
"use client"; | ||
|
||
import type { ReactNode } from "react"; | ||
import { | ||
useMemo, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
useRef, | ||
} from "react"; | ||
|
||
import { aiLogger } from "@oakai/logger"; | ||
|
||
import useAnalytics from "@/lib/analytics/useAnalytics"; | ||
|
||
const log = aiLogger("feature-flags"); | ||
|
||
export interface FeatureFlagContextProps { | ||
bootstrappedFeatures: Record<string, string | boolean>; | ||
} | ||
|
||
const FeatureFlagContext = createContext<FeatureFlagContextProps>({ | ||
bootstrappedFeatures: {}, | ||
}); | ||
|
||
export interface FeatureFlagProviderProps { | ||
children: ReactNode; | ||
bootstrappedFeatures: Record<string, string | boolean>; | ||
} | ||
|
||
export const FeatureFlagProvider = ({ | ||
children, | ||
bootstrappedFeatures, | ||
}: FeatureFlagProviderProps) => { | ||
const value = useMemo( | ||
() => ({ bootstrappedFeatures }), | ||
[bootstrappedFeatures], | ||
); | ||
|
||
return ( | ||
<FeatureFlagContext.Provider value={value}> | ||
{children} | ||
</FeatureFlagContext.Provider> | ||
); | ||
}; | ||
|
||
export const useClientSideFeatureFlag = (flag: string) => { | ||
const context = useContext(FeatureFlagContext); | ||
|
||
const { posthogAiBetaClient: posthog } = useAnalytics(); | ||
const hasLogged = useRef(false); | ||
|
||
const [posthogFeatureFlag, setPosthogFeatureFlag] = useState< | ||
boolean | string | undefined | ||
>(); | ||
|
||
const bootstrappedFlag = context.bootstrappedFeatures[flag]; | ||
|
||
useEffect(() => { | ||
return posthog.onFeatureFlags(() => { | ||
const updatedValue = posthog.isFeatureEnabled(flag); | ||
if (updatedValue !== bootstrappedFlag) { | ||
log.info(`Updating ${flag} to ${updatedValue}`); | ||
setPosthogFeatureFlag(updatedValue); | ||
} | ||
}); | ||
}, [posthog, flag, bootstrappedFlag]); | ||
|
||
const isDebug = process.env.NEXT_PUBLIC_POSTHOG_DEBUG === "true"; | ||
if (isDebug) { | ||
if (!hasLogged.current) { | ||
hasLogged.current = true; | ||
log.info(`Feature flag ${flag} is enabled in debug mode`); | ||
} | ||
return true; | ||
} | ||
|
||
// NOTE: This will flash from the bootstrapped value to the posthog value | ||
// only on page load within 1 minute of toggling a flag | ||
return posthogFeatureFlag ?? bootstrappedFlag ?? false; | ||
}; |
This file was deleted.
Oops, something went wrong.