Fix childer tennats

This commit is contained in:
Urtzi Alfaro
2025-12-29 17:25:20 +01:00
parent adef7971a0
commit c1dedfa44f
5 changed files with 108 additions and 57 deletions

View File

@@ -20,6 +20,10 @@ import { parseISO } from 'date-fns';
// Debounce delay for SSE-triggered query invalidations (ms)
const SSE_INVALIDATION_DEBOUNCE_MS = 500;
// Delay before SSE invalidations are allowed after initial load (ms)
// This prevents duplicate API calls when SSE events arrive during/right after initial fetch
const SSE_INITIAL_LOAD_GRACE_PERIOD_MS = 3000;
// ============================================================
// Types
// ============================================================
@@ -421,6 +425,15 @@ export function useControlPanelData(tenantId: string) {
// Ref for debouncing SSE-triggered invalidations
const invalidationTimeoutRef = useRef<NodeJS.Timeout | null>(null);
const lastEventCountRef = useRef<number>(0);
// Track when the initial data was successfully fetched to avoid immediate SSE refetches
const initialLoadTimestampRef = useRef<number | null>(null);
// Update initial load timestamp when query succeeds
useEffect(() => {
if (query.isSuccess && !initialLoadTimestampRef.current) {
initialLoadTimestampRef.current = Date.now();
}
}, [query.isSuccess]);
// SSE integration - invalidate query on relevant events (debounced)
useEffect(() => {
@@ -429,6 +442,17 @@ export function useControlPanelData(tenantId: string) {
return;
}
// OPTIMIZATION: Skip SSE-triggered invalidation during grace period after initial load
// This prevents duplicate API calls when SSE events arrive during/right after the initial fetch
if (initialLoadTimestampRef.current) {
const timeSinceInitialLoad = Date.now() - initialLoadTimestampRef.current;
if (timeSinceInitialLoad < SSE_INITIAL_LOAD_GRACE_PERIOD_MS) {
// Update the event count ref so we don't process these events later
lastEventCountRef.current = sseAlerts.length;
return;
}
}
const relevantEvents = sseAlerts.filter(event =>
event.event_type?.includes('production.') ||
event.event_type?.includes('batch_') ||