diff --git a/src/plugins/discover/public/application/main/services/discover_app_state_container.ts b/src/plugins/discover/public/application/main/services/discover_app_state_container.ts index 3b1bf5102506a..ac170a99312cf 100644 --- a/src/plugins/discover/public/application/main/services/discover_app_state_container.ts +++ b/src/plugins/discover/public/application/main/services/discover_app_state_container.ts @@ -234,7 +234,7 @@ function getInitialState( services, }); return handleSourceColumnState( - savedSearch.id && (!appStateFromUrl || !Object.keys(appStateFromUrl).length) + savedSearch.id ? { ...defaultAppState } : { ...defaultAppState, diff --git a/src/plugins/discover/public/application/main/services/discover_saved_search_container.ts b/src/plugins/discover/public/application/main/services/discover_saved_search_container.ts index 11d976bd703eb..51850e7b77995 100644 --- a/src/plugins/discover/public/application/main/services/discover_saved_search_container.ts +++ b/src/plugins/discover/public/application/main/services/discover_saved_search_container.ts @@ -11,6 +11,9 @@ import { BehaviorSubject } from 'rxjs'; import type { DataView } from '@kbn/data-views-plugin/common'; import { SavedObjectSaveOpts } from '@kbn/saved-objects-plugin/public'; import { differenceWith, isEqual, toPairs } from 'lodash'; +import { DataViewSpec } from '@kbn/data-views-plugin/common'; +import { loadSavedSearch } from '../utils/load_saved_search'; +import { InternalStateContainer } from './discover_internal_state_container'; import { updateSavedSearch } from '../utils/update_saved_search'; import { addLog } from '../../../utils/add_log'; import { handleSourceColumnState } from '../../../utils/state_helpers'; @@ -31,6 +34,18 @@ export interface SavedSearchContainer { savedSearchPersisted$: BehaviorSubject; hasChanged$: BehaviorSubject; set: (savedSearch: SavedSearch) => SavedSearch; + load: ( + id: string, + { + internalStateContainer, + setError, + dataViewSpec, + }: { + internalStateContainer: InternalStateContainer; + setError: (e: Error) => void; + dataViewSpec?: DataViewSpec; + } + ) => Promise; get: () => SavedSearch; update: ( nextDataView: DataView | undefined, @@ -218,10 +233,32 @@ export function getSavedSearchContainer({ return resetUrl(get().id || ''); }; + const load = ( + id: string, + { + internalStateContainer, + setError, + dataViewSpec, + }: { + internalStateContainer: InternalStateContainer; + setError: (e: Error) => void; + dataViewSpec?: DataViewSpec; + } + ) => { + return loadSavedSearch(id, { + services, + appStateContainer, + dataViewSpec, + internalStateContainer, + setError, + }); + }; + return { savedSearch$, savedSearchPersisted$, hasChanged$, + load, set, reset, resetUrl, diff --git a/src/plugins/discover/public/application/main/services/discover_state.test.ts b/src/plugins/discover/public/application/main/services/discover_state.test.ts index cf185ec2fdf6e..8518f94f01c48 100644 --- a/src/plugins/discover/public/application/main/services/discover_state.test.ts +++ b/src/plugins/discover/public/application/main/services/discover_state.test.ts @@ -86,16 +86,22 @@ describe('Test discover state', () => { describe('Test discover initial state sort handling', () => { test('Non-empty sort in URL should not be overwritten by saved search sort', async () => { history = createBrowserHistory(); - history.push('/#?_a=(sort:!(!(order_date,desc)))'); + history.push('/#?_a=(sort:!(!(timestamp,desc)))'); + const savedSearch = { + ...savedSearchMockWithTimeField, + ...{ sort: [['bytes', 'desc']] }, + } as SavedSearch; state = getDiscoverStateContainer({ - savedSearch: { ...savedSearchMock, ...{ sort: [['bytes', 'desc']] } }, + savedSearch: undefined, services: discoverServiceMock, history, }); - // await state.setAppState({}, true); + state.savedSearchState.load = jest.fn(() => Promise.resolve(savedSearch)); + await state.actions.loadSavedSearch(savedSearch.id!, undefined, jest.fn()); + // await state.setAppState({}); const stopSync = state.appState.syncState().stop; - expect(state.appState.getState().sort).toEqual([['order_date', 'desc']]); + expect(state.appState.getState().sort).toEqual([['timestamp', 'desc']]); stopSync(); }); test('Empty sort in URL should use saved search sort for state', async () => { diff --git a/src/plugins/discover/public/application/main/services/discover_state.ts b/src/plugins/discover/public/application/main/services/discover_state.ts index b65ac43e1d4b7..be80539c4c881 100644 --- a/src/plugins/discover/public/application/main/services/discover_state.ts +++ b/src/plugins/discover/public/application/main/services/discover_state.ts @@ -42,7 +42,6 @@ import { getSavedSearchContainer, SavedSearchContainer } from './discover_saved_ import { DiscoverServices } from '../../../build_services'; import { DISCOVER_APP_LOCATOR, DiscoverAppLocatorParams } from '../../../locator'; import { DiscoverSearchSessionManager } from './discover_search_session'; -import { loadSavedSearch } from '../utils/load_saved_search'; export interface AppStateUrl extends Omit { /** @@ -123,7 +122,7 @@ export interface DiscoverStateContainer { id: string, dataViewSpec: DataViewSpec | undefined, onError: (e: Error) => void - ) => void; + ) => Promise; loadNewSavedSearch: ( dataViewSpec: DataViewSpec | undefined, onError: (e: Error) => void @@ -267,9 +266,7 @@ export function getDiscoverStateContainer({ if (appStateContainer.isEmptyURL()) { appStateContainer.set({}); } - const currentSavedSearch = await loadSavedSearch(id, { - services, - appStateContainer, + const currentSavedSearch = await savedSearchContainer.load(id, { internalStateContainer, setError: onError, dataViewSpec,