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