@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 86 lines 2.9 KB
Raw
1 import { msg } from "gt-react";
2 import { useState, useSyncExternalStore } from "react";
3
4 import { useStableCallback } from "~/hooks/useStableCallback";
5 import { m } from "~/i18n/messages";
6 import { getDocumentActivity, subscribeDocumentActivity } from "~/lib/documentActivity";
7
8 const POLL_INTERVAL_MS = 1500;
9 const CHECK_FAILED_MESSAGE = msg("Could not check permissions. We'll try again automatically.");
10
11 interface PermissionSnapshot<Id extends string> {
12 readonly status: Record<Id, boolean>;
13 readonly error: string | null;
14 }
15
16 /**
17 * Holds the last permission check result and polls while subscribed: on
18 * subscribe, every interval, and when the window regains focus or visibility.
19 * Checks are skipped while the tab is hidden and never overlap.
20 */
21 export function createPermissionPoller<Id extends string>(
22 check: () => Promise<Record<Id, boolean>>,
23 initialStatus: Record<Id, boolean>,
24 ) {
25 let snapshot: PermissionSnapshot<Id> = { status: initialStatus, error: null };
26 return {
27 getSnapshot: () => snapshot,
28 subscribe(onChange: () => void): () => void {
29 let disposed = false;
30 let checking = false;
31 const publish = (next: PermissionSnapshot<Id>) => {
32 if (disposed) return;
33 snapshot = next;
34 onChange();
35 };
36 const refresh = async () => {
37 if (disposed || checking || !getDocumentActivity().visible) return;
38 checking = true;
39 try {
40 publish({ status: await check(), error: null });
41 } catch {
42 publish({ status: snapshot.status, error: m(CHECK_FAILED_MESSAGE) });
43 }
44 checking = false;
45 };
46 void refresh();
47 const timer = setInterval(() => void refresh(), POLL_INTERVAL_MS);
48 let previous = getDocumentActivity();
49 const stopActivity = subscribeDocumentActivity(() => {
50 const next = getDocumentActivity();
51 const regained = (next.focused && !previous.focused) || next.visible !== previous.visible;
52 previous = next;
53 if (regained) void refresh();
54 });
55 return () => {
56 disposed = true;
57 clearInterval(timer);
58 stopActivity();
59 };
60 },
61 };
62 }
63
64 const noopSubscribe = () => () => {};
65
66 /**
67 * Each check calls the latest `check`, since callers close over props. `initialStatus` is
68 * read once, when the component mounts, and the subscription only restarts when `enabled` flips.
69 */
70 export function usePermissionStatus<Id extends string>(
71 check: () => Promise<Record<Id, boolean>>,
72 initialStatus: Record<Id, boolean>,
73 enabled = true,
74 ) {
75 const checkLatest = useStableCallback(check);
76 const [poller] = useState(() => createPermissionPoller(checkLatest, initialStatus));
77 const { status, error } = useSyncExternalStore(
78 enabled ? poller.subscribe : noopSubscribe,
79 poller.getSnapshot,
80 );
81 return {
82 status,
83 error,
84 isReady: (required: readonly Id[]) => error === null && required.every((id) => status[id]),
85 };
86 }