Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add FeatureFlagProvider #353

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion apps/nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import "@/app/theme-config.css";
import { Providers } from "@/components/AppComponents/Chat//providers";
import { AnalyticsProvider } from "@/components/ContextProviders/AnalyticsProvider";
import { CookieConsentProvider } from "@/components/ContextProviders/CookieConsentProvider";
import { FeatureFlagProvider } from "@/components/ContextProviders/FeatureFlagProvider";
import FontProvider from "@/components/ContextProviders/FontProvider";
import { GleapProvider } from "@/components/ContextProviders/GleapProvider";
import { WebDebuggerPosition } from "@/lib/avo/Avo";
Expand Down Expand Up @@ -118,7 +119,13 @@ export default async function RootLayout({
}}
bootstrappedFeatures={bootstrappedFeatures}
>
<GleapProvider>{children}</GleapProvider>
<GleapProvider>
<FeatureFlagProvider
bootstrappedFeatures={bootstrappedFeatures}
>
{children}
</FeatureFlagProvider>
</GleapProvider>
</AnalyticsProvider>
</CookieConsentProvider>
</Providers>
Expand Down
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;
};
28 changes: 0 additions & 28 deletions apps/nextjs/src/utils/useClientSideFeatureFlag.ts

This file was deleted.

Loading