Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(dashboard-v2): reconcile optimistic patches from the PATCH response (#11947)

Ashwin Bhatkal124d76068c72
diff --git a/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/__tests__/useOptimisticPatch.test.ts b/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/__tests__/useOptimisticPatch.test.ts
index 520638fe91..fbf224152e 100644
--- a/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/__tests__/useOptimisticPatch.test.ts
+++ b/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/__tests__/useOptimisticPatch.test.ts
@@ -100,8 +100,23 @@ describe('useOptimisticPatch', () => {
expect(queryClient.setQueryData).not.toHaveBeenCalled();
});
- it('onSettled invalidates the dashboard query to reconcile', () => {
- captured.options.onSettled();
- expect(queryClient.invalidateQueries).toHaveBeenCalledWith(QUERY_KEY);
+ it('onSuccess reconciles the cache from the PATCH response without a refetch', () => {
+ const response = {
+ data: { spec: { display: { name: 'B' } } },
+ status: 'success',
+ };
+ captured.options.onSuccess(response);
+
+ expect(queryClient.setQueryData).toHaveBeenCalledWith(
+ QUERY_KEY,
+ expect.any(Function),
+ );
+ // The updater merges the server data into the existing envelope.
+ const updater = queryClient.setQueryData.mock.calls[0][1];
+ expect(
+ updater({ data: { spec: { display: { name: 'A' } } }, foo: 1 }),
+ ).toStrictEqual({ data: { spec: { display: { name: 'B' } } }, foo: 1 });
+ // No follow-up GET (that could read stale data and flicker the UI back).
+ expect(queryClient.invalidateQueries).not.toHaveBeenCalled();
});
});
diff --git a/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/useOptimisticPatch.ts b/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/useOptimisticPatch.ts
index 7376b4df38..36484288d7 100644
--- a/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/useOptimisticPatch.ts
+++ b/frontend/src/pages/DashboardPageV2/DashboardContainer/hooks/useOptimisticPatch.ts
@@ -26,7 +26,9 @@ export interface UseOptimisticPatch {
/**
* Central optimistic mutation for V2 dashboard spec edits: writes the ops to the
- * cached dashboard immediately, rolls back on error, reconciles on settle.
+ * cached dashboard immediately, rolls back on error, and reconciles from the PATCH
+ * response (which returns the updated dashboard) rather than a follow-up GET — an
+ * immediate refetch can read stale, pre-mutation data and flicker the UI back.
* `dashboardId` defaults to the edit-context store; the panel editor passes its own.
*/
export function useOptimisticPatch(
@@ -60,8 +62,10 @@ export function useOptimisticPatch(
queryClient.setQueryData(queryKey, context.previous);
}
},
- onSettled: () => {
- void queryClient.invalidateQueries(queryKey);
+ onSuccess: (response) => {
+ queryClient.setQueryData<GetDashboardV2200>(queryKey, (prev) =>
+ prev ? { ...prev, data: response.data } : (response as GetDashboardV2200),
+ );
},
});