"use client"; import { useEffect, useState } from "react"; import { useRouter, useSearchParams } from "next/navigation"; import { ArrowLeft, ArrowRight } from "lucide-react"; import { accountRequest } from "@/lib/account-client"; import type { AiSurvey } from "@/lib/ai-survey"; import { FORMATS, GOALS, LEVELS, WEEKLY_TIMES, tracksForGoals, type Format, type GoalId, type LearningProfile, type Option, type WeeklyTime, } from "@/lib/onboarding"; import { TRACKS, type TrackSlug } from "@/lib/tracks"; import type { Level } from "@/lib/tutorial-types"; import { skipOnboarding, useLearningProfile } from "@/lib/use-learning-profile"; import { AiSurveySteps } from "./ai-survey-steps"; import { OptionButton } from "./option-button"; type Draft = { goals: GoalId[]; tracks: TrackSlug[]; level?: Level; weeklyTime?: WeeklyTime; format?: Format; }; const TRACK_OPTIONS: Option[] = TRACKS.map((t) => ({ id: t.slug, label: t.name, hint: t.tagline })); const STEPS = [ { key: "goals", title: "What do you want to get out of AI?", note: "Pick as many as you like." }, { key: "tracks", title: "Which tools should we cover?", note: "We've ticked the ones that match. Change them if you like." }, { key: "level", title: "Where are you with AI right now?", note: "Your roadmap starts at this level." }, { key: "weeklyTime", title: "How much time can you give it?", note: "Used to pace your roadmap." }, { key: "format", title: "How do you like to learn?", note: "Videos or written guides go first." }, ] as const; export function Questionnaire() { const router = useRouter(); const params = useSearchParams(); const next = params.get("next") || "/roadmap"; const welcome = params.get("welcome") === "1"; const { user, profile, save } = useLearningProfile(); const [draft, setDraft] = useState(null); const [step, setStep] = useState(0); const [error, setError] = useState(null); const [saving, setSaving] = useState(false); // Signed-in people who haven't answered the optional AI-use questions get them after the roadmap ones; undefined until loaded const [survey, setSurvey] = useState(undefined); const [surveyStep, setSurveyStep] = useState(false); useEffect(() => { if (!user?.id) return; let active = true; accountRequest("survey").then(({ data, error }) => { if (!active) return; if (error) console.error("Failed to load AI survey:", error); setSurvey(error ? undefined : data); }); return () => { active = false; }; }, [user?.id]); // Start from earlier answers when retaking; after sign-in, people who already answered go straight on useEffect(() => { if (profile === undefined || draft) return; if (welcome && profile) { router.replace(next); return; } setDraft(profile ? { ...profile } : { goals: [], tracks: [] }); }, [profile, draft, welcome, next, router]); if (!draft) return
; if (surveyStep) return router.push(next)} />; const current = STEPS[step]; const set = (patch: Partial) => setDraft({ ...draft, ...patch }); const toggle = (list: T[], id: T) => (list.includes(id) ? list.filter((x) => x !== id) : [...list, id]); const finish = async (final: Draft) => { if (!final.level || !final.weeklyTime || !final.format) return; const answers: LearningProfile = { goals: final.goals, tracks: final.tracks.length ? final.tracks : tracksForGoals(final.goals), level: final.level, weeklyTime: final.weeklyTime, format: final.format, }; setSaving(true); const failed = await save(answers); setSaving(false); if (failed) return setError(failed); if (survey === null) { window.scrollTo({ top: 0 }); setSurveyStep(true); } else router.push(next); }; // Single-choice questions move on as soon as something is picked const pick = (patch: Partial) => { const updated = { ...draft, ...patch }; setDraft(updated); if (step === STEPS.length - 1) void finish(updated); else setTimeout(() => setStep((s) => s + 1), 220); }; const canContinue = current.key === "goals" ? draft.goals.length > 0 : current.key === "tracks" ? draft.tracks.length > 0 : !!draft[current.key]; const goNext = () => { if (current.key === "goals" && step === 0) { // Pre-tick the tools that match the goals, keeping any the user already chose const suggested = tracksForGoals(draft.goals); set({ tracks: draft.tracks.length && profile ? draft.tracks : suggested }); } if (step === STEPS.length - 1) void finish(draft); else setStep(step + 1); }; let options: React.ReactNode; if (current.key === "goals") { options = GOALS.map((o) => ( set({ goals: toggle(draft.goals, o.id) })} /> )); } else if (current.key === "tracks") { options = TRACK_OPTIONS.map((o) => ( set({ tracks: toggle(draft.tracks, o.id) })} /> )); } else { const list = { level: LEVELS, weeklyTime: WEEKLY_TIMES, format: FORMATS }[current.key] as Option[]; options = list.map((o) => ( pick({ [current.key]: o.id })} /> )); } const multi = current.key === "goals" || current.key === "tracks"; return (

{profile ? "Recalibrate your roadmap" : "Build your roadmap"}

{step + 1} / {STEPS.length}

{current.title}

{current.note}

{options}
{error &&

{error}

}
{step > 0 ? ( ) : welcome && user ? ( ) : ( )} {(multi || draft[current.key as "level"]) && ( )}
); }