nexus/ui/src/hooks/useOnlineStatus.ts
Nexus Dev 471a9daaa6 feat(26): merge worktree code from plans 26-00, 26-01, 26-03
SW cache-first rewrite, React.lazy code splitting, PWA types/test stubs,
install prompt, offline banner, offline queue, ChatPanel wiring.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 03:55:48 +00:00

26 lines
738 B
TypeScript

import { useEffect, useState } from "react";
/**
* Reactive online/offline status hook.
* Returns true when navigator.onLine is true (and updates reactively).
*/
export function useOnlineStatus(): boolean {
const [isOnline, setIsOnline] = useState(() =>
typeof navigator !== "undefined" ? navigator.onLine : true,
);
useEffect(() => {
const handleOnline = () => setIsOnline(true);
const handleOffline = () => setIsOnline(false);
window.addEventListener("online", handleOnline);
window.addEventListener("offline", handleOffline);
return () => {
window.removeEventListener("online", handleOnline);
window.removeEventListener("offline", handleOffline);
};
}, []);
return isOnline;
}