import { tr } from '../lib/i18n'; import { useEffect, useRef, useState } from 'react'; import { Btn, Dialog, Icon, IconBtn } from '../components/ui'; import { api } from '../lib/client'; import { navigate, href, useLocation } from '../lib/router'; import { blankTask, type Task } from '../lib/tasks'; import { TaskEditor } from '../components/TaskEditor'; import { pushToast, setNavOpen } from '../lib/store'; import { useBoard } from '../lib/use-board'; type TaskLeaf = Task & { id: string; }; export function TaskTree({ collapsed }: { collapsed: boolean; }) { const [editor, setEditor] = useState(null); const location = useLocation(); const { board } = useBoard(); const [open, setOpen] = useState(() => localStorage.getItem('strata.tasks-open') !== 'false'); const [tasks, setTasks] = useState([]), [remove, setRemove] = useState(null), [error, setError] = useState(''), [busy, setBusy] = useState(false); const [leaving, setLeaving] = useState(null); const leavingRef = useRef(null), animationTimer = useRef | null>(null); useEffect(() => { let alive = true; const refresh = () => { void api.get<{ tasks: TaskLeaf[]; }>('/api/tasks').then(r => { if (alive && !leavingRef.current) { setTasks(r.tasks.filter(t => !t.archived)); setError(''); } }).catch(e => { if (alive) setError(e.message); }); }; refresh(); const timer = setInterval(refresh, 5000); window.addEventListener('strata:tasks', refresh); return () => { alive = false; clearInterval(timer); if (animationTimer.current) clearTimeout(animationTimer.current); window.removeEventListener('strata:tasks', refresh); }; }, []); async function deleteTask() { if (!remove) return; const task = remove; setBusy(true); setError(''); try { await api.patch(`/api/tasks/${task.id}`, { archived: true, enabled: false }); leavingRef.current = task.id; setLeaving(task.id); setRemove(null); pushToast('success', 'Task removed'); if (location.param === task.id) navigate('tasks'); animationTimer.current = setTimeout(() => { setTasks(ts => ts.filter(t => t.id !== task.id)); leavingRef.current = null; setLeaving(null); window.dispatchEvent(new Event('strata:tasks')); }, 240); } catch (e) { setError(e instanceof Error ? e.message : String(e)); } finally { setBusy(false); } } // A dialog opened from the narrow-window menu closes the menu first (QA F03: the form opened behind it and the keyboard stayed there). The dialogs // live here, so they stay mounted while the menu is closed. const dialogs = <> {tr(editor ? setEditor(null)}/> : null)} !busy && setRemove(null)} actions={<> setRemove(null)}>{tr("Keep task")} void deleteTask()}>{tr(busy ? 'Removing…' : 'Remove task')}}>

“{remove?.title}”{tr(" will stop running and be removed.")}

{tr(error ?

{tr(error)}

: null)}
; if (collapsed) return dialogs; return
{ setNavOpen(false); setEditor(blankTask()); }}/>
{tr(open ?
{tr(tasks.map(t =>
{t.title}{tr(board.problemTasks.includes(t.id) ? : null)}{tr(!t.enabled ? : null)}
))}{tr(!tasks.length ?

{tr("No tasks yet")}

: null)}{tr(error && !remove ?

{tr("Couldn\u2019t load tasks.")}

: null)}
: null)} {dialogs}
; }