signal-desktop/ts/hooks/useIsOnline.ts

27 lines
599 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';
export function useIsOnline(): boolean {
const [isOnline, setIsOnline] = useState(navigator.onLine);
useEffect(() => {
const update = () => {
setIsOnline(navigator.onLine);
};
update();
window.addEventListener('offline', update);
window.addEventListener('online', update);
return () => {
window.removeEventListener('offline', update);
window.removeEventListener('online', update);
};
}, []);
return isOnline;
}