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): stop an empty filter override reading as an active one (#91560)

Thomas Obermüller1fd03ae42dd1
diff --git a/frontend/src/scenes/dashboard/DashboardOverridesBanner.tsx b/frontend/src/scenes/dashboard/DashboardOverridesBanner.tsx
index 676565582060..ae5673aae2e0 100644
--- a/frontend/src/scenes/dashboard/DashboardOverridesBanner.tsx
+++ b/frontend/src/scenes/dashboard/DashboardOverridesBanner.tsx
@@ -9,10 +9,10 @@ import { DashboardMode } from '~/types'
import { dashboardLogic } from './dashboardLogic'
export const DashboardOverridesBanner = (): JSX.Element | null => {
- const { dashboardMode, urlFilters, cancellingPreview } = useValues(dashboardLogic)
+ const { dashboardMode, hasUrlFilters, cancellingPreview } = useValues(dashboardLogic)
const { setDashboardMode } = useActions(dashboardLogic)
- if (dashboardMode === DashboardMode.Edit || Object.keys(urlFilters).length === 0) {
+ if (dashboardMode === DashboardMode.Edit || !hasUrlFilters) {
return null
}
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.test.ts b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
index eb592df4e6d8..ea3cc2b0b4aa 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardLogic.test.ts
@@ -1165,6 +1165,46 @@ describe('dashboardLogic', () => {
})
})
+ describe('url filter overrides', () => {
+ const PROPERTY_OVERRIDE = [{ key: '$browser', value: 'Chrome', type: 'event' }]
+
+ const openWithUrlFilters = async (urlFilters: Record<string, any>): Promise<void> => {
+ logic.unmount()
+ router.actions.push('/dashboard/5', {
+ [dashboardUtils.SEARCH_PARAM_FILTERS_KEY]: JSON.stringify(urlFilters),
+ })
+ logic = dashboardLogic({ id: 5 })
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+ }
+
+ // The overrides banner renders off hasUrlFilters. An override that constrains nothing still
+ // has keys, so testing for key presence announces overrides on a dashboard that is showing
+ // exactly its saved state.
+ const activeOverrideCases: [string, Record<string, any>, boolean][] = [
+ ['a date override is active', { date_from: '-7d', date_to: null }, true],
+ ['a property override is active', { properties: PROPERTY_OVERRIDE }, true],
+ ['properties cleared to empty is not active', { properties: [] }, false],
+ ]
+
+ it.each(activeOverrideCases)('%s', async (_name, urlFilters, expected) => {
+ await openWithUrlFilters(urlFilters)
+
+ expect(logic.values.hasUrlFilters).toBe(expected)
+ })
+
+ it('drops the url param when the last filter is cleared', async () => {
+ await openWithUrlFilters({ properties: PROPERTY_OVERRIDE })
+ expect(router.values.searchParams[dashboardUtils.SEARCH_PARAM_FILTERS_KEY]).not.toBeUndefined()
+
+ await expectLogic(logic, () => {
+ logic.actions.setProperties([])
+ }).toFinishAllListeners()
+
+ expect(router.values.searchParams[dashboardUtils.SEARCH_PARAM_FILTERS_KEY]).toBeUndefined()
+ })
+ })
+
describe('hasUnsavedLayoutChanges selector', () => {
const moveFirstTile = (): void => {
const firstTile = logic.values.dashboard!.tiles[0]
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index 49044f805e5a..ec8992727152 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -133,6 +133,7 @@ import {
mergeBreakdownColorConfigs,
} from './dashboardBreakdownColors'
import { AUTO_REFRESH_INITIAL_INTERVAL_SECONDS } from './dashboardConstants'
+import { isDashboardFilterEmpty } from './dashboardFilterEmpty'
import {
BREAKPOINT_COLUMN_COUNTS,
DASHBOARD_MIN_REFRESH_INTERVAL_MINUTES,
@@ -153,6 +154,7 @@ import {
parseURLFilters,
parseURLVariables,
runWithLimit,
+ searchParamsWithUrlFilters,
shouldSharedDashboardAutoForceForStaleTime,
shouldSnapshotUrlAtEditModeEntry,
} from './dashboardUtils'
@@ -2543,10 +2545,10 @@ export const dashboardLogic = kea<dashboardLogicType>([
(intermittentFilters: DashboardFilter) =>
Object.values(intermittentFilters).some((filter) => filter !== undefined),
],
- hasUrlFilters: [
- (s) => [s.urlFilters],
- (urlFilters: DashboardFilter) => Object.values(urlFilters).some((filter) => filter !== undefined),
- ],
+ // An override that constrains nothing still has keys — clearing the last property filter leaves
+ // `{"properties":[]}` in the URL. Counting keys reads that as an active override, so the dashboard
+ // announces overrides while showing exactly its saved state.
+ hasUrlFilters: [(s) => [s.urlFilters], (urlFilters: DashboardFilter) => !isDashboardFilterEmpty(urlFilters)],
showApplyFiltersBanner: [
(s) => [s.canAutoPreview, s.hasIntermittentFilters],
(canAutoPreview: boolean, hasIntermittentFilters: boolean) => !canAutoPreview && hasIntermittentFilters,
@@ -4774,13 +4776,9 @@ export const dashboardLogic = kea<dashboardLogicType>([
const urlFilters = parseURLFilters(currentLocation.searchParams)
const newUrlFilters: DashboardFilter = combineDashboardFilters(urlFilters, values.intermittentFilters)
- const newSearchParams = {
- ...currentLocation.searchParams,
- }
-
return [
currentLocation.pathname,
- { ...newSearchParams, ...encodeURLFilters(newUrlFilters) },
+ searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
currentLocation.hashParams,
]
},
@@ -4797,13 +4795,9 @@ export const dashboardLogic = kea<dashboardLogicType>([
properties,
}
- const newSearchParams = {
- ...currentLocation.searchParams,
- }
-
return [
currentLocation.pathname,
- { ...newSearchParams, ...encodeURLFilters(newUrlFilters) },
+ searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
currentLocation.hashParams,
]
},
@@ -4822,13 +4816,9 @@ export const dashboardLogic = kea<dashboardLogicType>([
explicitDate: explicit_date ?? values.intermittentFilters.explicitDate,
}
- const newSearchParams = {
- ...currentLocation.searchParams,
- }
-
return [
currentLocation.pathname,
- { ...newSearchParams, ...encodeURLFilters(newUrlFilters) },
+ searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
currentLocation.hashParams,
]
},
@@ -4845,13 +4835,9 @@ export const dashboardLogic = kea<dashboardLogicType>([
breakdown_filter,
}
- const newSearchParams = {
- ...currentLocation.searchParams,
- }
-
return [
currentLocation.pathname,
- { ...newSearchParams, ...encodeURLFilters(newUrlFilters) },
+ searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
currentLocation.hashParams,
]
},
@@ -4870,7 +4856,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- { ...currentLocation.searchParams, ...encodeURLFilters(newUrlFilters) },
+ searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
currentLocation.hashParams,
]
},
@@ -4889,7 +4875,7 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- { ...currentLocation.searchParams, ...encodeURLFilters(newUrlFilters) },
+ searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
currentLocation.hashParams,
]
},
diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.ts
index eaefb3049327..689c48c8f9ad 100644
--- a/frontend/src/scenes/dashboard/dashboardUtils.ts
+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts
@@ -32,6 +32,7 @@ import {
} from '~/types'
import { SHARED_DASHBOARD_AUTO_FORCE_IF_STALE_MINUTES } from './dashboardConstants'
+import { isDashboardFilterEmpty } from './dashboardFilterEmpty'
export function getInsightQueryError(insight: QueryBasedInsightModel): ApiError | null {
const queryStatus = insight.query_status
@@ -455,6 +456,23 @@ export const encodeURLFilters = (filters: DashboardFilter): Record<string, strin
return encodedFilters
}
+/**
+ * Search params for a dashboard filter change. A filter that constrains nothing drops the param instead
+ * of writing an empty one, so clearing the last filter doesn't leave the dashboard looking overridden on
+ * its next load. Spreading the encoded filter can't do this — it never removes a key already in the URL.
+ */
+export function searchParamsWithUrlFilters(
+ searchParams: Record<string, any>,
+ filters: DashboardFilter
+): Record<string, any> {
+ const nextSearchParams = { ...searchParams }
+ if (isDashboardFilterEmpty(filters)) {
+ delete nextSearchParams[SEARCH_PARAM_FILTERS_KEY]
+ return nextSearchParams
+ }
+ return { ...nextSearchParams, ...encodeURLFilters(filters) }
+}
+
export function combineDashboardFilters(...filters: DashboardFilter[]): DashboardFilter {
return filters.reduce((combined, filter) => {
Object.keys(filter).forEach((key) => {