Open History to see the last commit
1import { memo, useEffect, useRef } from "react";
2
3import { useLiveRegion } from "../../hooks/useLiveRegion";
4
5import {
6 nextThreadAnnouncement,
7 type ThreadActivitySnapshot,
8 type ThreadAnnouncementMemory,
9} from "./threadActivityAnnouncement";
10
11/** One polite live region for the open thread. */
12export const ThreadActivityAnnouncer = memo(function ThreadActivityAnnouncer({
13 threadKey,
14 status,
15 requestId,
16 runId,
17 runStatus,
18 detailBehind,
19}: ThreadActivitySnapshot & {
20 /** Null while the timeline shows another thread during a switch. */
21 readonly threadKey: string | null;
22 /** See isDetailBehindShell. */
23 readonly detailBehind: boolean;
24}) {
25 const { regionRef, announce, clear } = useLiveRegion();
26 const memoryRef = useRef<{ threadKey: string; memory: ThreadAnnouncementMemory } | null>(null);
27
28 useEffect(() => {
29 if (threadKey === null) {
30 memoryRef.current = null;
31 clear();
32 return;
33 }
34 if (detailBehind) return;
35 const previous = memoryRef.current?.threadKey === threadKey ? memoryRef.current.memory : null;
36 const { memory, announcement } = nextThreadAnnouncement(previous, {
37 status,
38 requestId,
39 runId,
40 runStatus,
41 });
42 memoryRef.current = { threadKey, memory };
43 if (previous === null) clear();
44 else if (announcement !== null) announce(announcement);
45 }, [threadKey, status, requestId, runId, runStatus, detailBehind, announce, clear]);
46
47 // No role="status": Orca 50 maps it to a status bar in Chromium and drops its updates.
48 return <div ref={regionRef} aria-live="polite" className="sr-only" />;
49});