Become a Pro / website · becomeapro.ai / src / lib / use-learning-profile.ts
| 1 | "use client"; |
| 2 | |
| 3 | import { useCallback, useEffect, useState } from "react"; |
| 4 | import { SKIP_ONBOARDING_COOKIE, parseProfile, type LearningProfile } from "@/lib/onboarding"; |
| 5 | import { accountRequest } from "@/lib/account-client"; |
| 6 | import { useUser } from "@/lib/use-user"; |
| 7 | |
| 8 | // Visitors who aren't signed in keep their answers and progress in this browser until they sign in, |
| 9 | // then both move to their account. |
| 10 | const LOCAL_PROFILE = "airp-learning-profile"; |
| 11 | const LOCAL_DONE = "airp-roadmap-done"; |
| 12 | |
| 13 | function readLocal<T>(key: string): T | null { |
| 14 | try { |
| 15 | const raw = window.localStorage.getItem(key); |
| 16 | return raw ? (JSON.parse(raw) as T) : null; |
| 17 | } catch { |
| 18 | return null; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | function writeLocal(key: string, value: unknown) { |
| 23 | try { |
| 24 | if (value === null) window.localStorage.removeItem(key); |
| 25 | else window.localStorage.setItem(key, JSON.stringify(value)); |
| 26 | } catch { |
| 27 | // Private mode or storage blocked: answers last for this visit only |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export function skipOnboarding() { |
| 32 | document.cookie = `${SKIP_ONBOARDING_COOKIE}=1; Max-Age=${60 * 60 * 24 * 365}; Path=/; SameSite=Lax`; |
| 33 | } |
| 34 | |
| 35 | // The user's onboarding answers, from their account when signed in or this browser when not. |
| 36 | // `profile` is undefined while loading and null when they haven't answered yet. |
| 37 | export function useLearningProfile() { |
| 38 | const { user, loading: userLoading } = useUser(); |
| 39 | const [profile, setProfile] = useState<LearningProfile | null | undefined>(undefined); |
| 40 | |
| 41 | useEffect(() => { |
| 42 | if (userLoading) return; |
| 43 | const local = parseProfile(readLocal(LOCAL_PROFILE)); |
| 44 | if (!user?.id) { |
| 45 | setProfile(local); |
| 46 | return; |
| 47 | } |
| 48 | let active = true; |
| 49 | accountRequest<LearningProfile | null>("profile") |
| 50 | .then(async ({ data: saved, error }) => { |
| 51 | if (!active) return; |
| 52 | if (error) { |
| 53 | console.error("Failed to load learning profile:", error); |
| 54 | return setProfile(local); |
| 55 | } |
| 56 | if (saved || !local) return setProfile(saved); |
| 57 | // Answered before signing in: move those answers to the account |
| 58 | const { error: saveError } = await accountRequest<LearningProfile>("profile", { profile: local }); |
| 59 | if (!active) return; |
| 60 | if (saveError) console.error("Failed to save learning profile:", saveError); |
| 61 | else writeLocal(LOCAL_PROFILE, null); |
| 62 | setProfile(local); |
| 63 | }); |
| 64 | return () => { active = false; }; |
| 65 | }, [user?.id, userLoading]); |
| 66 | |
| 67 | const save = useCallback( |
| 68 | async (next: LearningProfile): Promise<string | null> => { |
| 69 | if (!user?.id) { |
| 70 | writeLocal(LOCAL_PROFILE, next); |
| 71 | setProfile(next); |
| 72 | return null; |
| 73 | } |
| 74 | const { data, error } = await accountRequest<LearningProfile>("profile", { profile: next }); |
| 75 | if (error) return `Couldn't save your answers: ${error}`; |
| 76 | setProfile(data); |
| 77 | return null; |
| 78 | }, |
| 79 | [user?.id] |
| 80 | ); |
| 81 | |
| 82 | return { user, profile, save }; |
| 83 | } |
| 84 | |
| 85 | // Tutorials the user has marked done on their roadmap (keys like "claude/claude-md-guide") |
| 86 | export function useRoadmapProgress() { |
| 87 | const { user, loading: userLoading } = useUser(); |
| 88 | const [done, setDone] = useState<Set<string> | null>(null); |
| 89 | |
| 90 | useEffect(() => { |
| 91 | if (userLoading) return; |
| 92 | const local = readLocal<string[]>(LOCAL_DONE) ?? []; |
| 93 | if (!user?.id) { |
| 94 | setDone(new Set(local)); |
| 95 | return; |
| 96 | } |
| 97 | let active = true; |
| 98 | accountRequest<string[]>("progress") |
| 99 | .then(async ({ data, error }) => { |
| 100 | if (!active) return; |
| 101 | if (error) console.error("Failed to load roadmap progress:", error); |
| 102 | const keys = new Set(data ?? []); |
| 103 | const toMove = error ? [] : local.filter((k) => !keys.has(k)); |
| 104 | let moved = !error; |
| 105 | if (toMove.length) { |
| 106 | const { error: moveError } = await accountRequest<null>("progress", { keys: toMove, completed: true }); |
| 107 | if (!active) return; |
| 108 | if (moveError) { |
| 109 | console.error("Failed to save roadmap progress:", moveError); |
| 110 | moved = false; |
| 111 | } else toMove.forEach((k) => keys.add(k)); |
| 112 | } |
| 113 | if (moved) writeLocal(LOCAL_DONE, null); |
| 114 | setDone(keys); |
| 115 | }); |
| 116 | return () => { active = false; }; |
| 117 | }, [user?.id, userLoading]); |
| 118 | |
| 119 | const toggle = useCallback( |
| 120 | async (key: string) => { |
| 121 | if (!done) return; |
| 122 | const next = new Set(done); |
| 123 | const marking = !next.has(key); |
| 124 | if (marking) next.add(key); |
| 125 | else next.delete(key); |
| 126 | setDone(next); |
| 127 | if (!user?.id) return writeLocal(LOCAL_DONE, [...next]); |
| 128 | const { error } = await accountRequest<null>("progress", { keys: [key], completed: marking }); |
| 129 | if (error) { |
| 130 | console.error("Failed to update roadmap progress:", error); |
| 131 | setDone(done); |
| 132 | } |
| 133 | }, |
| 134 | [done, user?.id] |
| 135 | ); |
| 136 | |
| 137 | return { done, toggle }; |
| 138 | } |