import {localeTag} from '../lib/i18n'; import { tr } from '../lib/i18n'; import { useLocation, navigate } from '../lib/router'; import { useEffect, useState } from 'react'; import { Btn, Dialog, Icon, IconBtn, Select, Seg, ErrorNotice } from '../components/ui'; import { TaskBoard } from '../components/TaskBoard'; import { useNative } from '../lib/native'; import { Markdown } from '../components/Markdown'; import { Screen } from '../components/layout'; import { api } from '../lib/client'; import { continueTaskInNewChat, pushToast, useStore } from '../lib/store'; import { blankTask as blank, type Task } from '../lib/tasks'; import { TaskEditor } from '../components/TaskEditor'; import { TaskDetail } from '../components/TaskDetail'; export function TasksScreen() { const location = useLocation(); const library = useStore(s => s.library); const native = useNative(); const loaded = library?.variants.find(v => v.id === library.loaded); const [tasks, setTasks] = useState([]), [query, setQuery] = useState(''), [sort, setSort] = useState('next'), archived = false; const [editor, setEditor] = useState(null), [assistant, setAssistant] = useState(false), [pending, setPending] = useState(false), [error, setError] = useState(''), [ready, setReady] = useState(false), [result, setResult] = useState(null), [removeTask, setRemoveTask] = useState(false); async function refresh() { const data = await api.get<{ tasks: Task[]; }>('/api/tasks'); setTasks(data.tasks); setReady(true); } useEffect(() => { let alive = true; const load = () => { if (alive) void refresh().catch(e => { if (alive) { setError(e.message); setReady(true); } }); }; load(); const timer = setInterval(load, 3000); window.addEventListener('strata:tasks', load); return () => { alive = false; clearInterval(timer); window.removeEventListener('strata:tasks', load); }; }, []); async function action(fn: () => Promise) { setPending(true); setError(''); try { await fn(); await refresh(); window.dispatchEvent(new Event('strata:tasks')); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setPending(false); } } function open(task: Task, assist = false) { setEditor({ ...task }); setAssistant(assist); setError(''); } useEffect(() => { if (location.query.has('new')) open(blank()); }, [location.query.get('new')]); function closeEditor() { setEditor(null); if (location.query.has('new')) navigate('tasks', location.param ?? undefined); } const currentTask = tasks.find(t => t.id === location.param); // Schedule | Board: the board is #/tasks?view=board, the same screen (no route of its own). const view: 'schedule' | 'board' = location.query.get('view') === 'board' && !location.param ? 'board' : 'schedule'; const setView = (next: 'schedule' | 'board') => navigate('tasks', undefined, next === 'board' ? { view: 'board' } : undefined); const visible = tasks.filter(t => (!location.param || t.id === location.param) && !!t.archived === archived && `${t.title} ${t.prompt}`.toLowerCase().includes(query.toLowerCase())).sort((a, b) => sort === 'name' ? a.title.localeCompare(b.title) : (a.nextRunAt ?? 'z').localeCompare(b.nextRunAt ?? 'z')); const selected = tasks.find(t => t.id === result); const taskHeader = currentTask ?
void action(() => api.patch(`/api/tasks/${currentTask.id}`, { enabled: !currentTask.enabled }))}/> setRemoveTask(true)}/> open(currentTask)}>{tr("Edit")} {tr(currentTask.lastRunAt || currentTask.output || currentTask.error ? continueTaskInNewChat(currentTask)}>{tr("Continue in a new chat")} : null)} void action(() => api.post(`/api/tasks/${currentTask.id}/${currentTask.status === 'running' ? 'stop' : 'run'}`))}>{tr(currentTask.status === 'running' ? 'Stop' : 'Run now')}
: null; return {currentTask.title} : "Tasks"} titleDetail={currentTask ? undefined : view === 'board' ? {tr('Cards move on their own as things happen. A card in Needs attention stays there until you deal with it or dismiss it.')} : {tr(library?.mode === 'simulated' ? 'Example model · sample answers. ' : '')}{tr("Runs while Strata’s host is open. Runs saved instructions using the loaded model.")}} actions={currentTask ? taskHeader :
setQuery(e.target.value)}/>{tr(view === 'board' ? null :