The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useIsFocused } from "@react-navigation/native"; |
| 2 | import { |
| 3 | nextThreadAnnouncement, |
| 4 | type ThreadAnnouncementMemory, |
| 5 | } from "@t3tools/client-runtime/state/thread-activity-announcement"; |
| 6 | import { useEffect, useRef } from "react"; |
| 7 | import { AccessibilityInfo } from "react-native"; |
| 8 | |
| 9 | import { m } from "../../i18n/messages"; |
| 10 | import { useAppActive } from "../../lib/appState"; |
| 11 | import { |
| 12 | nextThreadActivitySnapshot, |
| 13 | type ThreadActivityInput, |
| 14 | type ThreadActivityMemory, |
| 15 | } from "./threadStatusAnnouncement"; |
| 16 | |
| 17 | /** Tells VoiceOver and TalkBack when the open thread starts working, needs the user, or finishes. */ |
| 18 | export function useThreadStatusAnnouncements({ |
| 19 | threadKey, |
| 20 | subject, |
| 21 | detailBehind, |
| 22 | approvalRequestId, |
| 23 | userInputRequestId, |
| 24 | shellRequestId, |
| 25 | shellPendingUserInput, |
| 26 | working, |
| 27 | run, |
| 28 | }: ThreadActivityInput & { |
| 29 | /** Null when nothing should be announced, such as a provider subagent's thread. */ |
| 30 | readonly threadKey: string | null; |
| 31 | /** The thread's title, said after each status when another thread is on screen beside it. */ |
| 32 | readonly subject: string | null; |
| 33 | /** See isDetailBehindShell. */ |
| 34 | readonly detailBehind: boolean; |
| 35 | }) { |
| 36 | // A retained route stays mounted under the screen above it; it speaks only while shown, |
| 37 | // and starts from a silent baseline when it is shown again. |
| 38 | const focused = useIsFocused(); |
| 39 | const appActive = useAppActive(); |
| 40 | const activeKey = focused && appActive ? threadKey : null; |
| 41 | const memoryRef = useRef<{ |
| 42 | threadKey: string; |
| 43 | activity: ThreadActivityMemory; |
| 44 | announcement: ThreadAnnouncementMemory; |
| 45 | } | null>(null); |
| 46 | const runId = run?.runId ?? null; |
| 47 | const runStatus = run?.status ?? null; |
| 48 | |
| 49 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- speaks committed status changes through the platform screen reader |
| 50 | useEffect(() => { |
| 51 | if (activeKey === null) { |
| 52 | memoryRef.current = null; |
| 53 | return; |
| 54 | } |
| 55 | if (detailBehind) return; |
| 56 | const previous = memoryRef.current?.threadKey === activeKey ? memoryRef.current : null; |
| 57 | const activity = nextThreadActivitySnapshot(previous?.activity ?? null, { |
| 58 | approvalRequestId, |
| 59 | userInputRequestId, |
| 60 | shellRequestId, |
| 61 | shellPendingUserInput, |
| 62 | working, |
| 63 | run: runId === null || runStatus === null ? null : { runId, status: runStatus }, |
| 64 | }); |
| 65 | const next = nextThreadAnnouncement(previous?.announcement ?? null, activity.snapshot, m); |
| 66 | memoryRef.current = { threadKey: activeKey, activity, announcement: next.memory }; |
| 67 | // Queued, like the web's polite region, so VoiceOver doesn't cut off what it is reading. |
| 68 | if (next.announcement !== null) { |
| 69 | AccessibilityInfo.announceForAccessibilityWithOptions( |
| 70 | subject === null ? next.announcement : `${next.announcement}: ${subject}`, |
| 71 | { queue: true }, |
| 72 | ); |
| 73 | } |
| 74 | }, [ |
| 75 | activeKey, |
| 76 | subject, |
| 77 | detailBehind, |
| 78 | approvalRequestId, |
| 79 | userInputRequestId, |
| 80 | shellRequestId, |
| 81 | shellPendingUserInput, |
| 82 | working, |
| 83 | runId, |
| 84 | runStatus, |
| 85 | ]); |
| 86 | } |