"use client"; import { useCallback, useEffect, useState } from "react"; import { SKIP_ONBOARDING_COOKIE, parseProfile, type LearningProfile } from "@/lib/onboarding"; import { accountRequest } from "@/lib/account-client"; import { useUser } from "@/lib/use-user"; // Visitors who aren't signed in keep their answers and progress in this browser until they sign in, // then both move to their account. const LOCAL_PROFILE = "airp-learning-profile"; const LOCAL_DONE = "airp-roadmap-done"; function readLocal(key: string): T | null { try { const raw = window.localStorage.getItem(key); return raw ? (JSON.parse(raw) as T) : null; } catch { return null; } } function writeLocal(key: string, value: unknown) { try { if (value === null) window.localStorage.removeItem(key); else window.localStorage.setItem(key, JSON.stringify(value)); } catch { // Private mode or storage blocked: answers last for this visit only } } export function skipOnboarding() { document.cookie = `${SKIP_ONBOARDING_COOKIE}=1; Max-Age=${60 * 60 * 24 * 365}; Path=/; SameSite=Lax`; } // The user's onboarding answers, from their account when signed in or this browser when not. // `profile` is undefined while loading and null when they haven't answered yet. export function useLearningProfile() { const { user, loading: userLoading } = useUser(); const [profile, setProfile] = useState(undefined); useEffect(() => { if (userLoading) return; const local = parseProfile(readLocal(LOCAL_PROFILE)); if (!user?.id) { setProfile(local); return; } let active = true; accountRequest("profile") .then(async ({ data: saved, error }) => { if (!active) return; if (error) { console.error("Failed to load learning profile:", error); return setProfile(local); } if (saved || !local) return setProfile(saved); // Answered before signing in: move those answers to the account const { error: saveError } = await accountRequest("profile", { profile: local }); if (!active) return; if (saveError) console.error("Failed to save learning profile:", saveError); else writeLocal(LOCAL_PROFILE, null); setProfile(local); }); return () => { active = false; }; }, [user?.id, userLoading]); const save = useCallback( async (next: LearningProfile): Promise => { if (!user?.id) { writeLocal(LOCAL_PROFILE, next); setProfile(next); return null; } const { data, error } = await accountRequest("profile", { profile: next }); if (error) return `Couldn't save your answers: ${error}`; setProfile(data); return null; }, [user?.id] ); return { user, profile, save }; } // Tutorials the user has marked done on their roadmap (keys like "claude/claude-md-guide") export function useRoadmapProgress() { const { user, loading: userLoading } = useUser(); const [done, setDone] = useState | null>(null); useEffect(() => { if (userLoading) return; const local = readLocal(LOCAL_DONE) ?? []; if (!user?.id) { setDone(new Set(local)); return; } let active = true; accountRequest("progress") .then(async ({ data, error }) => { if (!active) return; if (error) console.error("Failed to load roadmap progress:", error); const keys = new Set(data ?? []); const toMove = error ? [] : local.filter((k) => !keys.has(k)); let moved = !error; if (toMove.length) { const { error: moveError } = await accountRequest("progress", { keys: toMove, completed: true }); if (!active) return; if (moveError) { console.error("Failed to save roadmap progress:", moveError); moved = false; } else toMove.forEach((k) => keys.add(k)); } if (moved) writeLocal(LOCAL_DONE, null); setDone(keys); }); return () => { active = false; }; }, [user?.id, userLoading]); const toggle = useCallback( async (key: string) => { if (!done) return; const next = new Set(done); const marking = !next.has(key); if (marking) next.add(key); else next.delete(key); setDone(next); if (!user?.id) return writeLocal(LOCAL_DONE, [...next]); const { error } = await accountRequest("progress", { keys: [key], completed: marking }); if (error) { console.error("Failed to update roadmap progress:", error); setDone(done); } }, [done, user?.id] ); return { done, toggle }; }