From 27f7856b99ba602afb1b6721bae8ccf23ee0feb8 Mon Sep 17 00:00:00 2001 From: Suchit Sahoo Date: Fri, 4 Oct 2024 16:51:35 +0000 Subject: [PATCH] Fix Filter Manager Bugs in Saved Search Signed-off-by: Suchit Sahoo --- .../components/top_nav/get_top_nav_links.tsx | 2 ++ .../components/top_nav/open_search_panel.tsx | 1 + .../utils/state_management/discover_slice.tsx | 21 ++++++---------- .../view_components/utils/use_search.ts | 25 ++++++++++++------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/plugins/discover/public/application/components/top_nav/get_top_nav_links.tsx b/src/plugins/discover/public/application/components/top_nav/get_top_nav_links.tsx index d87e1e47f702..278612eb76f0 100644 --- a/src/plugins/discover/public/application/components/top_nav/get_top_nav_links.tsx +++ b/src/plugins/discover/public/application/components/top_nav/get_top_nav_links.tsx @@ -54,6 +54,7 @@ const getLegacyTopNavLinks = ( defaultMessage: 'New Search', }), run() { + query.filterManager.setFilters([]); // resetting the filters while we are loading a new search core.application.navigateToApp('discover', { path: '#/', }); @@ -306,6 +307,7 @@ export const getTopNavLinks = ( defaultMessage: 'New', }), run() { + query.filterManager.setFilters([]); // resetting the filters while we are loading a new search core.application.navigateToApp('discover', { path: '#/', }); diff --git a/src/plugins/discover/public/application/components/top_nav/open_search_panel.tsx b/src/plugins/discover/public/application/components/top_nav/open_search_panel.tsx index 6bb3a54e71f7..9acd1dc52a54 100644 --- a/src/plugins/discover/public/application/components/top_nav/open_search_panel.tsx +++ b/src/plugins/discover/public/application/components/top_nav/open_search_panel.tsx @@ -92,6 +92,7 @@ export function OpenSearchPanel({ onClose, makeUrl }: Props) { }, ]} onChoose={(id) => { + data.query.filterManager.setFilters([]); // resetting the filters application.navigateToApp('discover', { path: `#/view/${id}` }); onClose(); }} diff --git a/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx b/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx index 8bd0a8e0ea2f..442da1d2e25c 100644 --- a/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx +++ b/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx @@ -94,20 +94,13 @@ export const getPreloadedState = async ({ const indexPatternId = savedSearchInstance.searchSource.getField('index')?.id; // If the query enhancement is enabled don't add the indexpattern id to the root since they will be migrated into the _q state - if (config.get(QUERY_ENHANCEMENT_ENABLED_SETTING)) { - preloadedState.root = { - metadata: { - view: PLUGIN_ID, - }, - }; - } else { - preloadedState.root = { - metadata: { - indexPattern: indexPatternId, - view: PLUGIN_ID, - }, - }; - } + preloadedState.root = { + metadata: { + ...(indexPatternId && + !config.get(QUERY_ENHANCEMENT_ENABLED_SETTING) && { indexPattern: indexPatternId }), + view: PLUGIN_ID, + }, + }; savedSearchInstance.destroy(); // this instance is no longer needed, will create another one later } diff --git a/src/plugins/discover/public/application/view_components/utils/use_search.ts b/src/plugins/discover/public/application/view_components/utils/use_search.ts index 01ad93ebb7c4..2886ba811f62 100644 --- a/src/plugins/discover/public/application/view_components/utils/use_search.ts +++ b/src/plugins/discover/public/application/view_components/utils/use_search.ts @@ -12,7 +12,7 @@ import { cloneDeep } from 'lodash'; import { useLocation } from 'react-router-dom'; import { RequestAdapter } from '../../../../../inspector/public'; import { DiscoverViewServices } from '../../../build_services'; -import { search } from '../../../../../data/public'; +import { QueryState, search } from '../../../../../data/public'; import { validateTimeRange } from '../../helpers/validate_time_range'; import { updateSearchSource } from './update_search_source'; import { useIndexPattern } from './use_index_pattern'; @@ -352,19 +352,23 @@ export const useSearch = (services: DiscoverViewServices) => { useEffect(() => { (async () => { const savedSearchInstance = await getSavedSearchById(savedSearchId); + setSavedSearch(savedSearchInstance); - // if saved search does not exist, do not atempt to sync filters and query from savedObject - if (!savedSearchInstance) { + // if saved search does not exist, or the URL has filter tyhen don't sync the saved search state with that + if (!savedSearchInstance || !savedSearchId) { return; } - setSavedSearch(savedSearchInstance); - - // sync initial app filters from savedObject to filterManager - const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter')); + // Sync Query from the saved search const query = savedSearchInstance.searchSource.getField('query') || data.query.queryString.getDefaultQuery(); + + data.query.queryString.setQuery(query); + + // Sync Filters from the saved search + const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter')); + const actualFilters = []; if (filters !== undefined) { @@ -374,8 +378,11 @@ export const useSearch = (services: DiscoverViewServices) => { } } - filterManager.setAppFilters(actualFilters); - data.query.queryString.setQuery(query); + // Filters in URL are higher priority than the filters in saved search + const urlFilters = (osdUrlStateStorage.get('_q') as QueryState)?.filters ?? []; + if (!urlFilters || urlFilters.length === 0) { + filterManager.setAppFilters(actualFilters); + } if (savedSearchInstance?.id) { chrome.recentlyAccessed.add(