Refactor polling to prevent interruptions

This commit is contained in:
gpt-engineer-app[bot]
2025-10-03 19:12:50 +00:00
parent a2d3ed5ea4
commit b221c75d4a
3 changed files with 57 additions and 23 deletions

View File

@@ -29,6 +29,7 @@ export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
});
const [isLoading, setIsLoading] = useState(true);
const [isInitialLoad, setIsInitialLoad] = useState(true);
const [lastUpdated, setLastUpdated] = useState<Date | null>(null);
const onStatsChangeRef = useRef(onStatsChange);
@@ -37,11 +38,14 @@ export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
onStatsChangeRef.current = onStatsChange;
}, [onStatsChange]);
const fetchStats = useCallback(async () => {
const fetchStats = useCallback(async (silent = false) => {
if (!enabled) return;
try {
setIsLoading(true);
// Only show loading on initial load
if (!silent) {
setIsLoading(true);
}
const [submissionsResult, reportsResult, reviewsResult] = await Promise.all([
supabase
@@ -70,27 +74,35 @@ export const useModerationStats = (options: UseModerationStatsOptions = {}) => {
} catch (error) {
console.error('Error fetching moderation stats:', error);
} finally {
setIsLoading(false);
// Only clear loading if it was set
if (!silent) {
setIsLoading(false);
}
if (isInitialLoad) {
setIsInitialLoad(false);
}
}
}, [enabled]);
}, [enabled, isInitialLoad]);
// Initial fetch
useEffect(() => {
if (enabled) {
fetchStats();
fetchStats(false); // Show loading
}
}, [enabled, fetchStats]);
// Polling
useEffect(() => {
if (!enabled || !pollingEnabled) return;
if (!enabled || !pollingEnabled || isInitialLoad) return;
const interval = setInterval(fetchStats, pollingInterval);
const interval = setInterval(() => {
fetchStats(true); // Silent refresh
}, pollingInterval);
return () => {
clearInterval(interval);
};
}, [enabled, pollingEnabled, pollingInterval, fetchStats]);
}, [enabled, pollingEnabled, pollingInterval, fetchStats, isInitialLoad]);
return {
stats,