okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
3.6 KiB47 linesraw
1/** 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). */
2import { useState } from 'react';
3import { tr } from '../lib/i18n';
4import { Btn, Icon } from './ui';
5import { answerAgentAsk } from '../lib/store';
6import { plainFailureText } from '../lib/plain-errors';
7import { answerComplete, toAnswers, type AgentAsk, type AgentQuestion } from '../lib/agent-ask';
8
9type Draft = Record<string, { selected: string[]; custom: string }>;
10
11export function QuestionCard({ sessionId, ask }: { sessionId: string; ask: AgentAsk }) {
12    const [draft, setDraft] = useState<Draft>({});
13    const [busy, setBusy] = useState(false), [problem, setProblem] = useState('');
14    const current = (q: AgentQuestion) => draft[q.id] ?? { selected: [], custom: '' };
15    const pick = (q: AgentQuestion, label: string) => setDraft(d => {
16        const cur = d[q.id] ?? { selected: [], custom: '' };
17        const selected = q.multiSelect ? (cur.selected.includes(label) ? cur.selected.filter(x => x !== label) : [...cur.selected, label]) : [label];
18        // One choice for a single-select question: picking an option replaces words typed instead.
19        return { ...d, [q.id]: { selected, custom: q.multiSelect ? cur.custom : '' } };
20    });
21    const type = (q: AgentQuestion, custom: string) => setDraft(d => {
22        const cur = d[q.id] ?? { selected: [], custom: '' };
23        return { ...d, [q.id]: { selected: q.multiSelect || !custom.trim() ? cur.selected : [], custom } };
24    });
25    async function send(skip: boolean) {
26        if (busy) return;
27        setBusy(true); setProblem('');
28        try { await answerAgentAsk(sessionId, ask.callId, skip ? null : toAnswers(ask.questions, draft)); }
29        catch (e) { setProblem(plainFailureText(e instanceof Error ? e.message : String(e)).message); setBusy(false); }
30    }
31    return <section className="askcard askq" role="group" aria-label={tr("The assistant has a question for you")}>
32      {ask.questions.map(q => {
33        const cur = current(q);
34        return <div className="askq__item" key={q.id}>
35          <div className="askcard__top"><span className="askcard__icon"><Icon name="chat" size={18}/></span>
36            <div className="askcard__text">{q.header ? <p className="xs muted">{q.header}</p> : null}<p className="askcard__q">{q.question}</p>{q.detail ? <p className="small muted askq__detail">{q.detail}</p> : null}</div></div>
37          {q.options.length ? <div className="askq__opts" role={q.multiSelect ? 'group' : 'radiogroup'} aria-label={q.question}>{q.options.map(o => {
38            const on = cur.selected.includes(o.label);
39            return <button type="button" key={o.label} role={q.multiSelect ? 'checkbox' : 'radio'} aria-checked={on} className="askq__opt" data-on={on || undefined} disabled={busy} onClick={() => pick(q, o.label)}><span>{o.label}</span>{o.description ? <small className="muted">{o.description}</small> : null}</button>;
40          })}</div> : null}
41          <input className="st-input askq__own" aria-label={tr("Type your own answer")} placeholder={tr("Type your own answer")} value={cur.custom} maxLength={2000} disabled={busy} onChange={e => type(q, e.target.value)}/>
42        </div>;
43      })}
44      {problem ? <p className="small inline-error askq__problem" role="alert">{problem}</p> : null}
45      <div className="askq__acts"><Btn size="sm" disabled={busy} onClick={() => void send(true)}>{tr("Skip")}</Btn><Btn variant="primary" size="sm" disabled={busy || !answerComplete(ask.questions, draft)} onClick={() => void send(false)}>{tr("Send answer")}</Btn></div>
46    </section>;
47}