mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 12:11:11 -05:00
Refactor: Debounce effect fetches
This commit is contained in:
@@ -128,6 +128,8 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
|
|||||||
const fetchItemsRef = useRef<((silent?: boolean) => Promise<void>) | null>(null);
|
const fetchItemsRef = useRef<((silent?: boolean) => Promise<void>) | null>(null);
|
||||||
|
|
||||||
const FETCH_COOLDOWN_MS = 1000;
|
const FETCH_COOLDOWN_MS = 1000;
|
||||||
|
const EFFECT_DEBOUNCE_MS = 50; // Short debounce to let all effects settle
|
||||||
|
const effectFetchTimerRef = useRef<NodeJS.Timeout | null>(null);
|
||||||
|
|
||||||
// Store settings in refs to avoid re-creating fetchItems
|
// Store settings in refs to avoid re-creating fetchItems
|
||||||
const settingsRef = useRef(settings);
|
const settingsRef = useRef(settings);
|
||||||
@@ -856,19 +858,26 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
|
|||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [user?.id]);
|
}, [user?.id]);
|
||||||
|
|
||||||
|
// Debounced fetch for effects - prevents race conditions
|
||||||
|
const debouncedEffectFetch = useCallback(() => {
|
||||||
|
if (effectFetchTimerRef.current) {
|
||||||
|
clearTimeout(effectFetchTimerRef.current);
|
||||||
|
}
|
||||||
|
|
||||||
|
effectFetchTimerRef.current = setTimeout(() => {
|
||||||
|
console.log('[Debounced Fetch] Executing after effects settled');
|
||||||
|
fetchItemsRef.current?.(true);
|
||||||
|
}, EFFECT_DEBOUNCE_MS);
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Filter and tab changes trigger refetch
|
// Filter and tab changes trigger refetch
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!user || !initialFetchCompleteRef.current || isMountingRef.current) return;
|
if (!user || !initialFetchCompleteRef.current || isMountingRef.current) return;
|
||||||
|
|
||||||
console.log('[Filter/Tab Change] Refetching with:', {
|
console.log('[Filter/Tab Change] Queuing debounced fetch');
|
||||||
tab: filters.activeTab,
|
|
||||||
entity: filters.debouncedEntityFilter,
|
|
||||||
status: filters.debouncedStatusFilter
|
|
||||||
});
|
|
||||||
|
|
||||||
pagination.reset();
|
pagination.reset();
|
||||||
fetchItemsRef.current?.(true);
|
debouncedEffectFetch();
|
||||||
}, [filters.activeTab, filters.debouncedEntityFilter, filters.debouncedStatusFilter, user]);
|
}, [filters.activeTab, filters.debouncedEntityFilter, filters.debouncedStatusFilter, user, debouncedEffectFetch, pagination]);
|
||||||
|
|
||||||
// Sort changes trigger refetch
|
// Sort changes trigger refetch
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -876,20 +885,26 @@ export function useModerationQueueManager(config: ModerationQueueManagerConfig):
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('[Sort Change] Refetching with:', {
|
console.log('[Sort Change] Queuing debounced fetch');
|
||||||
field: sort.field,
|
|
||||||
direction: sort.direction
|
|
||||||
});
|
|
||||||
pagination.reset();
|
pagination.reset();
|
||||||
fetchItemsRef.current?.(true);
|
debouncedEffectFetch();
|
||||||
}, [sort.field, sort.direction, user, pagination]);
|
}, [sort.field, sort.direction, user, pagination, debouncedEffectFetch]);
|
||||||
|
|
||||||
// Pagination changes trigger refetch
|
// Pagination changes trigger refetch
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!user || !initialFetchCompleteRef.current || pagination.currentPage === 1) return;
|
if (!user || !initialFetchCompleteRef.current || pagination.currentPage === 1) return;
|
||||||
|
|
||||||
fetchItemsRef.current?.(true);
|
debouncedEffectFetch();
|
||||||
}, [pagination.currentPage, pagination.pageSize]);
|
}, [pagination.currentPage, pagination.pageSize, debouncedEffectFetch]);
|
||||||
|
|
||||||
|
// Cleanup effect timer on unmount
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
if (effectFetchTimerRef.current) {
|
||||||
|
clearTimeout(effectFetchTimerRef.current);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Polling effect (when realtime disabled)
|
// Polling effect (when realtime disabled)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user