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

ThreadActivityAnnouncer.tsx

50 lines1.7 KB

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 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- writes thread status changes to the aria-live region after commit
29 useEffect(() => {
30 if (threadKey === null) {
31 memoryRef.current = null;
32 clear();
33 return;
34 }
35 if (detailBehind) return;
36 const previous = memoryRef.current?.threadKey === threadKey ? memoryRef.current.memory : null;
37 const { memory, announcement } = nextThreadAnnouncement(previous, {
38 status,
39 requestId,
40 runId,
41 runStatus,
42 });
43 memoryRef.current = { threadKey, memory };
44 if (previous === null) clear();
45 else if (announcement !== null) announce(announcement);
46 }, [threadKey, status, requestId, runId, runStatus, detailBehind, announce, clear]);
47​
48 // No role="status": Orca 50 maps it to a status bar in Chromium and drops its updates.
49 return <div ref={regionRef} aria-live="polite" className="sr-only" />;
50});