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): drop debounced filter edits after an insight type switch (#127004)

posthog[bot]e9ee3ab8a8f0
diff --git a/frontend/src/scenes/insights/insightVizDataLogic.test.ts b/frontend/src/scenes/insights/insightVizDataLogic.test.ts
index c8f4b4a00ba1..5105398f5c73 100644
--- a/frontend/src/scenes/insights/insightVizDataLogic.test.ts
+++ b/frontend/src/scenes/insights/insightVizDataLogic.test.ts
@@ -1065,6 +1065,22 @@ describe('insightVizDataLogic', () => {
})
})
+ it('drops a debounced update when the insight type changes before it applies', async () => {
+ await expectLogic(builtInsightDataLogic, () => {
+ builtInsightVizDataLogic.actions.updateInsightFilter({
+ formula: undefined,
+ formulas: undefined,
+ formulaNodes: [{ formula: '(A/B)*100' }],
+ })
+ builtInsightDataLogic.actions.setQuery({
+ kind: NodeKind.InsightVizNode,
+ source: funnelsQueryDefault,
+ } as Node)
+ }).toFinishAllListeners()
+
+ expect(builtInsightVizDataLogic.values.querySource).toEqual(funnelsQueryDefault)
+ })
+
it('clears the breakdown when switching to the Metric display', async () => {
await expectLogic(builtInsightDataLogic, () => {
builtInsightVizDataLogic.actions.updateBreakdownFilter({
diff --git a/frontend/src/scenes/insights/insightVizDataLogic.ts b/frontend/src/scenes/insights/insightVizDataLogic.ts
index 2a0e7c001848..5e07bceb1a4b 100644
--- a/frontend/src/scenes/insights/insightVizDataLogic.ts
+++ b/frontend/src/scenes/insights/insightVizDataLogic.ts
@@ -1409,6 +1409,14 @@ export type insightVizDataLogicType = MakeLogicType<
insightVizDataLogicMeta
>
+// A debounced update targets the query as it was at dispatch, so it is dropped if
+// the user discarded their changes or switched insight type while it waited.
+const trackPendingUpdate = (cache: Record<string, any>, getKind: () => NodeKind | undefined): (() => boolean) => {
+ const discardCount = cache.discardCount ?? 0
+ const kind = getKind()
+ return () => (cache.discardCount ?? 0) !== discardCount || getKind() !== kind
+}
+
export const insightVizDataLogic = kea<insightVizDataLogicType>([
props({} as InsightLogicProps),
key(keyForInsightLogicProps('new')),
@@ -2803,12 +2811,8 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
}
},
- // Discarding edits reverts the query to the saved version. A debounced filter
- // update (e.g. updateDateRange) that was dispatched just before the discard would
- // otherwise resolve afterwards and re-apply the discarded value on top of the
- // reverted query. Flag it so the in-flight debounce bails out instead.
cancelChanges: () => {
- cache.pendingFilterUpdateCancelled = true
+ cache.discardCount = (cache.discardCount ?? 0) + 1
},
// query source
@@ -2849,14 +2853,11 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
// query source properties
updateDateRange: async ({ dateRange, ignoreDebounce }, breakpoint) => {
- cache.pendingFilterUpdateCancelled = false
+ const isStale = trackPendingUpdate(cache, () => values.localQuerySource?.kind)
if (!ignoreDebounce) {
await breakpoint(300)
}
- // Changes were discarded while this debounce was pending — don't re-apply the
- // edited date range over the query that cancelChanges just reverted.
- if (cache.pendingFilterUpdateCancelled) {
- cache.pendingFilterUpdateCancelled = false
+ if (isStale()) {
return
}
posthog.capture('insight date range changed', { query_kind: values.querySource?.kind })
@@ -2888,13 +2889,21 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
})
},
updateBreakdownFilter: async ({ breakdownFilter }, breakpoint) => {
+ const isStale = trackPendingUpdate(cache, () => values.localQuerySource?.kind)
await breakpoint(500) // extra debounce time because of number input
+ if (isStale()) {
+ return
+ }
posthog.capture('insight breakdown changed', { query_kind: values.querySource?.kind })
const update: Partial<TrendsQuery> = { breakdownFilter: { ...values.breakdownFilter, ...breakdownFilter } }
actions.updateQuerySource(update)
},
updateCompareFilter: async ({ compareFilter }, breakpoint) => {
+ const isStale = trackPendingUpdate(cache, () => values.localQuerySource?.kind)
await breakpoint(500) // extra debounce time because of number input
+ if (isStale()) {
+ return
+ }
posthog.capture('insight compare changed', { query_kind: values.querySource?.kind })
const update: Partial<TrendsQuery> = { compareFilter: { ...values.compareFilter, ...compareFilter } }
actions.updateQuerySource(update)
@@ -2902,6 +2911,8 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
// insight filter
updateInsightFilter: async ({ insightFilter }, breakpoint) => {
+ const isStale = trackPendingUpdate(cache, () => values.localQuerySource?.kind)
+
// When an external save handler is wired (dashboard card), skip the debounce so
// rapid successive toggle clicks don't cancel each other and lose earlier changes.
if (!props.setQuery) {
@@ -2912,6 +2923,10 @@ export const insightVizDataLogic = kea<insightVizDataLogicType>([
return
}
+ if (isStale()) {
+ return
+ }
+
const filterProperty = filterKeyForQuery(values.localQuerySource)
actions.updateQuerySource({
[filterProperty]: { ...filterForQuery(values.localQuerySource), ...insightFilter },