import { useState } from 'react'; import { MessageCircleQuestion, Check } from 'lucide-react'; import type { ChatMessage } from './types'; type ToolMessage = Extract; type QuestionOption = { label: string; description: string; }; type Question = { question: string; header: string; multiple: boolean; options: QuestionOption[]; }; type QuestionActivityProps = { message: ToolMessage; onAnswer: (text: string) => void; }; export const QuestionActivity = ({ message, onAnswer }: QuestionActivityProps) => { const [selectedOptions, setSelectedOptions] = useState>(new Set()); const [otherText, setOtherText] = useState(''); const [answered, setAnswered] = useState(false); const [answeredText, setAnsweredText] = useState(''); const input = message.toolInput as { questions?: Question[] }; const questions = input.questions; if (!questions || questions.length === 0) return null; const pending = message.output === undefined; const handleSelect = (question: Question, label: string) => { if (answered || !pending) return; if (question.multiple) { setSelectedOptions((prev) => { const next = new Set(prev); if (next.has(label)) next.delete(label); else next.add(label); return next; }); } else { const text = label; setAnswered(true); setAnsweredText(text); onAnswer(text); } }; const handleSubmitMultiple = () => { if (selectedOptions.size === 0 || answered || !pending) return; const text = Array.from(selectedOptions).join(', '); setAnswered(true); setAnsweredText(text); onAnswer(text); }; const handleSubmitOther = () => { const text = otherText.trim(); if (!text || answered || !pending) return; setAnswered(true); setAnsweredText(text); onAnswer(text); }; const isDisabled = answered || !pending; return (
{questions.map((q, qi) => (
{q.header}

{q.question}

{q.options.map((opt) => { const isSelected = answered ? answeredText === opt.label || answeredText.split(', ').includes(opt.label) : selectedOptions.has(opt.label); return ( ); })}
{/* "Other" free-text option */} {!isDisabled && (
setOtherText(ev.target.value)} onKeyDown={(ev) => { if (ev.key === 'Enter') { ev.preventDefault(); handleSubmitOther(); } }} placeholder="Other..." className="flex-1 px-3 py-1.5 rounded-lg border border-duck-dark/15 text-sm text-duck-dark placeholder:text-duck-dark/30 focus:outline-none focus:border-duck-teal/40" />
)} {/* Submit button for multi-select */} {q.multiple && !isDisabled && ( )} {/* Answered indicator */} {isDisabled && answeredText && (
Answered: {answeredText}
)}
))}
); };