Spaces:
Paused

dushyantsingh0 commited on
Commit
56cee03
·
1 Parent(s): 249a1a0

fix: ensure AI insights and narrative update in real-time on live event

Browse files
frontend/packages/shared/src/hooks/useDashboard.ts CHANGED
@@ -661,10 +661,14 @@ export const useAIInsights = (workspaceId?: string) => {
661
  };
662
  }, []);
663
 
664
- const fetchInsights = useCallback(async (force: boolean = false) => {
665
  const isNewWorkspace = lastLoadedAIWorkspaceId !== workspaceId;
666
  const isPending = (globalAIInsights as any)?.type === 'ASYNC_PENDING';
667
- if (globalAIInsightsLoading || (!force && globalAIInsights && !isNewWorkspace && !isPending)) return;
 
 
 
 
668
 
669
  lastLoadedAIWorkspaceId = workspaceId;
670
  globalAIInsightsLoading = true;
@@ -709,8 +713,8 @@ export const useAIInsights = (workspaceId?: string) => {
709
  // If no workspaceId, we are likely in a context that uses the default/current
710
  if (!workspaceId || data.workspace_id === workspaceId) {
711
  console.log('[DASHBOARD] AI Insights ready signal received, refreshing...');
712
- // DO NOT USE TRUE: Fetch the newly ready cache without forcing re-generation
713
- fetchInsights();
714
  }
715
  }
716
  };
@@ -755,10 +759,13 @@ export const useServiceNarrative = (workspaceId?: string) => {
755
  };
756
  }, []);
757
 
758
- const fetchNarrative = useCallback(async (force: boolean = false) => {
759
  const isNewWorkspace = lastLoadedNarrativeWorkspaceId !== workspaceId;
760
  const isPending = (globalNarrative as any)?.status === 'PENDING';
761
- if (globalNarrativeLoading || (!force && globalNarrative && !isNewWorkspace && !isPending)) return;
 
 
 
762
 
763
  lastLoadedNarrativeWorkspaceId = workspaceId;
764
  globalNarrativeLoading = true;
@@ -797,8 +804,8 @@ export const useServiceNarrative = (workspaceId?: string) => {
797
  if (data.type === 'WORKSPACE_INSIGHTS_READY') {
798
  if (!workspaceId || data.workspace_id === workspaceId) {
799
  console.log('[REPORTS] Service Narrative ready signal received, refreshing...');
800
- // DO NOT USE TRUE: Fetch cached insights without triggering new job
801
- fetchNarrative();
802
  }
803
  }
804
  };
 
661
  };
662
  }, []);
663
 
664
+ const fetchInsights = useCallback(async (force: boolean = false, ignoreLocalCache: boolean = false) => {
665
  const isNewWorkspace = lastLoadedAIWorkspaceId !== workspaceId;
666
  const isPending = (globalAIInsights as any)?.type === 'ASYNC_PENDING';
667
+
668
+ // Bypass check if forced, if it's a new workspace, if current state is pending, or if explicitly told to ignore local cache
669
+ const shouldFetch = force || isNewWorkspace || isPending || ignoreLocalCache || !globalAIInsights;
670
+
671
+ if (globalAIInsightsLoading || !shouldFetch) return;
672
 
673
  lastLoadedAIWorkspaceId = workspaceId;
674
  globalAIInsightsLoading = true;
 
713
  // If no workspaceId, we are likely in a context that uses the default/current
714
  if (!workspaceId || data.workspace_id === workspaceId) {
715
  console.log('[DASHBOARD] AI Insights ready signal received, refreshing...');
716
+ // Use ignoreLocalCache: true to fetch the newly ready cache without forcing re-generation
717
+ fetchInsights(false, true);
718
  }
719
  }
720
  };
 
759
  };
760
  }, []);
761
 
762
+ const fetchNarrative = useCallback(async (force: boolean = false, ignoreLocalCache: boolean = false) => {
763
  const isNewWorkspace = lastLoadedNarrativeWorkspaceId !== workspaceId;
764
  const isPending = (globalNarrative as any)?.status === 'PENDING';
765
+
766
+ const shouldFetch = force || isNewWorkspace || isPending || ignoreLocalCache || !globalNarrative;
767
+
768
+ if (globalNarrativeLoading || !shouldFetch) return;
769
 
770
  lastLoadedNarrativeWorkspaceId = workspaceId;
771
  globalNarrativeLoading = true;
 
804
  if (data.type === 'WORKSPACE_INSIGHTS_READY') {
805
  if (!workspaceId || data.workspace_id === workspaceId) {
806
  console.log('[REPORTS] Service Narrative ready signal received, refreshing...');
807
+ // Use ignoreLocalCache: true to fetch cached insights without triggering new job
808
+ fetchNarrative(false, true);
809
  }
810
  }
811
  };