Branchwell

AI Resource Pro / website · airesourcepro.com / src / app / roadmap / start / ai-survey-steps.tsx

1"use client";
2
3import { useState } from "react";
4import Link from "next/link";
5import { ArrowLeft, ArrowRight, Lock } from "lucide-react";
6import { accountRequest } from "@/lib/account-client";
7import {
8 AI_YEARS,
9 CODING,
10 DAILY_HOURS,
11 DEPTHS,
12 ROLES,
13 TOOLS,
14 USES,
15 parseSurvey,
16 type AiSurvey,
17} from "@/lib/ai-survey";
18import type { Option } from "@/lib/onboarding";
19import { OptionButton } from "./option-button";
20
21type ManyKey = "uses" | "tools";
22type OneKey = "role" | "dailyHours" | "aiYears" | "depth" | "coding";
23
24const STEPS: { key: ManyKey | OneKey; title: string; note: string; options: Option<string>[]; multi: boolean }[] = [
25 { key: "role", title: "What best describes you?", note: "Pick the closest fit.", options: ROLES, multi: false },
26 { key: "uses", title: "What do you use AI for?", note: "Pick everything that applies.", options: USES, multi: true },
27 { key: "tools", title: "Which AI tools do you use?", note: "Pick everything you've used in the last month.", options: TOOLS, multi: true },
28 { key: "dailyHours", title: "How much do you use AI on a typical day?", note: "A rough guess is fine.", options: DAILY_HOURS, multi: false },
29 { key: "aiYears", title: "How long have you been using AI tools?", note: "Since your first real use.", options: AI_YEARS, multi: false },
30 { key: "depth", title: "What's the most advanced thing you've done with AI?", note: "Pick the deepest one.", options: DEPTHS, multi: false },
31 { key: "coding", title: "How much coding experience do you have?", note: "Last one.", options: CODING, multi: false },
32];
33
34// The optional questions about how someone uses AI, saved to their account. Every step can be skipped.
35export function AiSurveySteps({ initial, onDone }: { initial: AiSurvey | null; onDone: () => void }) {
36 const [answers, setAnswers] = useState<AiSurvey>(initial ?? { uses: [], tools: [] });
37 const [step, setStep] = useState(0);
38 const [saving, setSaving] = useState(false);
39 const [error, setError] = useState<string | null>(null);
40
41 const current = STEPS[step];
42 const last = step === STEPS.length - 1;
43
44 const finish = async (final: AiSurvey) => {
45 const survey = parseSurvey(final);
46 // Nothing answered and nothing saved before: no row to write
47 if (!survey && !initial) return onDone();
48 setSaving(true);
49 const { error: saveError } = await accountRequest<AiSurvey | null>("survey", { survey });
50 setSaving(false);
51 if (saveError) return setError(`Couldn't save your answers: ${saveError}`);
52 onDone();
53 };
54
55 const advance = (updated: AiSurvey) => {
56 if (last) void finish(updated);
57 else setStep(step + 1);
58 };
59
60 const pick = (key: OneKey, id: string) => {
61 const updated = { ...answers, [key]: id };
62 setAnswers(updated);
63 setTimeout(() => advance(updated), 220);
64 };
65
66 const toggle = (key: ManyKey, id: string) => {
67 const list = answers[key];
68 setAnswers({ ...answers, [key]: list.includes(id) ? list.filter((x) => x !== id) : [...list, id] });
69 };
70
71 const hasAnswer = current.multi ? answers[current.key as ManyKey].length > 0 : !!answers[current.key as OneKey];
72 const long = current.options.length > 10;
73
74 return (
75 <div className="mx-auto w-full max-w-[760px]">
76 <div className="flex items-center justify-between gap-4">
77 <p className="label">
78 <i />
79 About your AI use · optional
80 </p>
81 <span className="font-mono text-[12px] uppercase tracking-[0.08em] text-[var(--ash)]">
82 {step + 1} / {STEPS.length}
83 </span>
84 </div>
85 <div className="mt-5 h-px w-full bg-border" aria-hidden>
86 <div className="h-px bg-primary transition-[width] duration-500" style={{ width: `${((step + 1) / STEPS.length) * 100}%` }} />
87 </div>
88
89 {step === 0 && (
90 <p className="mt-8 flex items-start gap-2.5 text-[15px] leading-relaxed text-[var(--mist)]">
91 <Lock className="mt-1 h-4 w-4 shrink-0 text-primary" aria-hidden />
92 <span>
93 Seven quick questions so we know who we&apos;re making tutorials for. Your answers stay between you and AI Resource Pro.
94 We never sell or share them.{" "}
95 <Link href="/privacy#sharing" className="text-link">
96 Privacy policy
97 </Link>
98 </span>
99 </p>
100 )}
101
102 <div key={current.key} className="animate-in fade-in slide-in-from-bottom-2 duration-500">
103 <h1 className="display-title mt-10 text-[clamp(32px,4.4vw,56px)]">{current.title}</h1>
104 <p className="mt-4 text-[17px] text-[var(--mist)]">{current.note}</p>
105 <div
106 role={current.multi ? "group" : "radiogroup"}
107 aria-label={current.title}
108 className={long ? "mt-8 grid grid-cols-2 gap-2.5 sm:grid-cols-3" : "mt-9 grid gap-3 sm:grid-cols-2"}
109 >
110 {current.options.map((o) =>
111 current.multi ? (
112 <OptionButton
113 key={o.id}
114 option={o}
115 multi
116 compact={long}
117 selected={answers[current.key as ManyKey].includes(o.id)}
118 onClick={() => toggle(current.key as ManyKey, o.id)}
119 />
120 ) : (
121 <OptionButton
122 key={o.id}
123 option={o}
124 multi={false}
125 selected={answers[current.key as OneKey] === o.id}
126 onClick={() => pick(current.key as OneKey, o.id)}
127 />
128 )
129 )}
130 </div>
131 {current.key === "uses" && answers.uses.includes("other") && (
132 <label className="mt-5 block">
133 <span className="text-[15px] text-[var(--mist)]">What else do you use it for?</span>
134 <input
135 type="text"
136 maxLength={200}
137 value={answers.usesOther ?? ""}
138 onChange={(e) => setAnswers({ ...answers, usesOther: e.target.value })}
139 className="mt-2 w-full rounded-xl border border-border bg-transparent px-4 py-3 text-[16px] text-foreground outline-none focus:border-primary"
140 placeholder="For example, materials science"
141 />
142 </label>
143 )}
144 </div>
145
146 {error && <p className="mt-6 rounded-xl border border-destructive/40 p-4 text-sm">{error}</p>}
147
148 <div className="mt-10 flex flex-wrap items-center justify-between gap-4">
149 <div className="flex items-center gap-6">
150 {step > 0 && (
151 <button type="button" onClick={() => setStep(step - 1)} className="ghost-link inline-flex items-center gap-2">
152 <ArrowLeft className="h-4 w-4" /> Back
153 </button>
154 )}
155 <button type="button" onClick={() => void finish(answers)} disabled={saving} className="ghost-link text-[var(--ash)]">
156 {step === 0 ? "Skip these" : "Skip the rest"}
157 </button>
158 </div>
159 <button type="button" onClick={() => advance(answers)} disabled={saving} className="pill disabled:opacity-40">
160 {saving ? "Saving…" : last ? "Finish" : hasAnswer ? "Continue" : "Skip question"}
161 <ArrowRight className="h-4 w-4" />
162 </button>
163 </div>
164 </div>
165 );
166}