/** A question the agent asked the person, in the style of the approval card: options to pick, the person's own words, one answer or a skip (#4244). */ import { useState } from 'react'; import { tr } from '../lib/i18n'; import { Btn, Icon } from './ui'; import { answerAgentAsk } from '../lib/store'; import { plainFailureText } from '../lib/plain-errors'; import { answerComplete, toAnswers, type AgentAsk, type AgentQuestion } from '../lib/agent-ask'; type Draft = Record; export function QuestionCard({ sessionId, ask }: { sessionId: string; ask: AgentAsk }) { const [draft, setDraft] = useState({}); const [busy, setBusy] = useState(false), [problem, setProblem] = useState(''); const current = (q: AgentQuestion) => draft[q.id] ?? { selected: [], custom: '' }; const pick = (q: AgentQuestion, label: string) => setDraft(d => { const cur = d[q.id] ?? { selected: [], custom: '' }; const selected = q.multiSelect ? (cur.selected.includes(label) ? cur.selected.filter(x => x !== label) : [...cur.selected, label]) : [label]; // One choice for a single-select question: picking an option replaces words typed instead. return { ...d, [q.id]: { selected, custom: q.multiSelect ? cur.custom : '' } }; }); const type = (q: AgentQuestion, custom: string) => setDraft(d => { const cur = d[q.id] ?? { selected: [], custom: '' }; return { ...d, [q.id]: { selected: q.multiSelect || !custom.trim() ? cur.selected : [], custom } }; }); async function send(skip: boolean) { if (busy) return; setBusy(true); setProblem(''); try { await answerAgentAsk(sessionId, ask.callId, skip ? null : toAnswers(ask.questions, draft)); } catch (e) { setProblem(plainFailureText(e instanceof Error ? e.message : String(e)).message); setBusy(false); } } return
{ask.questions.map(q => { const cur = current(q); return
{q.header ?

{q.header}

: null}

{q.question}

{q.detail ?

{q.detail}

: null}
{q.options.length ?
{q.options.map(o => { const on = cur.selected.includes(o.label); return ; })}
: null} type(q, e.target.value)}/>
; })} {problem ?

{problem}

: null}
void send(true)}>{tr("Skip")} void send(false)}>{tr("Send answer")}
; }