Branchwell

AI Resource Pro / website · airesourcepro.com / src / lib / onboarding.ts

Raw 4.2 KB on 6c7ce85c3441baadd17890ab9cc9bf8ed5112901
1// The onboarding questions and the profile they produce. Client-safe: no filesystem or database access here.
2import { TRACKS, type TrackSlug } from "@/lib/tracks";
3import type { Level } from "@/lib/tutorial-types";
4
5// Set when someone skips the questions after signing in, so later sign-ins stop sending them there (read in auth/callback)
6export const SKIP_ONBOARDING_COOKIE = "airp-onboarding-skipped";
7
8export type GoalId = "code" | "everyday" | "automate" | "video" | "own-gpu" | "current";
9export type WeeklyTime = "light" | "steady" | "deep";
10export type Format = "video" | "written" | "both";
11
12// What a user told us during onboarding. Stored in Supabase (learning_profiles) once they sign in.
13export type LearningProfile = {
14 goals: GoalId[];
15 tracks: TrackSlug[];
16 level: Level;
17 weeklyTime: WeeklyTime;
18 format: Format;
19 updatedAt?: string;
20};
21
22export type Option<T extends string> = { id: T; label: string; hint?: string };
23
24export const GOALS: (Option<GoalId> & { tracks: TrackSlug[] })[] = [
25 { id: "code", label: "Ship code with an AI agent", hint: "Claude Code, Codex", tracks: ["claude", "chatgpt"] },
26 { id: "everyday", label: "Get better answers from Claude and ChatGPT", hint: "Prompting, projects", tracks: ["claude", "chatgpt"] },
27 { id: "automate", label: "Hand browser and desktop chores to AI", hint: "Computer use", tracks: ["computer-use"] },
28 { id: "video", label: "Make AI video", hint: "ComfyUI, open models", tracks: ["ai-video"] },
29 { id: "own-gpu", label: "Run open models on my own GPU", hint: "Runpod", tracks: ["ai-video"] },
30 { id: "current", label: "Keep up with what's new", hint: "A bit of everything", tracks: TRACKS.map((t) => t.slug) },
31];
32
33export const LEVELS: Option<Level>[] = [
34 { id: "Beginner", label: "Beginner", hint: "I've chatted with AI, but not much more" },
35 { id: "Intermediate", label: "Intermediate", hint: "I use AI tools most weeks" },
36 { id: "Advanced", label: "Advanced", hint: "I build with agents, APIs or my own models" },
37];
38
39export const WEEKLY_TIMES: (Option<WeeklyTime> & { minutes: number })[] = [
40 { id: "light", label: "An hour or so a week", minutes: 60 },
41 { id: "steady", label: "A few hours a week", minutes: 180 },
42 { id: "deep", label: "Most days", minutes: 420 },
43];
44
45export const FORMATS: Option<Format>[] = [
46 { id: "video", label: "Videos", hint: "Watch it done first" },
47 { id: "written", label: "Written guides", hint: "Read and follow along" },
48 { id: "both", label: "Both", hint: "Whatever fits the topic" },
49];
50
51// Tracks implied by the chosen goals, in site order
52export function tracksForGoals(goals: GoalId[]): TrackSlug[] {
53 const wanted = new Set(GOALS.filter((g) => goals.includes(g.id)).flatMap((g) => g.tracks));
54 return TRACKS.map((t) => t.slug).filter((slug) => wanted.has(slug));
55}
56
57const isIn = <T extends string>(options: Option<T>[], value: unknown): value is T =>
58 options.some((o) => o.id === value);
59
60// Checks a profile read from storage or the database; returns null if anything is missing or unknown
61export function parseProfile(raw: unknown): LearningProfile | null {
62 if (!raw || typeof raw !== "object") return null;
63 const r = raw as Record<string, unknown>;
64 const goals = Array.isArray(r.goals) ? r.goals.filter((g): g is GoalId => isIn(GOALS, g)) : [];
65 const trackSlugs = TRACKS.map((t) => t.slug) as string[];
66 const tracks = Array.isArray(r.tracks) ? r.tracks.filter((t): t is TrackSlug => trackSlugs.includes(t)) : [];
67 const weeklyTime = r.weeklyTime ?? r.weekly_time;
68 const updatedAt = r.updatedAt ?? r.updated_at;
69 if (goals.length === 0 || !isIn(LEVELS, r.level) || !isIn(WEEKLY_TIMES, weeklyTime) || !isIn(FORMATS, r.format)) {
70 return null;
71 }
72 return {
73 goals,
74 tracks: tracks.length ? tracks : tracksForGoals(goals),
75 level: r.level,
76 weeklyTime,
77 format: r.format,
78 updatedAt: typeof updatedAt === "string" ? updatedAt : undefined,
79 };
80}
81
82// A database row for learning_profiles
83export function profileRow(userId: string, p: LearningProfile) {
84 return {
85 user_id: userId,
86 goals: p.goals,
87 tracks: p.tracks,
88 level: p.level,
89 weekly_time: p.weeklyTime,
90 format: p.format,
91 updated_at: new Date().toISOString(),
92 };
93}
94
95export const labelFor = <T extends string>(options: Option<T>[], id: T) => options.find((o) => o.id === id)?.label ?? id;