mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 18:11:12 -05:00
Fix moderation queue auto-refresh on tab switch
This commit is contained in:
@@ -137,6 +137,7 @@ export function useModerationQueueManager(
|
||||
const pauseFetchingRef = useRef(false);
|
||||
const initialFetchCompleteRef = useRef(false);
|
||||
const isMountingRef = useRef(true);
|
||||
const fetchItemsRef = useRef<((silent?: boolean) => Promise<void>) | null>(null);
|
||||
|
||||
const FETCH_COOLDOWN_MS = 1000;
|
||||
|
||||
@@ -428,6 +429,11 @@ export function useModerationQueueManager(
|
||||
}
|
||||
}, [user, isAdmin, isSuperuser, filters, pagination, profileCache, entityCache, toast]);
|
||||
|
||||
// Store fetchItems in ref to avoid re-creating visibility listener
|
||||
useEffect(() => {
|
||||
fetchItemsRef.current = fetchItems;
|
||||
}, [fetchItems]);
|
||||
|
||||
/**
|
||||
* Show pending new items by merging them into the queue
|
||||
*/
|
||||
@@ -788,6 +794,12 @@ export function useModerationQueueManager(
|
||||
|
||||
// Visibility change handler
|
||||
useEffect(() => {
|
||||
// Early return if feature is disabled
|
||||
if (!settings.refreshOnTabVisible) {
|
||||
console.log('⚙️ refreshOnTabVisible is DISABLED - no listener attached');
|
||||
return;
|
||||
}
|
||||
|
||||
const handleVisibilityChange = () => {
|
||||
if (document.hidden) {
|
||||
console.log('📴 Tab hidden - pausing queue updates');
|
||||
@@ -796,16 +808,21 @@ export function useModerationQueueManager(
|
||||
console.log('📱 Tab visible - resuming queue updates');
|
||||
pauseFetchingRef.current = false;
|
||||
|
||||
if (settings.refreshOnTabVisible && initialFetchCompleteRef.current && !isMountingRef.current) {
|
||||
console.log('🔄 Tab became visible - triggering refresh');
|
||||
fetchItems(true);
|
||||
if (initialFetchCompleteRef.current && !isMountingRef.current && fetchItemsRef.current) {
|
||||
console.log('🔄 Tab became visible - triggering refresh (setting enabled)');
|
||||
fetchItemsRef.current(true);
|
||||
} else {
|
||||
console.log('⏭️ Tab became visible - skipping refresh (initial fetch not complete or mounting)');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||||
return () => document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
}, [settings.refreshOnTabVisible, fetchItems]);
|
||||
return () => {
|
||||
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||
console.log('🧹 Visibility listener removed');
|
||||
};
|
||||
}, [settings.refreshOnTabVisible]);
|
||||
|
||||
// Initialize realtime subscriptions
|
||||
useRealtimeSubscriptions({
|
||||
|
||||
Reference in New Issue
Block a user