MCPcopy Create free account
hub / github.com/ResearAI/DeepScientist / useAdminTaskStream

Function useAdminTaskStream

src/ui/src/lib/hooks/useAdminTaskStream.ts:35–192  ·  view source on GitHub ↗
(taskId: string | null | undefined)

Source from the content-addressed store, hash-verified

33}
34
35export function useAdminTaskStream(taskId: string | null | undefined) {
36 const [task, setTask] = useState<AdminTask | null>(null)
37 const [events, setEvents] = useState<AdminTaskEvent[]>([])
38 const [connectionState, setConnectionState] = useState<'idle' | 'connecting' | 'connected' | 'reconnecting' | 'error'>(
39 taskId ? 'connecting' : 'idle'
40 )
41 const [error, setError] = useState<string | null>(null)
42 const abortRef = useRef<AbortController | null>(null)
43 const reconnectRef = useRef<number | null>(null)
44 const lastEventIdRef = useRef<string | null>(null)
45 const latestTaskRef = useRef<AdminTask | null>(null)
46
47 useEffect(() => {
48 latestTaskRef.current = task
49 }, [task])
50
51 useEffect(() => {
52 return () => {
53 abortRef.current?.abort()
54 if (reconnectRef.current != null) {
55 window.clearTimeout(reconnectRef.current)
56 }
57 }
58 }, [])
59
60 useEffect(() => {
61 if (!taskId) {
62 abortRef.current?.abort()
63 if (reconnectRef.current != null) {
64 window.clearTimeout(reconnectRef.current)
65 }
66 setTask(null)
67 setEvents([])
68 setConnectionState('idle')
69 setError(null)
70 lastEventIdRef.current = null
71 return
72 }
73
74 let cancelled = false
75
76 const fetchSnapshot = async (signal: AbortSignal) => {
77 const response = await fetch(`/api/system/tasks/${encodeURIComponent(taskId)}`, {
78 method: 'GET',
79 headers: {
80 Accept: 'application/json',
81 ...authHeaders(),
82 },
83 signal,
84 })
85 if (!response.ok) {
86 throw new Error(`HTTP ${response.status}`)
87 }
88 const payload = (await response.json()) as { task?: AdminTask | null }
89 if (payload?.task && typeof payload.task === 'object') {
90 setTask(payload.task)
91 }
92 }

Callers 3

SettingsSummarySectionFunction · 0.90
BenchStoreDialogFunction · 0.90

Calls 1

runFunction · 0.70

Tested by

no test coverage detected