-
Notifications
You must be signed in to change notification settings - Fork 579
Expand file tree
/
Copy pathAppInitializer.tsx
More file actions
55 lines (45 loc) · 1.82 KB
/
AppInitializer.tsx
File metadata and controls
55 lines (45 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use client";
import React, { useEffect, useState } from "react";
import { usePathname } from "next/navigation";
import { OnboardingWizard } from "./onboarding/OnboardingWizard";
import { useAuth } from "@/contexts/AuthContext";
import { useUserStore } from "@/lib/userStore";
const LOCAL_STORAGE_KEY = "kagent-onboarding";
export function AppInitializer({ children }: { children: React.ReactNode }) {
/** `null` = not read yet (must match server + first client paint to avoid hydration mismatch) */
const [isOnboarding, setIsOnboarding] = useState<boolean | null>(null);
const pathname = usePathname();
const { user, userIdClaim } = useAuth();
const setUserId = useUserStore((s) => s.setUserId);
useEffect(() => {
const identity = user?.[userIdClaim] as string | undefined;
if (identity) {
setUserId(identity);
}
}, [user, userIdClaim, setUserId]);
useEffect(() => {
const hasOnboarded = localStorage.getItem(LOCAL_STORAGE_KEY) === "true";
// Defer so this isn’t a synchronous setState in the effect body (react-hooks/set-state-in-effect).
const id = requestAnimationFrame(() => {
setIsOnboarding(!hasOnboarded);
});
return () => cancelAnimationFrame(id);
}, []);
const handleOnboardingComplete = () => {
localStorage.setItem(LOCAL_STORAGE_KEY, 'true');
setIsOnboarding(false);
};
const handleSkipWizard = () => {
localStorage.setItem(LOCAL_STORAGE_KEY, 'true');
setIsOnboarding(false);
// You might want to show a toast here as well, depending on your UI library setup
};
if (isOnboarding === null) {
return null;
}
// Don't show the wizard on the login page
if (isOnboarding && pathname !== '/login') {
return <OnboardingWizard onOnboardingComplete={handleOnboardingComplete} onSkip={handleSkipWizard} />;
}
return <>{children}</>;
}