refactor: Drive connection status from tRPC subscription state

Replace browser online/offline event listeners with tRPC onEvent subscription
status, so the connection indicator reflects actual server reachability rather
than general network availability.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lukas May
2026-03-05 15:59:39 +01:00
parent ebe186bd5e
commit 42f5bcb80a
2 changed files with 6 additions and 26 deletions

View File

@@ -1,31 +1,11 @@
import { useState, useEffect } from "react";
import { trpc } from '@/lib/trpc';
export type ConnectionState = "connected" | "reconnecting" | "disconnected";
export function useConnectionStatus(): ConnectionState {
const [state, setState] = useState<ConnectionState>("connected");
const { status } = trpc.onEvent.useSubscription(undefined);
useEffect(() => {
function handleOnline() {
setState("connected");
}
function handleOffline() {
setState("disconnected");
}
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);
// Initial state
if (!navigator.onLine) {
setState("disconnected");
}
return () => {
window.removeEventListener("online", handleOnline);
window.removeEventListener("offline", handleOffline);
};
}, []);
return state;
if (status === 'pending') return "connected";
if (status === 'connecting') return "reconnecting";
return "disconnected"; // 'idle' (stream closed) or 'error' (fatal)
}

File diff suppressed because one or more lines are too long