2021-09-17 22:24:21 +00:00
|
|
|
// Copyright 2020-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
export function usePageVisibility(): boolean {
|
|
|
|
const [result, setResult] = useState(!document.hidden);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-10-07 18:52:07 +00:00
|
|
|
const updatePageVisibility = () => {
|
2021-09-17 22:24:21 +00:00
|
|
|
setResult(!document.hidden);
|
|
|
|
};
|
|
|
|
|
2021-10-07 18:52:07 +00:00
|
|
|
updatePageVisibility();
|
|
|
|
|
|
|
|
document.addEventListener('visibilitychange', updatePageVisibility, false);
|
2021-09-17 22:24:21 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
document.removeEventListener(
|
|
|
|
'visibilitychange',
|
2021-10-07 18:52:07 +00:00
|
|
|
updatePageVisibility,
|
2021-09-17 22:24:21 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|