-
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.
Merge branch 'main' into chore-keywords
- Loading branch information
Showing
5 changed files
with
93 additions
and
31 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
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 |
---|---|---|
|
@@ -54,7 +54,8 @@ export function DemoProvider({ children }: Readonly<DemoProviderProps>) { | |
isDemoUser, | ||
appSessionsRemaining, | ||
appSessionsPerMonth: DEMO_APP_SESSIONS_PER_30D, | ||
contactHref: "mailto:[email protected]", | ||
contactHref: | ||
"https://share.hsforms.com/1R9ulYSNPQgqElEHde3KdhAbvumd", | ||
isSharingEnabled, | ||
} | ||
: { | ||
|
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.