Last active
December 22, 2023 19:04
-
-
Save awinogradov/8e81d2e77c3d6e2504c9dcab5b6cea4c to your computer and use it in GitHub Desktop.
Offline detector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useEffect, useState } from 'react'; | |
interface UseOfflineDetectorProps { | |
setStatus?: ([global, remote]: [boolean, boolean]) => void; | |
pollingDelay?: number; | |
remoteServerUrl?: string; | |
}; | |
export const useOfflineDetector = ({ | |
setStatus, | |
pollingDelay = 5000, | |
remoteServerUrl = '/', | |
}: UseOfflineDetectorProps) => { | |
const [globalOnlineStatus, setGlobalOnlineStatus] = useState(navigator.onLine); | |
const [remoteServerStatus, setRemoteServerStatus] = useState(true); | |
let timeout: NodeJS.Timeout; | |
const toggleStatus = useCallback((global: boolean) => () => { | |
setGlobalOnlineStatus(global); | |
setStatus?.([global, remoteServerStatus]); | |
}, [setStatus]); | |
useEffect(() => { | |
window.addEventListener('online', toggleStatus(true)); | |
window.addEventListener('offline', toggleStatus(false)); | |
if (!timeout) { | |
setTimeout(() => { | |
if (globalOnlineStatus !== navigator.onLine) { | |
setGlobalOnlineStatus(navigator.onLine); | |
} | |
fetch(remoteServerUrl, { method: 'HEAD' }) | |
.then((res) => { | |
setRemoteServerStatus(res.status < 400); | |
setStatus?.([globalOnlineStatus, true]); | |
}) | |
.catch(() => { | |
setRemoteServerStatus(false); | |
setStatus?.([globalOnlineStatus, false]); | |
}); | |
}, pollingDelay); | |
} | |
return () => { | |
window.removeEventListener('online', toggleStatus(true)); | |
window.removeEventListener('offline', toggleStatus(false)); | |
clearTimeout(timeout); | |
}; | |
}, []); | |
return [globalOnlineStatus, remoteServerStatus]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment