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): preserve cleared saved filters (#94634)

Matt Pua7d822941c314
diff --git a/frontend/src/scenes/dashboard/dashboardLogic.tsx b/frontend/src/scenes/dashboard/dashboardLogic.tsx
index 61a34c3577ad..57386d0c2ba1 100644
--- a/frontend/src/scenes/dashboard/dashboardLogic.tsx
+++ b/frontend/src/scenes/dashboard/dashboardLogic.tsx
@@ -137,7 +137,6 @@ 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,
@@ -146,6 +145,7 @@ import {
SEARCH_PARAM_FILTERS_KEY,
SEARCH_PARAM_QUERY_VARIABLES_KEY,
combineDashboardFilters,
+ dashboardFilterOverrideChangesFilters,
encodeURLFilters,
encodeURLVariables,
getDashboardWidgetType,
@@ -1032,7 +1032,11 @@ export interface dashboardLogicMeta {
shouldUseStreaming: (featureFlags: FeatureFlagsSet) => boolean
canAutoPreview: (insightTiles: DashboardTile<QueryBasedInsightModel<Node<Record<string, any>>>>[]) => boolean
hasIntermittentFilters: (intermittentFilters: DashboardFilter) => boolean
- hasUrlFilters: (urlFilters: DashboardFilter) => boolean
+ hasUrlFilters: (
+ dashboard: DashboardType<QueryBasedInsightModel<Node<Record<string, any>>>> | null,
+ externalFilters: DashboardFilter,
+ urlFilters: DashboardFilter
+ ) => boolean
showApplyFiltersBanner: (canAutoPreview: boolean, hasIntermittentFilters: boolean) => boolean
urlFilters: (searchParams: Record<string, any>) => DashboardFilter
filtersOverrideForLoad: (externalFilters: DashboardFilter, urlFilters: DashboardFilter) => DashboardFilter
@@ -2585,7 +2589,18 @@ export const dashboardLogic = kea<dashboardLogicType>([
// 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)],
+ hasUrlFilters: [
+ (s) => [s.dashboard, s.externalFilters, s.urlFilters],
+ (
+ dashboard: DashboardType<QueryBasedInsightModel> | null,
+ externalFilters: DashboardFilter,
+ urlFilters: DashboardFilter
+ ) =>
+ dashboardFilterOverrideChangesFilters(
+ urlFilters,
+ combineDashboardFilters(dashboard?.persisted_filters || {}, externalFilters)
+ ),
+ ],
showApplyFiltersBanner: [
(s) => [s.canAutoPreview, s.hasIntermittentFilters],
(canAutoPreview: boolean, hasIntermittentFilters: boolean) => !canAutoPreview && hasIntermittentFilters,
@@ -4828,7 +4843,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
+ searchParamsWithUrlFilters(
+ currentLocation.searchParams,
+ newUrlFilters,
+ combineDashboardFilters(values.dashboard?.persisted_filters || {}, values.externalFilters)
+ ),
currentLocation.hashParams,
]
},
@@ -4847,7 +4866,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
+ searchParamsWithUrlFilters(
+ currentLocation.searchParams,
+ newUrlFilters,
+ combineDashboardFilters(values.dashboard?.persisted_filters || {}, values.externalFilters)
+ ),
currentLocation.hashParams,
]
},
@@ -4868,7 +4891,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
+ searchParamsWithUrlFilters(
+ currentLocation.searchParams,
+ newUrlFilters,
+ combineDashboardFilters(values.dashboard?.persisted_filters || {}, values.externalFilters)
+ ),
currentLocation.hashParams,
]
},
@@ -4887,7 +4914,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
+ searchParamsWithUrlFilters(
+ currentLocation.searchParams,
+ newUrlFilters,
+ combineDashboardFilters(values.dashboard?.persisted_filters || {}, values.externalFilters)
+ ),
currentLocation.hashParams,
]
},
@@ -4906,7 +4937,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
+ searchParamsWithUrlFilters(
+ currentLocation.searchParams,
+ newUrlFilters,
+ combineDashboardFilters(values.dashboard?.persisted_filters || {}, values.externalFilters)
+ ),
currentLocation.hashParams,
]
},
@@ -4925,7 +4960,11 @@ export const dashboardLogic = kea<dashboardLogicType>([
return [
currentLocation.pathname,
- searchParamsWithUrlFilters(currentLocation.searchParams, newUrlFilters),
+ searchParamsWithUrlFilters(
+ currentLocation.searchParams,
+ newUrlFilters,
+ combineDashboardFilters(values.dashboard?.persisted_filters || {}, values.externalFilters)
+ ),
currentLocation.hashParams,
]
},
diff --git a/frontend/src/scenes/dashboard/dashboardUtils.test.ts b/frontend/src/scenes/dashboard/dashboardUtils.test.ts
index 00d13f176581..23e1002a21a9 100644
--- a/frontend/src/scenes/dashboard/dashboardUtils.test.ts
+++ b/frontend/src/scenes/dashboard/dashboardUtils.test.ts
@@ -2,10 +2,21 @@ import api from 'lib/api'
import { ApiError } from 'lib/api-error'
import { dayjs } from 'lib/dayjs'
-import { DashboardPlacement, DashboardTile, DashboardType, InsightModel, QueryBasedInsightModel } from '~/types'
+import { BreakdownFilter } from '~/queries/schema/schema-general'
+import {
+ AnyPropertyFilter,
+ DashboardPlacement,
+ DashboardTile,
+ DashboardType,
+ InsightModel,
+ PropertyFilterType,
+ PropertyOperator,
+ QueryBasedInsightModel,
+} from '~/types'
import {
dashboardToSaveableTemplate,
+ searchParamsWithUrlFilters,
getDashboardTileDisplayName,
getInsightWithRetry,
isWidgetTileVisibleOnPlacement,
@@ -16,6 +27,48 @@ import {
shouldSharedDashboardAutoForceForStaleTime,
} from './dashboardUtils'
+describe('searchParamsWithUrlFilters', () => {
+ const propertyFilter: AnyPropertyFilter[] = [
+ {
+ key: '$browser',
+ value: 'Chrome',
+ type: PropertyFilterType.Event,
+ operator: PropertyOperator.Exact,
+ },
+ ]
+ const breakdownFilter: BreakdownFilter = { breakdown: '$browser', breakdown_type: 'event' }
+
+ it.each([
+ ['property filter', { properties: [] }, { properties: propertyFilter }],
+ ['breakdown', { breakdown_filter: null }, { breakdown_filter: breakdownFilter }],
+ ])('keeps an empty %s override that clears a saved value', (_name, filters, persistedFilters) => {
+ const searchParams = searchParamsWithUrlFilters({}, filters, persistedFilters)
+
+ expect(parseURLFilters(searchParams)).toEqual(filters)
+ })
+
+ it('removes an empty override when the dashboard has no saved filters', () => {
+ const searchParams = searchParamsWithUrlFilters(
+ { [SEARCH_PARAM_FILTERS_KEY]: JSON.stringify({ properties: propertyFilter }) },
+ { properties: [] }
+ )
+
+ expect(searchParams[SEARCH_PARAM_FILTERS_KEY]).toBeUndefined()
+ })
+
+ it('keeps an explicit date mode override', () => {
+ const searchParams = searchParamsWithUrlFilters({}, { explicitDate: false }, { explicitDate: true })
+
+ expect(parseURLFilters(searchParams)).toEqual({ explicitDate: false })
+ })
+
+ it('keeps an override that clears an external filter', () => {
+ const searchParams = searchParamsWithUrlFilters({}, { properties: [] }, { properties: propertyFilter })
+
+ expect(parseURLFilters(searchParams)).toEqual({ properties: [] })
+ })
+})
+
describe('getDashboardTileDisplayName', () => {
it('uses widget header title when no custom name is set', () => {
const tile: DashboardTile<QueryBasedInsightModel> = {
diff --git a/frontend/src/scenes/dashboard/dashboardUtils.ts b/frontend/src/scenes/dashboard/dashboardUtils.ts
index 650217b5fc2e..78bd31321fe7 100644
--- a/frontend/src/scenes/dashboard/dashboardUtils.ts
+++ b/frontend/src/scenes/dashboard/dashboardUtils.ts
@@ -1,3 +1,4 @@
+import { deepEqual as equal } from 'fast-equals'
import { ResponsiveLayouts } from 'react-grid-layout'
import { lemonToast } from '@posthog/lemon-ui'
@@ -32,7 +33,6 @@ 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
@@ -461,22 +461,40 @@ export const encodeURLFilters = (filters: DashboardFilter): Record<string, strin
}
/**
- * 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.
+ * An empty override must stay in the URL when it clears a saved dashboard filter. Without that explicit
+ * value, a reload restores the saved filter. Empty overrides on unfiltered dashboards can be removed.
*/
export function searchParamsWithUrlFilters(
searchParams: Record<string, any>,
- filters: DashboardFilter
+ filters: DashboardFilter,
+ persistedFilters: DashboardFilter = {}
): Record<string, any> {
const nextSearchParams = { ...searchParams }
- if (isDashboardFilterEmpty(filters)) {
+ if (!dashboardFilterOverrideChangesFilters(filters, persistedFilters)) {
delete nextSearchParams[SEARCH_PARAM_FILTERS_KEY]
return nextSearchParams
}
return { ...nextSearchParams, ...encodeURLFilters(filters) }
}
+export function dashboardFilterOverrideChangesFilters(
+ filters: DashboardFilter,
+ persistedFilters: DashboardFilter
+): boolean {
+ return Object.entries(filters).some(([key, value]) => {
+ const persistedValue = (persistedFilters as Record<string, unknown>)[key]
+ if (key === 'properties') {
+ const properties = Array.isArray(value) ? value : []
+ const persistedProperties = Array.isArray(persistedValue) ? persistedValue : []
+ return !equal(properties, persistedProperties)
+ }
+ if (value == null && persistedValue == null) {
+ return false
+ }
+ return !equal(value, persistedValue)
+ })
+}
+
export function combineDashboardFilters(...filters: DashboardFilter[]): DashboardFilter {
return filters.reduce((combined, filter) => {
Object.keys(filter).forEach((key) => {