/** * Question Options Component * * Renders structured questions from AskUserQuestion tool. * Shows clickable option buttons. */ import { useState } from 'react' import { Check } from 'lucide-react' import type { SpecQuestion } from '../lib/types' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Card, CardContent } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' interface QuestionOptionsProps { questions: SpecQuestion[] onSubmit: (answers: Record) => void disabled?: boolean } export function QuestionOptions({ questions, onSubmit, disabled = false, }: QuestionOptionsProps) { // Track selected answers for each question const [answers, setAnswers] = useState>({}) const [customInputs, setCustomInputs] = useState>({}) const [showCustomInput, setShowCustomInput] = useState>({}) const handleOptionClick = (questionIdx: number, optionLabel: string, multiSelect: boolean) => { const key = String(questionIdx) if (optionLabel === 'Other') { setShowCustomInput((prev) => ({ ...prev, [key]: true })) return } setShowCustomInput((prev) => ({ ...prev, [key]: false })) setAnswers((prev) => { if (multiSelect) { const current = (prev[key] as string[]) || [] if (current.includes(optionLabel)) { return { ...prev, [key]: current.filter((o) => o !== optionLabel) } } else { return { ...prev, [key]: [...current, optionLabel] } } } else { return { ...prev, [key]: optionLabel } } }) } const handleCustomInputChange = (questionIdx: number, value: string) => { const key = String(questionIdx) setCustomInputs((prev) => ({ ...prev, [key]: value })) setAnswers((prev) => ({ ...prev, [key]: value })) } const handleSubmit = () => { // Ensure all questions have answers const finalAnswers: Record = {} questions.forEach((_, idx) => { const key = String(idx) if (showCustomInput[key] && customInputs[key]) { finalAnswers[key] = customInputs[key] } else if (answers[key]) { finalAnswers[key] = answers[key] } }) onSubmit(finalAnswers) } const isOptionSelected = (questionIdx: number, optionLabel: string, multiSelect: boolean) => { const key = String(questionIdx) const answer = answers[key] if (multiSelect) { return Array.isArray(answer) && answer.includes(optionLabel) } return answer === optionLabel } const hasAnswer = (questionIdx: number) => { const key = String(questionIdx) return !!(answers[key] || (showCustomInput[key] && customInputs[key])) } const allQuestionsAnswered = questions.every((_, idx) => hasAnswer(idx)) return (
{questions.map((q, questionIdx) => ( {/* Question header */}
{q.header} {q.question} {q.multiSelect && ( (select multiple) )}
{/* Options grid */}
{q.options.map((opt, optIdx) => { const isSelected = isOptionSelected(questionIdx, opt.label, q.multiSelect) return ( ) })} {/* "Other" option */}
{/* Custom input field */} {showCustomInput[String(questionIdx)] && (
handleCustomInputChange(questionIdx, e.target.value)} placeholder="Type your answer..." autoFocus disabled={disabled} />
)}
))} {/* Submit button */}
) }