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): carry unsaved filter edits into edit mode (#103592)

Thomas Obermüller30b77e611697
diff --git a/frontend/src/scenes/insights/InsightPageHeader.spec.tsx b/frontend/src/scenes/insights/InsightPageHeader.spec.tsx
index c4169379645e..c81bb74ed0c5 100644
--- a/frontend/src/scenes/insights/InsightPageHeader.spec.tsx
+++ b/frontend/src/scenes/insights/InsightPageHeader.spec.tsx
@@ -1,15 +1,18 @@
import '@testing-library/jest-dom'
-import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { BindLogic } from 'kea'
+import { router } from 'kea-router'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import { urls } from 'scenes/urls'
import { useMocks } from '~/mocks/jest'
-import { Node } from '~/queries/schema/schema-general'
+import { InsightVizNode, Node, NodeKind } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
import { AccessControlLevel, InsightShortId, QueryBasedInsightModel, ItemMode } from '~/types'
+import { insightDataLogic } from './insightDataLogic'
import { insightLogic } from './insightLogic'
import { InsightPageHeader } from './InsightPageHeader'
import { insightSceneLogic } from './insightSceneLogic'
@@ -197,6 +200,68 @@ describe('InsightPageHeader', () => {
)
})
+ describe('unsaved view-mode edits carried into edit mode', () => {
+ // Spying on router.actions.push (rather than asserting post-navigation state) keeps this
+ // test at the level of the code the fix actually changed, without booting the fuller
+ // router/upgradeQuery chain insightSceneLogic.test.ts already covers.
+ let pushSpy: jest.SpyInstance
+
+ beforeEach(() => {
+ pushSpy = jest.spyOn(router.actions, 'push').mockImplementation(() => ({ type: 'noop' }) as any)
+ })
+
+ afterEach(() => {
+ pushSpy.mockRestore()
+ })
+
+ function makeTrendsQuery(compare: boolean): InsightVizNode {
+ return {
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
+ compareFilter: { compare },
+ },
+ } as InsightVizNode
+ }
+
+ it('includes the current query in the edit URL when it differs from the saved insight', () => {
+ const savedQuery = makeTrendsQuery(false)
+ const insight = makeInsight({ query: savedQuery })
+ renderHeader({ insightMode: ItemMode.View, dashboardItemId: SAVED_INSIGHT_ID, insight })
+
+ // The rendered header already mounted its own insightDataLogic instance — reuse it
+ // rather than mounting a second one, which desyncs the reducer state Kea expects.
+ const dataLogic = insightDataLogic.findMounted({ dashboardItemId: SAVED_INSIGHT_ID, doNotLoad: true })
+ expect(dataLogic).not.toBeNull()
+
+ const modifiedQuery = makeTrendsQuery(true)
+ act(() => {
+ dataLogic!.actions.setQuery(modifiedQuery)
+ })
+
+ fireEvent.click(queryByAttr('insight-edit-button')!)
+
+ expect(pushSpy).toHaveBeenCalledTimes(1)
+ const pushedUrl = pushSpy.mock.calls[0][0] as string
+ expect(pushedUrl).toContain(`#q=${encodeURIComponent(JSON.stringify(modifiedQuery))}`)
+ })
+
+ it('pushes the same edit URL as before when there is nothing unsaved to carry over', () => {
+ const savedQuery = makeTrendsQuery(false)
+ const insight = makeInsight({ query: savedQuery })
+ renderHeader({ insightMode: ItemMode.View, dashboardItemId: SAVED_INSIGHT_ID, insight })
+
+ fireEvent.click(queryByAttr('insight-edit-button')!)
+
+ // No #q= means the editor falls back to loading the saved insight's own query, same as
+ // it always has — this asserts the URL is byte-for-byte what insightEdit() alone builds,
+ // so the saved query can't have been dropped by this change.
+ expect(pushSpy).toHaveBeenCalledTimes(1)
+ expect(pushSpy.mock.calls[0][0]).toEqual(urls.insightEdit(SAVED_INSIGHT_ID))
+ })
+ })
+
describe('alert tool', () => {
it('is inactive when the query type does not support alerts', () => {
const insight = makeInsight({
diff --git a/frontend/src/scenes/insights/InsightPageHeader.tsx b/frontend/src/scenes/insights/InsightPageHeader.tsx
index b70f2ed32146..66a71894d9b7 100644
--- a/frontend/src/scenes/insights/InsightPageHeader.tsx
+++ b/frontend/src/scenes/insights/InsightPageHeader.tsx
@@ -208,11 +208,12 @@ export function InsightPageHeader({ insightLogicProps }: { insightLogicProps: In
})
)
} else if (insight.short_id) {
- const editUrl = dashboardId
- ? combineUrl(urls.insightEdit(insight.short_id), {
- dashboard: dashboardId,
- }).url
- : urls.insightEdit(insight.short_id)
+ const editUrl = combineUrl(
+ urls.insightEdit(insight.short_id),
+ dashboardId ? { dashboard: dashboardId } : {},
+ // Carry unsaved view-mode filter edits into the editor so they can be saved
+ queryChanged ? { q: JSON.stringify(query) } : {}
+ ).url
push(editUrl)
} else {
setInsightMode(ItemMode.Edit, null)