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(dashboards): skip tile reload on edit mode cancel without preview (#76535)

Thomas Obermüller1d248ab9de2f
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
index ca55593bc3b4..22862b35284d 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
@@ -11,6 +11,7 @@ import { DASHBOARD_WIDGET_FETCH_ERROR_MESSAGE } from '@posthog/products-dashboar
import api from 'lib/api'
import { FEATURE_FLAGS } from 'lib/constants'
import { dayjs, now } from 'lib/dayjs'
+import * as featureFlagLib from 'lib/logic/featureFlagLogic'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { DashboardEventSource, eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { addInsightToDashboardLogic } from 'scenes/dashboard/addInsightToDashboardModalLogic'
@@ -845,6 +846,72 @@ describe('dashboardLogic', () => {
restoreSpy.mockRestore()
})
+ it('discarding after a previewed filter change reloads tiles', async () => {
+ await expectLogic(logic).toFinishAllListeners()
+
+ await expectLogic(logic, () => {
+ logic.actions.setDashboardMode(DashboardMode.Edit, DashboardEventSource.DashboardFilters)
+ logic.actions.setDates('-14d', null)
+ }).toFinishAllListeners()
+
+ await expectLogic(logic, () => {
+ logic.actions.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges)
+ })
+ .toDispatchActions([
+ // anchor at the discard dispatch, so the refresh matched below is the
+ // discard-triggered one and not an earlier (initial load / preview) one
+ logic.actionCreators.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges),
+ 'refreshDashboardItems',
+ ])
+ .toFinishAllListeners()
+ })
+
+ it('discarding without a previewed filter change does not reload tiles', async () => {
+ await expectLogic(logic).toFinishAllListeners()
+
+ await expectLogic(logic, () => {
+ logic.actions.setDashboardMode(DashboardMode.Edit, DashboardEventSource.SceneCommonButtons)
+ }).toFinishAllListeners()
+
+ await expectLogic(logic, () => {
+ logic.actions.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges)
+ })
+ .toDispatchActions([
+ // anchor at the discard dispatch, so only actions after it are considered
+ logic.actionCreators.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges),
+ ])
+ .toFinishAllListeners()
+ .toNotHaveDispatchedActions(['refreshDashboardItems'])
+ })
+
+ it('discarding an unapplied filter edit above the auto-preview limit does not reload tiles', async () => {
+ // The skip-reload check relies on unpreviewed edits never reaching the URL:
+ // when the dashboard is over the auto-preview limit, filter edits stay
+ // intermittent (no URL write, no refresh) until "Apply filters" is clicked.
+ const payloadSpy = jest.spyOn(featureFlagLib, 'getFeatureFlagPayload').mockReturnValue(1)
+
+ await expectLogic(logic).toFinishAllListeners()
+ expect(logic.values.canAutoPreview).toBe(false)
+
+ await expectLogic(logic, () => {
+ logic.actions.setDashboardMode(DashboardMode.Edit, DashboardEventSource.DashboardFilters)
+ logic.actions.setDates('-14d', null)
+ }).toFinishAllListeners()
+
+ expect(router.values.searchParams[dashboardUtils.SEARCH_PARAM_FILTERS_KEY]).toBeUndefined()
+
+ await expectLogic(logic, () => {
+ logic.actions.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges)
+ })
+ .toDispatchActions([
+ logic.actionCreators.setDashboardMode(null, DashboardEventSource.DashboardHeaderDiscardChanges),
+ ])
+ .toFinishAllListeners()
+ .toNotHaveDispatchedActions(['refreshDashboardItems'])
+
+ payloadSpy.mockRestore()
+ })
+
it('filter edit source clears layout edit mode', async () => {
await expectLogic(logic).toFinishAllListeners()
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index c943d7a0fdea..e48efe73b8e8 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -1545,12 +1545,14 @@ export const dashboardLogic = kea<dashboardLogicType>([
values.dashboard?.tiles
) {
// layout changes were discarded so need to reset to original state
- const restoredTiles = values.dashboard?.tiles?.map((tile) => ({
- ...tile,
- layouts: values.dashboardLayouts?.[tile.id],
- }))
-
- values.dashboard.tiles = restoredTiles
+ // (as a new object — selectors only recompute when the reference changes)
+ return {
+ ...values.dashboard,
+ tiles: values.dashboard.tiles.map((tile) => ({
+ ...tile,
+ layouts: values.dashboardLayouts?.[tile.id],
+ })),
+ }
}
return values.dashboard
@@ -3973,13 +3975,27 @@ export const dashboardLogic = kea<dashboardLogicType>([
} else if (source === DashboardEventSource.DashboardHeaderDiscardChanges) {
// reset filters to that before previewing
actions.resetIntermittentFilters()
- actions.restoreUrlStateAtEditModeEntry(values.urlSearchParamsAtEditModeEntry)
- // reset tile data by reloading dashboard
- actions.refreshDashboardItems({
- action: RefreshDashboardItemsAction.Preview,
- forceRefresh: false,
- })
+ // Previews route filters/variables through the URL before refreshing, so tile data
+ // always matches the current URL state. Restoring the snapshot therefore only
+ // requires a reload when it actually changes that state — if no preview ran during
+ // this edit session, the data on screen is already correct.
+ const snapshot = values.urlSearchParamsAtEditModeEntry
+ const urlStateChangedSinceEditModeEntry =
+ !equal(values.urlFilters, parseURLFilters({ [SEARCH_PARAM_FILTERS_KEY]: snapshot?.filters })) ||
+ !equal(
+ parseURLVariables(router.values.searchParams),
+ parseURLVariables({ [SEARCH_PARAM_QUERY_VARIABLES_KEY]: snapshot?.variables })
+ )
+ actions.restoreUrlStateAtEditModeEntry(snapshot)
+
+ if (urlStateChangedSinceEditModeEntry) {
+ // reset tile data by reloading dashboard
+ actions.refreshDashboardItems({
+ action: RefreshDashboardItemsAction.Preview,
+ forceRefresh: false,
+ })
+ }
// also reset layout to that we stored in dashboardLayouts
// this is done in the reducer for dashboard