File
Blob: src/client/hooks/use-online.ts
| 1 | import { useSyncExternalStore } from "react"; |
| 2 | |
| 3 | function subscribe(cb: () => void) { |
| 4 | window.addEventListener("online", cb); |
| 5 | window.addEventListener("offline", cb); |
| 6 | return () => { |
| 7 | window.removeEventListener("online", cb); |
| 8 | window.removeEventListener("offline", cb); |
| 9 | }; |
| 10 | } |
| 11 | |
| 12 | function getSnapshot() { |
| 13 | return navigator.onLine; |
| 14 | } |
| 15 | |
| 16 | export function useOnline(): boolean { |
| 17 | return useSyncExternalStore(subscribe, getSnapshot); |
| 18 | } |