feat: public events/polls, friends graph, captcha, SSE hardening, and admin UX
Ship product gaps: register/vote pages, friend add/accept/decline/remove, email verify TTL, captcha on login/forgot, soft-fail user actions, SSE abort/shared client, Commando Centrum error toasts, admin delete for events/polls, and IT/NL i18n fills. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
2ff08e5127
commit
ed7db6e048
76 files changed
+4834
-1376
No files matched your search
@@ -6,17 +6,30 @@ type UseEventSourceOptions = {
|
||||
onMessage?: (data: unknown) => void;
|
||||
onError?: (event: Event) => void;
|
||||
enabled?: boolean;
|
||||
/** Max reconnect attempts after errors (default 8). Set 0 to disable reconnect. */
|
||||
maxRetries?: number;
|
||||
/** Initial reconnect delay in ms (default 1000); doubles each attempt. */
|
||||
baseDelayMs?: number;
|
||||
/** Cap on reconnect delay in ms (default 30000). */
|
||||
maxDelayMs?: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* Subscribe to a Server-Sent Events (SSE) endpoint.
|
||||
* Reconnects automatically on connection loss.
|
||||
* Reconnects with exponential backoff up to `maxRetries`.
|
||||
*/
|
||||
export function useEventSource<T = unknown>(
|
||||
url: string | null,
|
||||
options?: UseEventSourceOptions,
|
||||
) {
|
||||
const { onMessage, onError, enabled = true } = options ?? {};
|
||||
const {
|
||||
onMessage,
|
||||
onError,
|
||||
enabled = true,
|
||||
maxRetries = 8,
|
||||
baseDelayMs = 1000,
|
||||
maxDelayMs = 30_000,
|
||||
} = options ?? {};
|
||||
const [data, setData] = useState<T | null>(null);
|
||||
const [connected, setConnected] = useState(false);
|
||||
const onMessageRef = useRef(onMessage);
|
||||
@@ -29,12 +42,18 @@ export function useEventSource<T = unknown>(
|
||||
|
||||
const endpoint: string = url;
|
||||
let es: EventSource | null = null;
|
||||
let reconnectTimer: ReturnType<typeof setTimeout>;
|
||||
let reconnectTimer: ReturnType<typeof setTimeout> | undefined;
|
||||
let attempt = 0;
|
||||
let disposed = false;
|
||||
|
||||
function connect() {
|
||||
if (disposed) return;
|
||||
es = new EventSource(endpoint);
|
||||
|
||||
es.onopen = () => setConnected(true);
|
||||
es.onopen = () => {
|
||||
attempt = 0;
|
||||
setConnected(true);
|
||||
};
|
||||
|
||||
es.onmessage = (event) => {
|
||||
try {
|
||||
@@ -48,20 +67,27 @@ export function useEventSource<T = unknown>(
|
||||
|
||||
es.onerror = (event) => {
|
||||
es?.close();
|
||||
es = null;
|
||||
setConnected(false);
|
||||
onErrorRef.current?.(event);
|
||||
reconnectTimer = setTimeout(connect, 3000);
|
||||
|
||||
if (disposed || attempt >= maxRetries) return;
|
||||
|
||||
const delay = Math.min(baseDelayMs * 2 ** attempt, maxDelayMs);
|
||||
attempt++;
|
||||
reconnectTimer = setTimeout(connect, delay);
|
||||
};
|
||||
}
|
||||
|
||||
connect();
|
||||
|
||||
return () => {
|
||||
clearTimeout(reconnectTimer);
|
||||
disposed = true;
|
||||
if (reconnectTimer !== undefined) clearTimeout(reconnectTimer);
|
||||
es?.close();
|
||||
setConnected(false);
|
||||
};
|
||||
}, [url, enabled]);
|
||||
}, [url, enabled, maxRetries, baseDelayMs, maxDelayMs]);
|
||||
|
||||
return { data, connected };
|
||||
}
|
||||
Reference in new issue
Block a user