diff --git a/app/hooks/useDraftSocket.ts b/app/hooks/useDraftSocket.ts index ea483a4..1875000 100644 --- a/app/hooks/useDraftSocket.ts +++ b/app/hooks/useDraftSocket.ts @@ -93,12 +93,31 @@ export function useDraftSocket(seasonId: string, teamId?: string): UseDraftSocke } }; + const handleVisibilityChange = () => { + if (document.visibilityState !== "visible") return; + + if (!socketRef.current?.connected) { + // Mobile browsers kill WebSockets when the app is backgrounded without + // firing "offline". Reconnect immediately when the user returns. + socketRef.current?.connect(); + } else { + // Socket appears connected but may have gone stale while JS was suspended. + // Rejoin the draft room (idempotent) and bump reconnectCount so the draft + // page revalidates loader data to catch any picks/state changes missed + // while the browser was backgrounded. + socketRef.current?.emit("join-draft", seasonId, teamId); + setReconnectCount((c) => c + 1); + } + }; + window.addEventListener("offline", handleOffline); window.addEventListener("online", handleOnline); + document.addEventListener("visibilitychange", handleVisibilityChange); return () => { window.removeEventListener("offline", handleOffline); window.removeEventListener("online", handleOnline); + document.removeEventListener("visibilitychange", handleVisibilityChange); console.log("Leaving draft room:", seasonId); socket.emit("leave-draft", seasonId); socket.disconnect();