Skip to content

feat: do not automatically refresh content when browser tab is inactive #2122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/utils/hooks/useAutoRefreshInterval.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import React from 'react';

import {AUTO_REFRESH_INTERVAL} from '../constants';

import {useSetting} from './useSetting';

export function useAutoRefreshInterval() {
return useSetting(AUTO_REFRESH_INTERVAL, 0);
export function useAutoRefreshInterval(): [number, (value: number) => void] {
const [settingValue, setSettingValue] = useSetting(AUTO_REFRESH_INTERVAL, 0);
const [effectiveInterval, setEffectiveInterval] = React.useState(
document.visibilityState === 'visible' ? settingValue : 0,
);

React.useEffect(() => {
// Update the effective interval when the setting changes
setEffectiveInterval(document.visibilityState === 'visible' ? settingValue : 0);

// Handle visibility change events
const handleVisibilityChange = () => {
setEffectiveInterval(document.visibilityState === 'visible' ? settingValue : 0);
};

document.addEventListener('visibilitychange', handleVisibilityChange);

return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
};
}, [settingValue]);

return [effectiveInterval, setSettingValue];
}
Loading