okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Task board: Schedule | Board view on Tasks, derivation with tests, loop record, sidebar count, all 8 localesdca9770ef

main
3.3 KiB65 linesraw
1import { useEffect, useMemo, useState } from 'react';
2import { api } from './client';
3import { boardCards, type BoardPending, type BoardResult } from './board';
4import { liveChatIds, loopChatIds, useStore } from './store';
5import type { Task } from './tasks';
6
7/**
8 * What the board and the sidebar's count read: the host's tasks and its waiting approvals (asked every few seconds, by one timer however
9 * many parts of the page use them), turned into cards together with the chats in this browser.
10 */
11interface Shared { tasks: Task[]; pending: BoardPending[] | null; ready: boolean; error: string }
12let shared: Shared = { tasks: [], pending: null, ready: false, error: '' };
13const listeners = new Set<() => void>();
14let timer: ReturnType<typeof setInterval> | null = null;
15let inFlight = false;
16
17async function refresh() {
18  if (inFlight) return;
19  inFlight = true;
20  try {
21    const [tasks, waiting] = await Promise.all([
22      api.get<{ tasks: Task[] }>('/api/tasks'),
23      api.get<{ chats: { chatId: string; folder: string | null; pending: { callId: string; at: number }[] }[] }>('/api/agent/pending').catch(() => null),
24    ]);
25    shared = {
26      tasks: tasks.tasks,
27      // Unknown (the host did not answer) is not the same as "nothing waits": the chats' own cards are trusted then.
28      pending: waiting ? waiting.chats.map(c => ({ chatId: c.chatId, folder: c.folder, callIds: c.pending.map(p => p.callId), at: Math.min(...c.pending.map(p => p.at)) })) : null,
29      ready: true, error: '',
30    };
31  } catch (e) {
32    shared = { ...shared, ready: true, error: e instanceof Error ? e.message : String(e) };
33  } finally {
34    inFlight = false;
35    listeners.forEach(l => l());
36  }
37}
38
39/** Ask now (after an action), instead of waiting for the next tick. */
40export function refreshBoard() { void refresh(); }
41
42function subscribe(listener: () => void) {
43  listeners.add(listener);
44  if (listeners.size === 1) { void refresh(); timer = setInterval(() => void refresh(), 4000); window.addEventListener('strata:tasks', refreshBoard); }
45  return () => {
46    listeners.delete(listener);
47    if (!listeners.size && timer) { clearInterval(timer); timer = null; window.removeEventListener('strata:tasks', refreshBoard); }
48  };
49}
50
51export function useBoard(): { board: BoardResult; ready: boolean; error: string; tasks: Task[] } {
52  const [, tick] = useState(0);
53  useEffect(() => subscribe(() => tick(n => n + 1)), []);
54  const sessions = useStore(s => s.sessions), activeSession = useStore(s => s.activeSession), live = useStore(s => s.liveChats);
55  // The clock moves the "waiting 4 min" labels and the seven-day window on, without needing a new fetch.
56  const [minute, setMinute] = useState(() => Math.floor(Date.now() / 60_000));
57  useEffect(() => { const t = setInterval(() => setMinute(Math.floor(Date.now() / 60_000)), 30_000); return () => clearInterval(t); }, []);
58  const board = useMemo(
59    () => boardCards({ tasks: shared.tasks, sessions, liveChats: liveChatIds(), loopChats: loopChatIds(), pending: shared.pending, activeSession, now: Date.now() }),
60    // `live` (how many chats are mid-turn) changes whenever a turn starts or ends; the shared snapshot changes by object identity.
61    // eslint-disable-next-line react-hooks/exhaustive-deps
62    [shared, sessions, activeSession, live, minute],
63  );
64  return { board, ready: shared.ready, error: shared.error, tasks: shared.tasks };
65}