okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
dca9770ef
main
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 () { 18if ( inFlight ) return ; 19inFlight = true ; 20try { 21const [ tasks , waiting ] = await Promise . all ([ 22api . get <{ tasks :Task [] }>( '/api/tasks' ), 23api . get <{ chats :{ chatId :string ; folder :string | null ; pending :{ callId :string ; at :number }[] }[] }>( '/api/agent/pending' ). catch (() => null ), 24]); 25shared = { 26tasks :tasks . tasks , 27// Unknown (the host did not answer) is not the same as "nothing waits": the chats' own cards are trusted then. 28pending :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 , 29ready :true , error :'' , 30}; 31} catch ( e ) { 32shared = { ...shared , ready :true , error :e instanceof Error ?e . message :String ( e ) }; 33} finally { 34inFlight = false ; 35listeners . 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 ) { 43listeners . add ( listener ); 44if ( listeners . size === 1 ) { void refresh (); timer = setInterval (() => void refresh (), 4000 ); window . addEventListener ( 'strata:tasks' , refreshBoard ); } 45return () => { 46listeners . delete ( listener ); 47if ( ! 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 [] } { 52const [, tick ] = useState ( 0 ); 53useEffect (() => subscribe (() => tick ( n => n + 1 )), []); 54const 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. 56const [ minute , setMinute ] = useState (() => Math . floor ( Date . now () / 60_000 )); 57useEffect (() => { const t = setInterval (() => setMinute ( Math . floor ( Date . now () / 60_000 )), 30_000 ); return () => clearInterval ( t ); }, []); 58const 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); 64return { board, ready :shared . ready , error :shared . error , tasks :shared . tasks }; 65}