File tree Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Expand file tree Collapse file tree 1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change
1
+ import React from 'react' ;
2
+
1
3
import { AUTO_REFRESH_INTERVAL } from '../constants' ;
2
4
3
5
import { useSetting } from './useSetting' ;
4
6
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 ] ;
7
30
}
You can’t perform that action at this time.
0 commit comments