From 07063ffdd7e983185f9b315fb028fb42ebbd82db Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Fri, 15 Nov 2024 13:00:17 -0800 Subject: [PATCH] [Discover] Dataset search on page load issues (#8871) (#8877) * Dataset search on page load issues Required a double click on search and then also potentially loading issue. Issue n/a * Clean up dependencies * Addresses issue * add some tests * Changeset file for PR #8871 created/updated --------- (cherry picked from commit 42df421f5a4cc3158132cf680a8bb4722bac92d6) Signed-off-by: Kawika Avilla Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/8871.yml | 2 + .../view_components/utils/use_search.test.tsx | 42 +++++++++++++++++++ .../view_components/utils/use_search.ts | 26 +++++------- 3 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 changelogs/fragments/8871.yml diff --git a/changelogs/fragments/8871.yml b/changelogs/fragments/8871.yml new file mode 100644 index 000000000000..032a928fd5c0 --- /dev/null +++ b/changelogs/fragments/8871.yml @@ -0,0 +1,2 @@ +fix: +- Search on page load out of sync state when clicking submit. ([#8871](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8871)) \ No newline at end of file diff --git a/src/plugins/discover/public/application/view_components/utils/use_search.test.tsx b/src/plugins/discover/public/application/view_components/utils/use_search.test.tsx index 4a92bb5d37be..b76651899b61 100644 --- a/src/plugins/discover/public/application/view_components/utils/use_search.test.tsx +++ b/src/plugins/discover/public/application/view_components/utils/use_search.test.tsx @@ -110,6 +110,48 @@ describe('useSearch', () => { }); }); + it('should initialize with uninitialized state when dataset type config search on page load is disabled', async () => { + const services = createMockServices(); + (services.uiSettings.get as jest.Mock).mockReturnValueOnce(true); + (services.data.query.queryString.getDatasetService as jest.Mock).mockReturnValue({ + meta: { searchOnLoad: false }, + }); + (services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({ + pause: true, + value: 10, + }); + + const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper }); + expect(result.current.data$.getValue()).toEqual( + expect.objectContaining({ status: ResultStatus.UNINITIALIZED }) + ); + + await act(async () => { + await waitForNextUpdate(); + }); + }); + + it('should initialize with uninitialized state when dataset type config search on page load is enabled but the UI setting is disabled', async () => { + const services = createMockServices(); + (services.uiSettings.get as jest.Mock).mockReturnValueOnce(false); + (services.data.query.queryString.getDatasetService as jest.Mock).mockReturnValue({ + meta: { searchOnLoad: true }, + }); + (services.data.query.timefilter.timefilter.getRefreshInterval as jest.Mock).mockReturnValue({ + pause: true, + value: 10, + }); + + const { result, waitForNextUpdate } = renderHook(() => useSearch(services), { wrapper }); + expect(result.current.data$.getValue()).toEqual( + expect.objectContaining({ status: ResultStatus.UNINITIALIZED }) + ); + + await act(async () => { + await waitForNextUpdate(); + }); + }); + it('should update startTime when hook rerenders', async () => { const services = createMockServices(); 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 7f2270efc5a6..158a9cd46074 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 @@ -115,24 +115,23 @@ export const useSearch = (services: DiscoverViewServices) => { requests: new RequestAdapter(), }; - const getDatasetAutoSearchOnPageLoadPreference = () => { - // Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to true - const datasetType = data.query.queryString.getQuery().dataset?.type; - - const datasetService = data.query.queryString.getDatasetService(); - - return !datasetType || (datasetService?.getType(datasetType)?.meta?.searchOnLoad ?? true); - }; - const shouldSearchOnPageLoad = useCallback(() => { + // Checks the searchOnpageLoadPreference for the current dataset if not specifed defaults to UI Settings + const { queryString } = data.query; + const { dataset } = queryString.getQuery(); + const typeConfig = dataset ? queryString.getDatasetService().getType(dataset.type) : undefined; + const datasetPreference = + typeConfig?.meta?.searchOnLoad ?? uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING); + // A saved search is created on every page load, so we check the ID to see if we're loading a // previously saved search or if it is just transient return ( - services.uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) || + datasetPreference || + uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) || savedSearch?.id !== undefined || timefilter.getRefreshInterval().pause === false ); - }, [savedSearch, services.uiSettings, timefilter]); + }, [data.query, savedSearch, uiSettings, timefilter]); const startTime = Date.now(); const data$ = useMemo( @@ -346,9 +345,6 @@ export const useSearch = (services: DiscoverViewServices) => { ]); useEffect(() => { - if (!getDatasetAutoSearchOnPageLoadPreference()) { - skipInitialFetch.current = true; - } const fetch$ = merge( refetch$, filterManager.getFetches$(), @@ -379,8 +375,6 @@ export const useSearch = (services: DiscoverViewServices) => { return () => { subscription.unsubscribe(); }; - // disabling the eslint since we are not adding getDatasetAutoSearchOnPageLoadPreference since this changes when dataset changes and these chnages are already part of data.query.queryString - // eslint-disable-next-line react-hooks/exhaustive-deps }, [ data$, data.query.queryString,