signal-desktop/ts/hooks/useIsOnline.ts
2021-10-19 08:53:11 -05:00

26 lines
599 B
TypeScript

// 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;
}