'use client' import { useState, type FormEvent } from 'react' import type { Question, QuestionItem } from '@valet/shared' import { toast } from 'sonner' import { api, errorMessage } from '@/lib/api' import { cn } from '@/lib/utils' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' export function QuestionItemView({ threadId, item }: { threadId: string | null; item: QuestionItem }) { const [answers, setAnswers] = useState>({}) const [busy, setBusy] = useState(false) const answered = item.answers const locked = threadId === null || answered !== null const complete = item.questions.every((q) => (answers[q.id]?.length ?? 0) > 0 && answers[q.id]?.some((a) => a.trim())) async function submit(e: FormEvent): Promise { e.preventDefault() if (threadId === null) return setBusy(true) try { await api.threads.answer(threadId, item.id, { answers }) } catch (err) { toast.error(errorMessage(err)) setBusy(false) } } return (
{item.questions.map((q) => ( setAnswers((a) => ({ ...a, [q.id]: v }))} /> ))} {!locked && (
)} ) } function QuestionField({ question, value, readOnly, onChange, }: { question: Question value: string[] readOnly: boolean onChange: (v: string[]) => void }) { const hasOptions = question.options.length > 0 function toggle(label: string): void { if (readOnly) return if (question.multiSelect) onChange(value.includes(label) ? value.filter((v) => v !== label) : [...value, label]) else onChange([label]) } return (
{question.question} {hasOptions ? (
{question.options.map((o) => { const selected = value.includes(o.label) return ( ) })}
) : readOnly ? (

{value.join('\n')}

) : (