Become a Pro / website · becomeapro.ai / src / app / roadmap / start / questionnaire.tsx
| 1 | "use client"; |
| 2 | |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import { useRouter, useSearchParams } from "next/navigation"; |
| 5 | import { ArrowLeft, ArrowRight } from "lucide-react"; |
| 6 | import { accountRequest } from "@/lib/account-client"; |
| 7 | import type { AiSurvey } from "@/lib/ai-survey"; |
| 8 | import { |
| 9 | FORMATS, |
| 10 | GOALS, |
| 11 | LEVELS, |
| 12 | WEEKLY_TIMES, |
| 13 | tracksForGoals, |
| 14 | type Format, |
| 15 | type GoalId, |
| 16 | type LearningProfile, |
| 17 | type Option, |
| 18 | type WeeklyTime, |
| 19 | } from "@/lib/onboarding"; |
| 20 | import { TRACKS, type TrackSlug } from "@/lib/tracks"; |
| 21 | import type { Level } from "@/lib/tutorial-types"; |
| 22 | import { skipOnboarding, useLearningProfile } from "@/lib/use-learning-profile"; |
| 23 | import { AiSurveySteps } from "./ai-survey-steps"; |
| 24 | import { OptionButton } from "./option-button"; |
| 25 | |
| 26 | type Draft = { |
| 27 | goals: GoalId[]; |
| 28 | tracks: TrackSlug[]; |
| 29 | level?: Level; |
| 30 | weeklyTime?: WeeklyTime; |
| 31 | format?: Format; |
| 32 | }; |
| 33 | |
| 34 | const TRACK_OPTIONS: Option<TrackSlug>[] = TRACKS.map((t) => ({ id: t.slug, label: t.name, hint: t.tagline })); |
| 35 | |
| 36 | const STEPS = [ |
| 37 | { key: "goals", title: "What do you want to get out of AI?", note: "Pick as many as you like." }, |
| 38 | { key: "tracks", title: "Which tools should we cover?", note: "We've ticked the ones that match. Change them if you like." }, |
| 39 | { key: "level", title: "Where are you with AI right now?", note: "Your roadmap starts at this level." }, |
| 40 | { key: "weeklyTime", title: "How much time can you give it?", note: "Used to pace your roadmap." }, |
| 41 | { key: "format", title: "How do you like to learn?", note: "Videos or written guides go first." }, |
| 42 | ] as const; |
| 43 | |
| 44 | export function Questionnaire() { |
| 45 | const router = useRouter(); |
| 46 | const params = useSearchParams(); |
| 47 | const next = params.get("next") || "/roadmap"; |
| 48 | const welcome = params.get("welcome") === "1"; |
| 49 | const { user, profile, save } = useLearningProfile(); |
| 50 | const [draft, setDraft] = useState<Draft | null>(null); |
| 51 | const [step, setStep] = useState(0); |
| 52 | const [error, setError] = useState<string | null>(null); |
| 53 | const [saving, setSaving] = useState(false); |
| 54 | // Signed-in people who haven't answered the optional AI-use questions get them after the roadmap ones; undefined until loaded |
| 55 | const [survey, setSurvey] = useState<AiSurvey | null | undefined>(undefined); |
| 56 | const [surveyStep, setSurveyStep] = useState(false); |
| 57 | |
| 58 | useEffect(() => { |
| 59 | if (!user?.id) return; |
| 60 | let active = true; |
| 61 | accountRequest<AiSurvey | null>("survey").then(({ data, error }) => { |
| 62 | if (!active) return; |
| 63 | if (error) console.error("Failed to load AI survey:", error); |
| 64 | setSurvey(error ? undefined : data); |
| 65 | }); |
| 66 | return () => { active = false; }; |
| 67 | }, [user?.id]); |
| 68 | |
| 69 | // Start from earlier answers when retaking; after sign-in, people who already answered go straight on |
| 70 | useEffect(() => { |
| 71 | if (profile === undefined || draft) return; |
| 72 | if (welcome && profile) { |
| 73 | router.replace(next); |
| 74 | return; |
| 75 | } |
| 76 | setDraft(profile ? { ...profile } : { goals: [], tracks: [] }); |
| 77 | }, [profile, draft, welcome, next, router]); |
| 78 | |
| 79 | if (!draft) return <div className="min-h-[60vh]" />; |
| 80 | if (surveyStep) return <AiSurveySteps initial={survey ?? null} onDone={() => router.push(next)} />; |
| 81 | |
| 82 | const current = STEPS[step]; |
| 83 | const set = (patch: Partial<Draft>) => setDraft({ ...draft, ...patch }); |
| 84 | const toggle = <T extends string>(list: T[], id: T) => (list.includes(id) ? list.filter((x) => x !== id) : [...list, id]); |
| 85 | |
| 86 | const finish = async (final: Draft) => { |
| 87 | if (!final.level || !final.weeklyTime || !final.format) return; |
| 88 | const answers: LearningProfile = { |
| 89 | goals: final.goals, |
| 90 | tracks: final.tracks.length ? final.tracks : tracksForGoals(final.goals), |
| 91 | level: final.level, |
| 92 | weeklyTime: final.weeklyTime, |
| 93 | format: final.format, |
| 94 | }; |
| 95 | setSaving(true); |
| 96 | const failed = await save(answers); |
| 97 | setSaving(false); |
| 98 | if (failed) return setError(failed); |
| 99 | if (survey === null) { |
| 100 | window.scrollTo({ top: 0 }); |
| 101 | setSurveyStep(true); |
| 102 | } else router.push(next); |
| 103 | }; |
| 104 | |
| 105 | // Single-choice questions move on as soon as something is picked |
| 106 | const pick = (patch: Partial<Draft>) => { |
| 107 | const updated = { ...draft, ...patch }; |
| 108 | setDraft(updated); |
| 109 | if (step === STEPS.length - 1) void finish(updated); |
| 110 | else setTimeout(() => setStep((s) => s + 1), 220); |
| 111 | }; |
| 112 | |
| 113 | const canContinue = |
| 114 | current.key === "goals" ? draft.goals.length > 0 : current.key === "tracks" ? draft.tracks.length > 0 : !!draft[current.key]; |
| 115 | |
| 116 | const goNext = () => { |
| 117 | if (current.key === "goals" && step === 0) { |
| 118 | // Pre-tick the tools that match the goals, keeping any the user already chose |
| 119 | const suggested = tracksForGoals(draft.goals); |
| 120 | set({ tracks: draft.tracks.length && profile ? draft.tracks : suggested }); |
| 121 | } |
| 122 | if (step === STEPS.length - 1) void finish(draft); |
| 123 | else setStep(step + 1); |
| 124 | }; |
| 125 | |
| 126 | let options: React.ReactNode; |
| 127 | if (current.key === "goals") { |
| 128 | options = GOALS.map((o) => ( |
| 129 | <OptionButton key={o.id} option={o} multi selected={draft.goals.includes(o.id)} onClick={() => set({ goals: toggle(draft.goals, o.id) })} /> |
| 130 | )); |
| 131 | } else if (current.key === "tracks") { |
| 132 | options = TRACK_OPTIONS.map((o) => ( |
| 133 | <OptionButton key={o.id} option={o} multi selected={draft.tracks.includes(o.id)} onClick={() => set({ tracks: toggle(draft.tracks, o.id) })} /> |
| 134 | )); |
| 135 | } else { |
| 136 | const list = { level: LEVELS, weeklyTime: WEEKLY_TIMES, format: FORMATS }[current.key] as Option<string>[]; |
| 137 | options = list.map((o) => ( |
| 138 | <OptionButton key={o.id} option={o} multi={false} selected={draft[current.key] === o.id} onClick={() => pick({ [current.key]: o.id })} /> |
| 139 | )); |
| 140 | } |
| 141 | |
| 142 | const multi = current.key === "goals" || current.key === "tracks"; |
| 143 | |
| 144 | return ( |
| 145 | <div className="mx-auto w-full max-w-[760px]"> |
| 146 | <div className="flex items-center justify-between gap-4"> |
| 147 | <p className="label"> |
| 148 | <i /> |
| 149 | {profile ? "Recalibrate your roadmap" : "Build your roadmap"} |
| 150 | </p> |
| 151 | <span className="font-mono text-[12px] uppercase tracking-[0.08em] text-[var(--ash)]"> |
| 152 | {step + 1} / {STEPS.length} |
| 153 | </span> |
| 154 | </div> |
| 155 | <div className="mt-5 h-px w-full bg-border" aria-hidden> |
| 156 | <div className="h-px bg-primary transition-[width] duration-500" style={{ width: `${((step + 1) / STEPS.length) * 100}%` }} /> |
| 157 | </div> |
| 158 | |
| 159 | <div key={current.key} className="animate-in fade-in slide-in-from-bottom-2 duration-500"> |
| 160 | <h1 className="display-title mt-10 text-[clamp(36px,5vw,64px)]">{current.title}</h1> |
| 161 | <p className="mt-4 text-[17px] text-[var(--mist)]">{current.note}</p> |
| 162 | <div role={multi ? "group" : "radiogroup"} aria-label={current.title} className="mt-9 grid gap-3 sm:grid-cols-2"> |
| 163 | {options} |
| 164 | </div> |
| 165 | </div> |
| 166 | |
| 167 | {error && <p className="mt-6 rounded-xl border border-destructive/40 p-4 text-sm">{error}</p>} |
| 168 | |
| 169 | <div className="mt-10 flex flex-wrap items-center justify-between gap-4"> |
| 170 | {step > 0 ? ( |
| 171 | <button type="button" onClick={() => setStep(step - 1)} className="ghost-link inline-flex items-center gap-2"> |
| 172 | <ArrowLeft className="h-4 w-4" /> Back |
| 173 | </button> |
| 174 | ) : welcome && user ? ( |
| 175 | <button |
| 176 | type="button" |
| 177 | onClick={() => { |
| 178 | skipOnboarding(); |
| 179 | router.replace(next); |
| 180 | }} |
| 181 | className="ghost-link text-[var(--ash)]" |
| 182 | > |
| 183 | Skip for now |
| 184 | </button> |
| 185 | ) : ( |
| 186 | <span /> |
| 187 | )} |
| 188 | {(multi || draft[current.key as "level"]) && ( |
| 189 | <button type="button" onClick={goNext} disabled={!canContinue || saving} className="pill disabled:opacity-40"> |
| 190 | {saving ? "Saving…" : step === STEPS.length - 1 ? "See my roadmap" : "Continue"} |
| 191 | <ArrowRight className="h-4 w-4" /> |
| 192 | </button> |
| 193 | )} |
| 194 | </div> |
| 195 | </div> |
| 196 | ); |
| 197 | } |