Refresh thread replies periodically & when refocusing window (#36547)
This commit is contained in:
parent
6adbd9ce52
commit
7ea2af6ae2
4 changed files with 266 additions and 83 deletions
32
app/javascript/mastodon/hooks/useIsDocumentVisible.ts
Normal file
32
app/javascript/mastodon/hooks/useIsDocumentVisible.ts
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
export function useIsDocumentVisible({
|
||||
onChange,
|
||||
}: {
|
||||
onChange?: (isVisible: boolean) => void;
|
||||
} = {}) {
|
||||
const [isDocumentVisible, setIsDocumentVisible] = useState(
|
||||
() => document.visibilityState === 'visible',
|
||||
);
|
||||
|
||||
const onChangeRef = useRef(onChange);
|
||||
useEffect(() => {
|
||||
onChangeRef.current = onChange;
|
||||
}, [onChange]);
|
||||
|
||||
useEffect(() => {
|
||||
function handleVisibilityChange() {
|
||||
const isVisible = document.visibilityState === 'visible';
|
||||
|
||||
setIsDocumentVisible(isVisible);
|
||||
onChangeRef.current?.(isVisible);
|
||||
}
|
||||
window.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return isDocumentVisible;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue