"use client"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { PixelAvatar } from "@/components/avatar/pixel-avatar"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { createClient } from "@/lib/supabase/client"; import { routes } from "@/lib/routes"; const questions = [ { id: "focus", prompt: "What habit do you most want to build first?", options: ["Sleep", "Movement", "Hydration", "Mindfulness"], }, { id: "pet-name", prompt: "Pick a starter vibe for your pet.", options: ["Calm", "Energetic", "Curious", "Cozy"], }, ]; export default function OnboardingQuizPage() { const router = useRouter(); const [step, setStep] = useState(0); const [answers, setAnswers] = useState>({}); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const currentQuestion = questions[step]; const isLastStep = step === questions.length - 1; const handleSelect = (value: string) => { setAnswers((current) => ({ ...current, [currentQuestion.id]: value, })); }; const handleContinue = async () => { if (!answers[currentQuestion.id]) { return; } if (!isLastStep) { setStep((current) => current + 1); return; } setIsSubmitting(true); setError(null); try { const supabase = createClient(); const { error: updateError } = await supabase.auth.updateUser({ data: { onboarding_completed: true, onboarding_answers: answers, }, }); if (updateError) { throw updateError; } router.push(routes.avatar); router.refresh(); } catch (submitError) { setError( submitError instanceof Error ? submitError.message : "Could not save onboarding answers.", ); } finally { setIsSubmitting(false); } }; return (

Step {step + 1} of {questions.length}

Welcome to Habit Pet

{currentQuestion.prompt} This helps personalize your pet and starting habits. {currentQuestion.options.map((option) => { const selected = answers[currentQuestion.id] === option; return ( ); })} {error ?

{error}

: null}
); }