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): sync query in insightLogic on display-option saves (#66070)

Sam Penningtona0313dad7736
diff --git a/frontend/src/scenes/insights/insightLogic.test.ts b/frontend/src/scenes/insights/insightLogic.test.ts
index 5975962e19c8..722b1719626a 100644
--- a/frontend/src/scenes/insights/insightLogic.test.ts
+++ b/frontend/src/scenes/insights/insightLogic.test.ts
@@ -4,6 +4,7 @@ import { router } from 'kea-router'
import { expectLogic, partial, truth } from 'kea-test-utils'
import api from 'lib/api'
+import { objectsEqual } from 'lib/utils/objects'
import 'lib/constants'
import { dashboardLogic } from 'scenes/dashboard/dashboardLogic'
import { savedInsightsLogic } from 'scenes/saved-insights/savedInsightsLogic'
@@ -695,6 +696,32 @@ describe('insightLogic', () => {
})
})
+ it('updates query and savedInsight.query on renameInsightSuccess (display-option save)', async () => {
+ const updatedQuery: InsightVizNode = {
+ kind: NodeKind.InsightVizNode,
+ source: {
+ kind: NodeKind.TrendsQuery,
+ series: [],
+ trendsFilter: { showLegend: true },
+ },
+ }
+ const originalResult = logic.values.insight.result
+
+ insightsModel.actions.renameInsightSuccess(
+ insightModelWith({
+ id: 42,
+ short_id: Insight42,
+ query: updatedQuery,
+ result: null, // display-option PATCHes don't recompute — server returns null
+ })
+ )
+
+ // query updated, existing result preserved (not blanked by the null in the response)
+ expect(objectsEqual(logic.values.insight.query, updatedQuery)).toBe(true)
+ expect(logic.values.insight.result).toEqual(originalResult)
+ expect(objectsEqual(logic.values.savedInsight.query, updatedQuery)).toBe(true)
+ })
+
it('does not react to rename of a different insight', async () => {
await expectLogic(logic, () => {
insightsModel.actions.renameInsightSuccess(
diff --git a/frontend/src/scenes/insights/insightLogic.tsx b/frontend/src/scenes/insights/insightLogic.tsx
index 69d222b757bf..85458175c0a1 100644
--- a/frontend/src/scenes/insights/insightLogic.tsx
+++ b/frontend/src/scenes/insights/insightLogic.tsx
@@ -52,6 +52,8 @@ import {
import {
AccessControlLevel,
AccessControlResourceType,
+ DashboardTile,
+ DashboardType,
InsightLogicProps,
InsightShortId,
ItemMode,
@@ -305,8 +307,12 @@ export const insightLogic: LogicWrapper<insightLogicType> = kea<insightLogicType
// Note: setInsightMetadata state updates are handled by the loader
setInsightMetadataLocal: (state, { metadataUpdate }) => ({ ...state, ...metadataUpdate }),
[dashboardsModel.actionTypes.updateDashboardInsight]: (
- state,
- { insight, extraDashboardIds, sourceDashboardId }
+ state: Partial<QueryBasedInsightModel>,
+ {
+ insight,
+ extraDashboardIds,
+ sourceDashboardId,
+ }: { insight: QueryBasedInsightModel; extraDashboardIds?: number[]; sourceDashboardId?: number }
) => {
// Dashboard refresh responses merge that dashboard's filters into `query`; only the embedded
// insight for that dashboard (`props.dashboardId`) should apply them. Other dashboards or
@@ -328,25 +334,40 @@ export const insightLogic: LogicWrapper<insightLogicType> = kea<insightLogicType
},
[insightsModel.actionTypes.renameInsightSuccess]: (state, { item }) => {
if (item.id === state.id) {
- return { ...state, name: item.name, description: item.description }
+ // Also sync query (display-option saves); preserve result — bare PATCHes return result: null.
+ return {
+ ...state,
+ name: item.name,
+ description: item.description,
+ query: (item.query ?? state.query) as QueryBasedInsightModel['query'],
+ }
}
return state
},
- [insightsModel.actionTypes.insightsAddedToDashboard]: (state, { dashboardId, insightIds }) => {
- if (insightIds.includes(state.id)) {
+ [insightsModel.actionTypes.insightsAddedToDashboard]: (
+ state: Partial<QueryBasedInsightModel>,
+ { dashboardId, insightIds }: { dashboardId: number; insightIds: number[] }
+ ) => {
+ if (state.id != null && insightIds.includes(state.id)) {
return { ...state, dashboards: [...(state.dashboards || []), dashboardId] }
}
return state
},
- [dashboardsModel.actionTypes.tileRemovedFromDashboard]: (state, { tile, dashboardId }) => {
- if (tile.insight?.id === state.id) {
- return { ...state, dashboards: state.dashboards?.filter((d) => d !== dashboardId) }
+ [dashboardsModel.actionTypes.tileRemovedFromDashboard]: (
+ state: Partial<QueryBasedInsightModel>,
+ { tile, dashboardId }: { tile?: DashboardTile<QueryBasedInsightModel>; dashboardId?: number }
+ ) => {
+ if (tile?.insight?.id === state.id) {
+ return { ...state, dashboards: state.dashboards?.filter((d: number) => d !== dashboardId) }
}
return state
},
- [dashboardsModel.actionTypes.deleteDashboardSuccess]: (state, { dashboard }) => {
+ [dashboardsModel.actionTypes.deleteDashboardSuccess]: (
+ state: Partial<QueryBasedInsightModel>,
+ { dashboard }: { dashboard: DashboardType<QueryBasedInsightModel> }
+ ) => {
const { id } = dashboard
- return { ...state, dashboards: state.dashboards?.filter((d) => d !== id) }
+ return { ...state, dashboards: state.dashboards?.filter((d: number) => d !== id) }
},
},
accessDeniedToInsight: [false, { setAccessDeniedToInsight: () => true }],
@@ -371,6 +392,10 @@ export const insightLogic: LogicWrapper<insightLogicType> = kea<insightLogicType
tags: insight.tags,
favorited: insight.favorited,
}),
+ [insightsModel.actionTypes.renameInsightSuccess]: (state, { item }) =>
+ item.id === state.id
+ ? { ...state, name: item.name, description: item.description, query: item.query ?? state.query }
+ : state,
},
],
insightLoading: [