mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 19:11:12 -05:00
Refactor polling to prevent interruptions
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user