From 1108e3c01bf48ed4bb2660a51b61552cbf37c5db Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Fri, 15 Nov 2024 00:38:02 +0000 Subject: [PATCH 1/5] Dataset search on page load issues Required a double click on search and then also potentially loading issue. Issue n/a Signed-off-by: Kawika Avilla --- .../view_components/utils/use_search.ts | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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..c3f2a89404aa 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 ( + datasetPreference || services.uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) || savedSearch?.id !== undefined || timefilter.getRefreshInterval().pause === false ); - }, [savedSearch, services.uiSettings, timefilter]); + }, [data.query, savedSearch?.id, services.uiSettings, timefilter, uiSettings]); const startTime = Date.now(); const data$ = useMemo( From 6e3333802bb4d8453bda6636bb95cd7e5c41f4dc Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Fri, 15 Nov 2024 00:43:34 +0000 Subject: [PATCH 2/5] Clean up dependencies Signed-off-by: Kawika Avilla --- .../public/application/view_components/utils/use_search.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 c3f2a89404aa..e1f43829e0ed 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 @@ -127,11 +127,11 @@ export const useSearch = (services: DiscoverViewServices) => { // previously saved search or if it is just transient return ( datasetPreference || - services.uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) || + uiSettings.get(SEARCH_ON_PAGE_LOAD_SETTING) || savedSearch?.id !== undefined || timefilter.getRefreshInterval().pause === false ); - }, [data.query, savedSearch?.id, services.uiSettings, timefilter, uiSettings]); + }, [data.query, savedSearch, uiSettings, timefilter]); const startTime = Date.now(); const data$ = useMemo( From 87f7cc7d1bb721c08a4cfd5d8cf9496744a5093a Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Fri, 15 Nov 2024 00:48:03 +0000 Subject: [PATCH 3/5] Addresses issue Signed-off-by: Kawika Avilla --- .../public/application/view_components/utils/use_search.ts | 5 ----- 1 file changed, 5 deletions(-) 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 e1f43829e0ed..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 @@ -345,9 +345,6 @@ export const useSearch = (services: DiscoverViewServices) => { ]); useEffect(() => { - if (!getDatasetAutoSearchOnPageLoadPreference()) { - skipInitialFetch.current = true; - } const fetch$ = merge( refetch$, filterManager.getFetches$(), @@ -378,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, From 361c850a4041dbb198d76ce5be162a45ef047da1 Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Fri, 15 Nov 2024 11:01:52 +0000 Subject: [PATCH 4/5] add some tests Signed-off-by: Kawika Avilla --- .../view_components/utils/use_search.test.tsx | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) 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(); From 71a26e2f2c852f27a3c23b5b819225bfc2d4bdf7 Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Fri, 15 Nov 2024 11:06:41 +0000 Subject: [PATCH 5/5] Changeset file for PR #8871 created/updated --- changelogs/fragments/8871.yml | 2 ++ 1 file changed, 2 insertions(+) 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