lbry-desktop/ui/effects/use-connection-status.js
infinite-persistence 7d6c91c15a
Fix "you are offline" nag being stuck
Was trying to save one event listener in the previous implementation and rely on other redux state-changes to spark a GUI update, but turns out there are scenarios where nothing is updated and the "offline" nag is stuck on screen.

## Change
Do it properly using the event listeners. The nag should now update promptly.
2021-12-22 12:41:05 +08:00

25 lines
593 B
JavaScript

import React from 'react';
export default function useConnectionStatus() {
const [online, setOnline] = React.useState(window.navigator.onLine);
React.useEffect(() => {
function handleOnline(event) {
setOnline(true);
}
function handleOffline(event) {
setOnline(false);
}
window.addEventListener('online', handleOnline);
window.addEventListener('offline', handleOffline);
return () => {
window.removeEventListener('online', handleOnline);
window.removeEventListener('offline', handleOffline);
};
}, []);
return { online };
}