2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
2021-09-17 22:24:21 +00:00
|
|
|
// 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;
|
|
|
|
}
|