okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3d9c21509
main
1import { useEffect , useState } from 'react' ; 2import { tr } from '../lib/i18n' ; 3import { goalAction , useGoal , type Goal } from '../lib/goal' ; 4import { pushToast , resumeGoal , useStore } from '../lib/store' ; 5import { plainFailureText } from '../lib/plain-errors' ; 6import { Btn , Dialog , Icon , IconBtn } from './ui' ; 7 8/** The words after the objective: where the goal is, in one short phrase. */ 9export function goalStatusText ( goal :Goal ) :string { 10switch ( goal . phase ) { 11case 'paused' :return tr ( 'Paused' ); 12case 'blocked' :return goal . blocked || tr ( 'Needs you' ); 13case 'complete' :return tr ( 'Done' ); 14default :return tr ( 'Working' ); // no "step 1 of 256": the host's round count is not a measure of progress 15} 16} 17 18/** 19* The goal of this chat, as a slim bar attached above the message box: part of the chat, but separate. Icon-only Pause or Resume, Edit and 20* Remove. The host owns the goal; every button asks it and draws the answer. 21*/ 22export function GoalBar ({ chatId} :{ chatId :string | null | undefined }) { 23const goal = useGoal ( chatId ); 24const streaming = useStore ( s => s . streaming ); 25const [ editing , setEditing ] = useState ( false ), [ removing , setRemoving ] = useState ( false ), [ text , setText ] = useState ( '' ), [ busy , setBusy ] = useState ( false ); 26useEffect (() => { setEditing ( false ); setRemoving ( false ); }, [ chatId ]); 27if ( ! chatId || ! goal ) return null ; 28const run = async ( fn :() => Promise < unknown >) => { 29setBusy ( true ); 30try { await fn (); } 31catch ( e ) { pushToast ( 'error' , tr ( "The goal was not changed" ), plainFailureText ( e instanceof Error ?e . message :String ( e )). message ); } 32finally { setBusy ( false ); } 33}; 34const resumable = goal . phase === 'paused' || goal . phase === 'blocked' ; 35return <> 36< div className = "goalbar" data-phase = { goal . phase } role = "group" aria-label = { tr ( "Goal" )}> 37< Icon name = { goal . phase === 'complete' ?'check' :'bolt' } size = { 16 }/> 38< span className = "goalbar__text" title = { goal . objective }>< strong >{ tr ( "Goal" )}</ strong >< span > · { goal . objective }</ span >< span className = "goalbar__state" > · { goalStatusText ( goal )}</ span ></ span > 39{ goal . phase !== 'complete' ?( resumable 40 ?< IconBtn name = "play" title = { tr ( "Resume the goal" )} size = { 16 } disabled = { busy || streaming } onClick = {() => void run (() => resumeGoal ())}/> 41 :< IconBtn name = "pause" title = { tr ( "Pause the goal" )} size = { 16 } disabled = { busy } onClick = {() => void run (() => goalAction ( chatId , 'pause' ))}/>) :null } 42{ goal . phase !== 'complete' ?< IconBtn name = "pencil" title = { tr ( "Edit the goal" )} size = { 16 } disabled = { busy } onClick = {() => { setText ( goal . objective ); setEditing ( true ); }}/> :null } 43< IconBtn name = "x" title = { tr ( "Remove the goal" )} size = { 16 } disabled = { busy } onClick = {() => setRemoving ( true )}/> 44</ div > 45< Dialog open = { editing } compact title = { tr ( "Edit the goal" )} onClose = {() => setEditing ( false )} actions = {<>< Btn onClick = {() => setEditing ( false )}>{ tr ( "Cancel" )}</ Btn >< Btn variant = "primary" disabled = { busy || ! text . trim ()} onClick = {() => void run ( async () => { await goalAction ( chatId , 'edit' , text . trim ()); setEditing ( false ); })}>{ tr ( "Save goal" )}</ Btn ></>}> 46< label className = "input-field" >< span >{ tr ( "What should the assistant keep working towards?" )}</ span >< textarea className = "st-input" rows = { 3 } maxLength = { 4000 } value = { text } onChange = { e => setText ( e . target . value )} autoFocus /></ label > 47</ Dialog > 48< Dialog open = { removing } compact title = { tr ( "Remove this goal?" )} onClose = {() => setRemoving ( false )} actions = {<>< Btn onClick = {() => setRemoving ( false )}>{ tr ( "Keep goal" )}</ Btn >< Btn variant = "danger" disabled = { busy } onClick = {() => void run ( async () => { await goalAction ( chatId , 'clear' ); setRemoving ( false ); })}>{ tr ( "Remove goal" )}</ Btn ></>}> 49< p >{ tr ( "The assistant stops working towards it. Your chat and files stay as they are." )}</ p > 50</ Dialog > 51</>; 52}