Skip to content

Commit 341c0d8

Browse files
authored
feat: do not automatically refresh content when browser tab is inactive (#2122)
1 parent 4776c0c commit 341c0d8

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed
Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
1+
import React from 'react';
2+
13
import {AUTO_REFRESH_INTERVAL} from '../constants';
24

35
import {useSetting} from './useSetting';
46

5-
export function useAutoRefreshInterval() {
6-
return useSetting(AUTO_REFRESH_INTERVAL, 0);
7+
export function useAutoRefreshInterval(): [number, (value: number) => void] {
8+
const [settingValue, setSettingValue] = useSetting(AUTO_REFRESH_INTERVAL, 0);
9+
const [effectiveInterval, setEffectiveInterval] = React.useState(
10+
document.visibilityState === 'visible' ? settingValue : 0,
11+
);
12+
13+
React.useEffect(() => {
14+
// Update the effective interval when the setting changes
15+
setEffectiveInterval(document.visibilityState === 'visible' ? settingValue : 0);
16+
17+
// Handle visibility change events
18+
const handleVisibilityChange = () => {
19+
setEffectiveInterval(document.visibilityState === 'visible' ? settingValue : 0);
20+
};
21+
22+
document.addEventListener('visibilitychange', handleVisibilityChange);
23+
24+
return () => {
25+
document.removeEventListener('visibilitychange', handleVisibilityChange);
26+
};
27+
}, [settingValue]);
28+
29+
return [effectiveInterval, setSettingValue];
730
}

0 commit comments

Comments
 (0)