Open History to see the last commit
1import { memo, useEffect, useRef } from "react";
2
3import {
4 nextThreadAnnouncement,
5 type ThreadActivitySnapshot,
6 type ThreadAnnouncementMemory,
7} from "./threadActivityAnnouncement";
8
9/**
10 * One polite live region for the open thread. Text is written straight to the
11 * DOM so an announcement never re-renders the timeline.
12 */
13export const ThreadActivityAnnouncer = memo(function ThreadActivityAnnouncer({
14 threadKey,
15 status,
16 requestId,
17 runId,
18 runStatus,
19 detailBehind,
20}: ThreadActivitySnapshot & {
21 /** Null while the timeline shows another thread during a switch. */
22 readonly threadKey: string | null;
23 /** See isDetailBehindShell. */
24 readonly detailBehind: boolean;
25}) {
26 const regionRef = useRef<HTMLDivElement>(null);
27 const memoryRef = useRef<{ threadKey: string; memory: ThreadAnnouncementMemory } | null>(null);
28
29 useEffect(() => {
30 const region = regionRef.current;
31 if (threadKey === null) {
32 memoryRef.current = null;
33 if (region !== null) region.textContent = "";
34 return;
35 }
36 if (detailBehind) return;
37 const previous = memoryRef.current?.threadKey === threadKey ? memoryRef.current.memory : null;
38 const { memory, announcement } = nextThreadAnnouncement(previous, {
39 status,
40 requestId,
41 runId,
42 runStatus,
43 });
44 memoryRef.current = { threadKey, memory };
45 if (region === null) return;
46 if (previous === null) {
47 region.textContent = "";
48 } else if (announcement !== null) {
49 // Screen readers ignore a write that leaves the text unchanged, so a
50 // repeated "Done" gets a trailing non-breaking space to count as new.
51 region.textContent = region.textContent === announcement ? `${announcement} ` : announcement;
52 }
53 }, [threadKey, status, requestId, runId, runStatus, detailBehind]);
54
55 // No role="status": Orca 50 maps it to a status bar in Chromium and drops its updates.
56 return <div ref={regionRef} aria-live="polite" className="sr-only" />;
57});