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';
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
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 {
|
2024-03-18 21:48:00 +00:00
|
|
|
const [isOnline, setIsOnline] = useState(getOnlineStatus());
|
2021-10-19 13:53:11 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const update = () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
setIsOnline(getOnlineStatus());
|
2021-10-19 13:53:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
2024-03-18 21:48:00 +00:00
|
|
|
window.Whisper.events.on('online', update);
|
|
|
|
window.Whisper.events.on('offline', update);
|
2021-10-19 13:53:11 +00:00
|
|
|
|
|
|
|
return () => {
|
2024-03-18 21:48:00 +00:00
|
|
|
window.Whisper.events.off('online', update);
|
|
|
|
window.Whisper.events.off('offline', update);
|
2021-10-19 13:53:11 +00:00
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return isOnline;
|
|
|
|
}
|