diff --git a/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx b/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx index 99016cbe2fe46..2f906127b9229 100644 --- a/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx +++ b/src/plugins/dashboard/public/application/hooks/use_dashboard_app_state.test.tsx @@ -70,6 +70,7 @@ const createDashboardAppStateServices = () => { const data = dataPluginMock.createStartContract(); data.query.filterManager.getUpdates$ = jest.fn().mockImplementation(() => of(void 0)); + data.query.filterManager.getFilters = jest.fn().mockImplementation(() => []); data.query.queryString.getUpdates$ = jest.fn().mockImplementation(() => of({})); data.query.timefilter.timefilter.getTimeUpdate$ = jest.fn().mockImplementation(() => of(void 0)); data.query.timefilter.timefilter.getRefreshIntervalUpdate$ = jest diff --git a/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts b/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts index 24d891c658056..22af4f5d3a022 100644 --- a/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts +++ b/src/plugins/dashboard/public/application/lib/convert_dashboard_state.ts @@ -9,6 +9,13 @@ import _ from 'lodash'; import type { KibanaExecutionContext } from '@kbn/core/public'; import { ControlGroupInput } from '@kbn/controls-plugin/public'; +import { + compareFilters, + isFilterPinned, + migrateFilter, + COMPARE_ALL_OPTIONS, + type Filter, +} from '@kbn/es-query'; import { DashboardSavedObject } from '../../saved_dashboards'; import { getTagsFromSavedDashboard, migrateAppState } from '.'; import { EmbeddablePackageState, ViewMode } from '../../services/embeddable'; @@ -110,11 +117,20 @@ export const stateToDashboardContainerInput = ({ query, title, timeRestore, + filters: dashboardFilters, } = dashboardState; return { refreshConfig: timefilter.getRefreshInterval(), - filters: filterManager.getFilters(), + filters: filterManager + .getFilters() + .filter( + (filter) => + isFilterPinned(filter) || + dashboardFilters.some((dashboardFilter) => + filtersAreEqual(migrateFilter(_.cloneDeep(dashboardFilter)), filter) + ) + ), isFullScreenMode: fullScreenMode, id: savedDashboard.id || '', dashboardCapabilities, @@ -136,6 +152,9 @@ export const stateToDashboardContainerInput = ({ }; }; +const filtersAreEqual = (first: Filter, second: Filter) => + compareFilters(first, second, { ...COMPARE_ALL_OPTIONS, state: false }); + /** * Converts a given dashboard state object to raw dashboard state. This is useful for sharing, and session restoration, as * they require panels to be formatted as an array.