Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

refactor(frontend): improve LaunchDarkly provider initialization (#10422)

Nicholas Tindleae6ef8c0c2dd
diff --git a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx
index d47026b50d..ad12b0e131 100644
--- a/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx
+++ b/autogpt_platform/frontend/src/services/feature-flags/feature-flag-provider.tsx
@@ -1,7 +1,8 @@
"use client";
import { LDProvider } from "launchdarkly-react-client-sdk";
-import { ReactNode } from "react";
+import type { ReactNode } from "react";
+import { useMemo } from "react";
import { useSupabase } from "@/lib/supabase/hooks/useSupabase";
import { BehaveAs, getBehaveAs } from "@/lib/utils";
@@ -9,33 +10,59 @@ const clientId = process.env.NEXT_PUBLIC_LAUNCHDARKLY_CLIENT_ID;
const envEnabled = process.env.NEXT_PUBLIC_LAUNCHDARKLY_ENABLED === "true";
export function LaunchDarklyProvider({ children }: { children: ReactNode }) {
- const { user } = useSupabase();
+ const { user, isUserLoading } = useSupabase();
const isCloud = getBehaveAs() === BehaveAs.CLOUD;
- const enabled = isCloud && envEnabled && clientId && user;
+ const isLaunchDarklyConfigured = isCloud && envEnabled && clientId;
- if (!enabled) return <>{children}</>;
-
- const userContext = user
- ? {
- kind: "user",
- key: user.id,
- email: user.email,
- anonymous: false,
- custom: {
- role: user.role,
- },
- }
- : {
- kind: "user",
+ const context = useMemo(() => {
+ if (isUserLoading || !user) {
+ console.debug("[LaunchDarklyProvider] Using anonymous context", {
+ isUserLoading,
+ hasUser: !!user,
+ });
+ return {
+ kind: "user" as const,
key: "anonymous",
anonymous: true,
};
+ }
+
+ console.debug("[LaunchDarklyProvider] Using authenticated context", {
+ userId: user.id,
+ email: user.email,
+ role: user.role,
+ });
+ return {
+ kind: "user" as const,
+ key: user.id,
+ ...(user.email && { email: user.email }),
+ anonymous: false,
+ custom: {
+ ...(user.role && { role: user.role }),
+ },
+ };
+ }, [user, isUserLoading]);
+
+ if (!isLaunchDarklyConfigured) {
+ console.debug(
+ "[LaunchDarklyProvider] Not configured for this environment",
+ {
+ isCloud,
+ envEnabled,
+ hasClientId: !!clientId,
+ },
+ );
+ return <>{children}</>;
+ }
return (
<LDProvider
+ // Add this key prop. It will be 'anonymous' when logged out,
+ key={context.key}
clientSideID={clientId}
- context={userContext}
+ context={context}
reactOptions={{ useCamelCaseFlagKeys: false }}
+ options={{ bootstrap: "localStorage" }}
>
{children}
</LDProvider>