Simplify online/offline status management

This commit is contained in:
Fedor Indutny 2024-03-18 14:48:00 -07:00 committed by GitHub
parent b359d28771
commit 9aff86f02b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 432 additions and 335 deletions

View file

@ -3,22 +3,31 @@
import { useEffect, useState } from 'react';
function getOnlineStatus(): boolean {
if (window.textsecure) {
return window.textsecure.server?.isOnline() ?? true;
}
// Only for storybook
return navigator.onLine;
}
export function useIsOnline(): boolean {
const [isOnline, setIsOnline] = useState(navigator.onLine);
const [isOnline, setIsOnline] = useState(getOnlineStatus());
useEffect(() => {
const update = () => {
setIsOnline(navigator.onLine);
setIsOnline(getOnlineStatus());
};
update();
window.addEventListener('offline', update);
window.addEventListener('online', update);
window.Whisper.events.on('online', update);
window.Whisper.events.on('offline', update);
return () => {
window.removeEventListener('offline', update);
window.removeEventListener('online', update);
window.Whisper.events.off('online', update);
window.Whisper.events.off('offline', update);
};
}, []);