Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix saved search loading issue #9034

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/9034.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Fix saved search loading issue ([#9034](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9034))
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
const { pathname } = useLocation();
const initalSearchComplete = useRef(false);
const [savedSearch, setSavedSearch] = useState<SavedSearch | undefined>(undefined);
const { savedSearch: savedSearchId, sort, interval } = useSelector((state) => state.discover);
const { savedSearch: savedSearchId, sort, interval, savedQuery } = useSelector(
(state) => state.discover

Check warning on line 88 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L87-L88

Added lines #L87 - L88 were not covered by tests
);
const indexPattern = useIndexPattern(services);
const {
data,
Expand Down Expand Up @@ -322,21 +324,34 @@
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 (!savedSearch) {
return;
const query =
savedSearchInstance.searchSource.getField('query') || data.query.queryString.getQuery();

const isEnhancementsEnabled = await uiSettings.get('query:enhancements:enabled');

Check warning on line 331 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L331

Added line #L331 was not covered by tests
if (isEnhancementsEnabled && query.dataset) {
let pattern = await data.indexPatterns.get(

Check warning on line 333 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L333

Added line #L333 was not covered by tests
query.dataset.id,
query.dataset.type !== 'INDEX_PATTERN'
);
if (!pattern) {
await data.query.queryString.getDatasetService().cacheDataset(query.dataset);
pattern = await data.indexPatterns.get(

Check warning on line 339 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L338-L339

Added lines #L338 - L339 were not covered by tests
query.dataset.id,
query.dataset.type !== 'INDEX_PATTERN'
);
savedSearchInstance.searchSource.setField('index', pattern);

Check warning on line 343 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L343

Added line #L343 was not covered by tests
}
}

// sync initial app filters from savedObject to filterManager
const filters = cloneDeep(savedSearchInstance.searchSource.getOwnField('filter'));
const query =
savedSearchInstance.searchSource.getField('query') ||
data.query.queryString.getDefaultQuery();
const actualFilters = [];

if (filters !== undefined) {
let actualFilters: any[] = [];

Check warning on line 350 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L350

Added line #L350 was not covered by tests

if (savedQuery) {
actualFilters = data.query.filterManager.getFilters();

Check warning on line 353 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L353

Added line #L353 was not covered by tests
} else if (filters !== undefined) {
const result = typeof filters === 'function' ? filters() : filters;
if (result !== undefined) {
actualFilters.push(...(Array.isArray(result) ? result : [result]));
Expand All @@ -345,6 +360,7 @@

filterManager.setAppFilters(actualFilters);
data.query.queryString.setQuery(query);
setSavedSearch(savedSearchInstance);

Check warning on line 363 in src/plugins/discover/public/application/view_components/utils/use_search.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/discover/public/application/view_components/utils/use_search.ts#L363

Added line #L363 was not covered by tests

if (savedSearchInstance?.id) {
chrome.recentlyAccessed.add(
Expand All @@ -357,8 +373,6 @@
);
}
})();

return () => {};
// This effect will only run when getSavedSearchById is called, which is
// only called when the component is first mounted.
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
Loading