Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BackgroundThreadAnnouncer.tsx

64 lines2.8 KB

1import { useAtomValue } from "@effect/atom-react";
2import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
3import { effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
4import { useEffect, useRef } from "react";
5​
6import { useComposerDraftStore } from "../composerDraftStore";
7import { useLiveRegion } from "../hooks/useLiveRegion";
8import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
9import { useQuietThreadShells } from "../state/entities";
10import { liveShellEnvironmentIdsAtom } from "../state/shell";
11import { resolveActiveThreadRouteRef } from "../threadRoutes";
12import {
13 type BackgroundThreadMemory,
14 type BackgroundThreadSnapshot,
15 nextBackgroundThreadAnnouncement,
16 resolveBackgroundThreadRequest,
17} from "./backgroundThreadAnnouncement";
18​
19/**
20 * Speaks status changes for threads other than the open one. It lives outside
21 * the sidebar because a collapsed sidebar is inert, which silences its rows.
22 */
23export function BackgroundThreadAnnouncer() {
24 const threads = useQuietThreadShells();
25 const liveEnvironmentIds = useAtomValue(liveShellEnvironmentIdsAtom);
26 const routeTarget = useThreadRouteTarget();
27 const routeDraftThread = useComposerDraftStore((store) =>
28 routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null,
29 );
30 const openThreadRef = resolveActiveThreadRouteRef(routeTarget, routeDraftThread);
31 const openThreadKey = openThreadRef === null ? null : scopedThreadKey(openThreadRef);
32 const { regionRef, announce } = useLiveRegion();
33 const memoryRef = useRef<BackgroundThreadMemory>(new Map());
34​
35 useEffect(() => {
36 const now = new Date().toISOString();
37 const snapshots: BackgroundThreadSnapshot[] = [];
38 for (const thread of threads) {
39 // Subagents have no row of their own; their requests surface on the parent.
40 if (thread.archivedAt !== null || thread.lineage.relationshipToParent === "subagent")
41 continue;
42 if (!liveEnvironmentIds.has(thread.environmentId)) continue;
43 snapshots.push({
44 key: scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
45 title: thread.title,
46 ...resolveBackgroundThreadRequest(thread),
47 runId: thread.latestRun?.runId ?? null,
48 runStatus: thread.latestRun?.status ?? null,
49 activeRunId: thread.runtime?.activeRunId ?? null,
50 snoozed: effectiveSnoozed(thread, { now }),
51 });
52 }
53 const { memory, announcement } = nextBackgroundThreadAnnouncement(
54 memoryRef.current,
55 snapshots,
56 openThreadKey,
57 );
58 memoryRef.current = memory;
59 if (announcement !== null) announce(announcement);
60 }, [threads, liveEnvironmentIds, openThreadKey, announce]);
61​
62 // No role="status": Orca 50 maps it to a status bar in Chromium and drops its updates.
63 return <div ref={regionRef} aria-live="polite" className="sr-only" />;