Skip to content

: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.

fix(insights): stop the insight editor auto-saving on a chart-type change (#105737)

Daniel Parker5158cf75d458
diff --git a/frontend/src/scenes/insights/insightDataLogic.test.ts b/frontend/src/scenes/insights/insightDataLogic.test.ts
index 3a0d9e3b3ba2..943b46e38d8d 100644
--- a/frontend/src/scenes/insights/insightDataLogic.test.ts
+++ b/frontend/src/scenes/insights/insightDataLogic.test.ts
@@ -678,7 +678,29 @@ describe('insightDataLogic', () => {
sceneLogic.mount()
sceneLogic.actions.setScene(Scene.Insight, undefined, {} as any)
const findMountedSpy = jest.spyOn(insightSceneLogic, 'findMounted').mockReturnValue({
- values: { insightLogicRef: { logic: { key: Insight42 } } },
+ values: { insightId: Insight42, dashboardId: null },
+ } as any)
+
+ try {
+ await expectLogic(logic, () => {
+ logic.actions.persistDisplayOptions(updatedQuery)
+ }).toFinishAllListeners()
+
+ expect(patchSpy).not.toHaveBeenCalled()
+ } finally {
+ findMountedSpy.mockRestore()
+ sceneLogic.unmount()
+ }
+ })
+
+ it('skips the PATCH even when insightSceneLogic has not yet rebuilt its insightLogicRef for this insight', async () => {
+ // insightLogicRef can lag insightId/dashboardId right after the scene mounts, because a
+ // separate listener rebuilds it asynchronously. This mocks that lag: the guard must not
+ // rely on insightLogicRef being present or already correct.
+ sceneLogic.mount()
+ sceneLogic.actions.setScene(Scene.Insight, undefined, {} as any)
+ const findMountedSpy = jest.spyOn(insightSceneLogic, 'findMounted').mockReturnValue({
+ values: { insightId: Insight42, dashboardId: null, insightLogicRef: null },
} as any)
try {
diff --git a/frontend/src/scenes/insights/insightDataLogic.tsx b/frontend/src/scenes/insights/insightDataLogic.tsx
index 4c97fcbf4621..5a1ed0bad071 100644
--- a/frontend/src/scenes/insights/insightDataLogic.tsx
+++ b/frontend/src/scenes/insights/insightDataLogic.tsx
@@ -101,9 +101,21 @@ import { insightUsageLogic } from './insightUsageLogic'
import { crushDraftQueryForLocalStorage, isQueryTooLarge } from './utils'
import { compareQuery, isDraftQueryWorthSaving } from './utils/queryUtils'
-export const isInsightSceneInstance = (props: InsightLogicProps): boolean =>
- sceneLogic.values.activeSceneId === Scene.Insight &&
- insightSceneLogic.findMounted()?.values.insightLogicRef?.logic.key === keyForInsightLogicProps('new')(props)
+// insightId/dashboardId are reducers set synchronously by the same action that matches the URL.
+// insightLogicRef is set by a separate listener that rebuilds and mounts a new logic instance, a
+// side effect that can still be running right after the scene mounts. Comparing against the ref
+// can read a stale value while insightId/dashboardId are already correct, letting a save through
+// while the editor is still showing this insight.
+export const isInsightSceneInstance = (props: InsightLogicProps): boolean => {
+ if (sceneLogic.values.activeSceneId !== Scene.Insight) {
+ return false
+ }
+ const sceneValues = insightSceneLogic.findMounted()?.values
+ return (
+ sceneValues?.insightId === props.dashboardItemId &&
+ (sceneValues?.dashboardId ?? null) === (props.dashboardId ?? null)
+ )
+}
const isMatchingSqlQuery = (
query: Node | null | undefined,