Skip to content

Commit

Permalink
Fix Loading and Editing Saved Searches in Discover
Browse files Browse the repository at this point in the history
Signed-off-by: Suchit Sahoo <[email protected]>
  • Loading branch information
LDrago27 committed Sep 24, 2024
1 parent 6ebc1e3 commit 3e8e756
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* under the License.
*/

import { QueryStringContract } from 'src/plugins/data/public/';
import { migrateLegacyQuery } from './migrate_legacy_query';
import { SearchSource, SearchSourceDependencies } from './search_source';
import { IndexPatternsContract } from '../../index_patterns/index_patterns';
Expand All @@ -52,10 +53,17 @@ import { SearchSourceFields } from './types';
* @public */
export const createSearchSource = (
indexPatterns: IndexPatternsContract,
queryStringService: QueryStringContract,
searchSourceDependencies: SearchSourceDependencies
) => async (searchSourceFields: SearchSourceFields = {}) => {
const fields = { ...searchSourceFields };

// When we load a saved search and the saved search contains a non index pattern data source this step creates the temperary index patterns and sets the appriopriate query
if (fields.query?.dataset && fields.query?.dataset?.type !== 'INDEX_PATTERN') {
await queryStringService.getDatasetService().cacheDataset(fields.query?.dataset);
// await queryStringService.setQuery(fields.query);
}

// hydrating index pattern
if (fields.index && typeof fields.index === 'string') {
fields.index = await indexPatterns.get(searchSourceFields.index as any);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@
* under the License.
*/

import { QueryStringContract } from 'src/plugins/data/public';
import { createSearchSource, SearchSource, SearchSourceDependencies } from './';
import { IndexPatternsContract } from '../../index_patterns/index_patterns';

export class SearchSourceService {
public setup() {}

public start(indexPatterns: IndexPatternsContract, dependencies: SearchSourceDependencies) {
public start(
indexPatterns: IndexPatternsContract,
queryStringService: QueryStringContract,
dependencies: SearchSourceDependencies
) {
return {
/**
* creates searchsource based on serialized search source fields
*/
create: createSearchSource(indexPatterns, dependencies),
create: createSearchSource(indexPatterns, queryStringService, dependencies),
/**
* creates an enpty search source
*/
Expand Down
8 changes: 7 additions & 1 deletion src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,19 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
df: dfService,
};

const queryString = getQueryService().queryString;

return {
aggs: this.aggsService.start({ fieldFormats, uiSettings }),
search,
showError: (e: Error) => {
this.searchInterceptor.showError(e);
},
searchSource: this.searchSourceService.start(indexPatterns, searchSourceDependencies),
searchSource: this.searchSourceService.start(
indexPatterns,
queryString,
searchSourceDependencies
),
__enhance: (enhancements: SearchEnhancements) => {
this.searchInterceptor = enhancements.searchInterceptor;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ export const getPreloadedStore = async (services: DataExplorerServices) => {

// If the url state is different from the current state, then we need to update the store
// the state should have a view property if it was loaded from the url
if (action === 'POP' && urlState.metadata?.view && !isEqual(urlState, currentState)) {
if (
(action === 'POP' || action === 'REPLACE') &&
urlState.metadata?.view &&
!isEqual(urlState, currentState)
) {
store.dispatch(hydrate(urlState as RootState));
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,13 @@ export const TopNav = ({ opts, showSaveQuery, isEnhancementsEnabled }: TopNavPro

if (savedSearch?.id) {
chrome.setBreadcrumbs([...getRootBreadcrumbs(), { text: savedSearch.title }]);
if (savedSearch.searchSource.getField('query')) {
data.query.queryString.setQuery(savedSearch.searchSource.getField('query'));
}
} else {
chrome.setBreadcrumbs([...getRootBreadcrumbs()]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [chrome, getUrlForApp, savedSearch?.id, savedSearch?.title]);

useEffect(() => {
Expand Down

0 comments on commit 3e8e756

Please sign in to comment.