:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(insights): only persist display options when the query changed (#66571)
Sam Pennington28735a6f3b2a
diff --git a/frontend/src/scenes/insights/insightDataLogic.test.ts b/frontend/src/scenes/insights/insightDataLogic.test.tsindex 2a6cf2427b3b..80c09dfd7b59 100644--- a/frontend/src/scenes/insights/insightDataLogic.test.ts+++ b/frontend/src/scenes/insights/insightDataLogic.test.ts@@ -8,6 +8,9 @@ import { expectLogic } from 'kea-test-utils'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { insightLogic } from 'scenes/insights/insightLogic'+import { insightSceneLogic } from 'scenes/insights/insightSceneLogic'+import { sceneLogic } from 'scenes/sceneLogic'+import { Scene } from 'scenes/sceneTypes'import { useMocks } from '~/mocks/jest'import { insightsModel } from '~/models/insightsModel'@@ -511,5 +514,34 @@ describe('insightDataLogic', () => {expect(patchSpy).toHaveBeenCalledTimes(1)})++ it('skips the PATCH when the query is unchanged from the saved insight', async () => {+ await expectLogic(logic, () => {+ logic.actions.persistDisplayOptions(baseQuery)+ }).toFinishAllListeners()++ expect(patchSpy).not.toHaveBeenCalled()+ })++ it('skips the PATCH while this insight is being edited in the insight scene', async () => {+ // Editing an insight opened from a dashboard reuses the tile's keyed logic, which wired+ // persistDisplayOptions as props.setQuery. The scene must persist only via explicit save.+ sceneLogic.mount()+ sceneLogic.actions.setScene(Scene.Insight, undefined, {} as any)+ const findMountedSpy = jest.spyOn(insightSceneLogic, 'findMounted').mockReturnValue({+ values: { insightLogicRef: { logic: { key: Insight42 } } },+ } as any)++ try {+ await expectLogic(logic, () => {+ logic.actions.persistDisplayOptions(updatedQuery)+ }).toFinishAllListeners()++ expect(patchSpy).not.toHaveBeenCalled()+ } finally {+ findMountedSpy.mockRestore()+ sceneLogic.unmount()+ }+ })})})diff --git a/frontend/src/scenes/insights/insightDataLogic.tsx b/frontend/src/scenes/insights/insightDataLogic.tsxindex 80743de8cb51..532e047b8e3a 100644--- a/frontend/src/scenes/insights/insightDataLogic.tsx+++ b/frontend/src/scenes/insights/insightDataLogic.tsx@@ -294,6 +294,15 @@ export const insightDataLogic = kea<insightDataLogicType>([listeners(({ actions, values, props }) => ({persistDisplayOptions: async ({ query }, breakpoint) => {+ // Never auto-persist while the user is editing this insight in the insight scene.+ // insightDataLogic is keyed `${shortId}/on-dashboard-${dashboardId}`, so an insight+ // opened from a dashboard shares its instance with the dashboard tile — which wired+ // props.setQuery to persistDisplayOptions. Without this guard, any edit in the scene+ // (a display toggle or removing a filter) would PATCH the insight before the user+ // clicks Save. Edits there must persist only through an explicit save.+ if (isInsightSceneInstance(props)) {+ return+ }// Debounce rapid clicks. insightDataLogic is keyed per insight, so breakpoint// only cancels concurrent saves for THIS insight — not unrelated tiles.await breakpoint(700)@@ -301,6 +310,13 @@ export const insightDataLogic = kea<insightDataLogicType>([if (!insightId) {return}+ // Only persist when the query actually differs from what's saved. The setQuery →+ // props.setQuery path fires for any InsightVizNode change, including programmatic+ // re-syncs (tile re-renders, results refreshes) that carry an unchanged query —+ // persisting those produces spurious saves and activity-log churn.+ if (objectsEqual(query, values.savedInsight.query)) {+ return+ }try {const updatedItem = await insightsApi.update(insightId, { query })// Drop the response if a newer save started while this request was in flight.