signal-desktop/ts/hooks/useIsOnline.ts

36 lines
782 B
TypeScript
Raw Normal View History

2021-10-19 13:53:11 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useEffect, useState } from 'react';
function getOnlineStatus(): boolean {
if (window.textsecure) {
return window.textsecure.server?.isOnline() ?? true;
}
// Only for storybook
return navigator.onLine;
}
2021-10-19 13:53:11 +00:00
export function useIsOnline(): boolean {
const [isOnline, setIsOnline] = useState(getOnlineStatus());
2021-10-19 13:53:11 +00:00
useEffect(() => {
const update = () => {
setIsOnline(getOnlineStatus());
2021-10-19 13:53:11 +00:00
};
update();
window.Whisper.events.on('online', update);
window.Whisper.events.on('offline', update);
2021-10-19 13:53:11 +00:00
return () => {
window.Whisper.events.off('online', update);
window.Whisper.events.off('offline', update);
2021-10-19 13:53:11 +00:00
};
}, []);
return isOnline;
}