okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
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 }) { 12const [ draft , setDraft ] = useState < Draft >({}); 13const [ busy , setBusy ] = useState ( false ), [ problem , setProblem ] = useState ( '' ); 14const current = ( q :AgentQuestion ) => draft [ q . id ] ?? { selected :[], custom :'' }; 15const pick = ( q :AgentQuestion , label :string ) => setDraft ( d => { 16const cur = d [ q . id ] ?? { selected :[], custom :'' }; 17const 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. 19return { ...d , [ q . id ] :{ selected, custom :q . multiSelect ?cur . custom :'' } }; 20}); 21const type = ( q :AgentQuestion , custom :string ) => setDraft ( d => { 22const cur = d [ q . id ] ?? { selected :[], custom :'' }; 23return { ...d , [ q . id ] :{ selected :q . multiSelect || ! custom . trim () ?cur . selected :[], custom} }; 24}); 25async function send ( skip :boolean ) { 26if ( busy ) return ; 27setBusy ( true ); setProblem ( '' ); 28try { await answerAgentAsk ( sessionId , ask . callId , skip ?null :toAnswers ( ask . questions , draft )); } 29catch ( e ) { setProblem ( plainFailureText ( e instanceof Error ?e . message :String ( e )). message ); setBusy ( false ); } 30} 31return < section className = "askcard askq" role = "group" aria-label = { tr ( "The assistant has a question for you" )}> 32{ ask . questions . map ( q => { 33const cur = current ( q ); 34return < 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 => { 38const on = cur . selected . includes ( o . label ); 39return < 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}