import { useEffect, useState } from 'react'; import { tr } from '../lib/i18n'; import { goalAction, useGoal, type Goal } from '../lib/goal'; import { pushToast, resumeGoal, useStore } from '../lib/store'; import { plainFailureText } from '../lib/plain-errors'; import { Btn, Dialog, Icon, IconBtn } from './ui'; /** The words after the objective: where the goal is, in one short phrase. */ export function goalStatusText(goal: Goal): string { switch (goal.phase) { case 'paused': return tr('Paused'); case 'blocked': return goal.blocked || tr('Needs you'); case 'complete': return tr('Done'); default: return tr('Working'); // no "step 1 of 256": the host's round count is not a measure of progress } } /** * 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 * Remove. The host owns the goal; every button asks it and draws the answer. */ export function GoalBar({ chatId }: { chatId: string | null | undefined }) { const goal = useGoal(chatId); const streaming = useStore(s => s.streaming); const [editing, setEditing] = useState(false), [removing, setRemoving] = useState(false), [text, setText] = useState(''), [busy, setBusy] = useState(false); useEffect(() => { setEditing(false); setRemoving(false); }, [chatId]); if (!chatId || !goal) return null; const run = async (fn: () => Promise) => { setBusy(true); try { await fn(); } catch (e) { pushToast('error', tr("The goal was not changed"), plainFailureText(e instanceof Error ? e.message : String(e)).message); } finally { setBusy(false); } }; const resumable = goal.phase === 'paused' || goal.phase === 'blocked'; return <>
{tr("Goal")} · {goal.objective} · {goalStatusText(goal)} {goal.phase !== 'complete' ? (resumable ? void run(() => resumeGoal())}/> : void run(() => goalAction(chatId, 'pause'))}/>) : null} {goal.phase !== 'complete' ? { setText(goal.objective); setEditing(true); }}/> : null} setRemoving(true)}/>
setEditing(false)} actions={<> setEditing(false)}>{tr("Cancel")} void run(async () => { await goalAction(chatId, 'edit', text.trim()); setEditing(false); })}>{tr("Save goal")}}>