import { useState, useEffect } from "react"; import { Card, CardContent, CardHeader, CardTitle } from "~/components/ui/card"; import { Button } from "~/components/ui"; import { Checkbox } from "~/components/ui/checkbox"; import { Label } from "~/components/ui/label"; import type { OnboardingQuestion, OnboardingAnswer } from "./onboarding-utils"; interface OnboardingQuestionProps { question: OnboardingQuestion; answer?: string | string[]; onAnswer: (answer: OnboardingAnswer) => void; onNext: () => void; onPrevious?: () => void; isFirst: boolean; isLast: boolean; currentStep: number; totalSteps: number; loading?: boolean; } export default function OnboardingQuestionComponent({ question, answer, onAnswer, onNext, onPrevious, isFirst, isLast, currentStep, totalSteps, loading, }: OnboardingQuestionProps) { const [selectedValue, setSelectedValue] = useState( answer || (question.type === "multi-select" ? [] : ""), ); // Sync local state when answer prop changes (e.g., when navigating between steps) useEffect(() => { setSelectedValue(answer || (question.type === "multi-select" ? [] : "")); }, [answer, question.type]); const handleSingleSelect = (value: string) => { setSelectedValue(value); onAnswer({ questionId: question.id, value }); }; const handleMultiSelect = (optionValue: string, checked: boolean) => { const currentValues = Array.isArray(selectedValue) ? selectedValue : []; const newValues = checked ? [...currentValues, optionValue] : currentValues.filter((v) => v !== optionValue); setSelectedValue(newValues); onAnswer({ questionId: question.id, value: newValues }); }; const isValid = () => { if (!question.required) return true; if (question.type === "multi-select") { return Array.isArray(selectedValue) && selectedValue.length > 0; } return selectedValue && selectedValue !== ""; }; return (
Step {currentStep} of {totalSteps}
{question.title}
{question.type === "single-select" && question.options && (
{question.options.map((option) => ( ))}
)} {question.type === "multi-select" && question.options && (
{question.options.map((option) => (
handleMultiSelect(option.value, !!checked) } className="h-6 w-6 text-xl" checkboxClassname="h-5 w-5 text-xl" />
))}
)}
{!isFirst && ( )}
); }