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:
@@ -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
Reference in New Issue
Block a user