Disable start/call button if offline

This commit is contained in:
Evan Hahn 2021-10-19 08:53:11 -05:00 committed by GitHub
parent f914556e4c
commit 75248d8e2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 1 deletions

26
ts/hooks/useIsOnline.ts Normal file
View file

@ -0,0 +1,26 @@
// 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;
}