mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-05-08 08:58:38 +01:00
Simplify online/offline status management
This commit is contained in:
+15
-6
@@ -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);
|
||||
};
|
||||
}, []);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user