From 96ecb1a6e7a7cadcc99f28ac8a289c44e6f7f266 Mon Sep 17 00:00:00 2001 From: Hannah Mudge Date: Wed, 26 Jun 2024 08:20:02 -0600 Subject: [PATCH] Handle queries + filters for dataview saved search --- .../embeddable/create/create_dashboard.ts | 1 + .../embeddable/dashboard_container.tsx | 1 + .../diffing/dashboard_diffing_integration.ts | 7 ++- .../editor/saved_search_edit_flyout.tsx | 4 +- .../editor/saved_search_editor_dataview.tsx | 48 +++++++++++++++---- 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts b/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts index 32b21f0cf1c1e..3b44320d166cc 100644 --- a/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts +++ b/src/plugins/dashboard/public/dashboard_container/embeddable/create/create_dashboard.ts @@ -365,6 +365,7 @@ export const initializeDashboard = async ({ const stopSyncingUnifiedSearchState = syncUnifiedSearchState.bind(dashboardContainer)(kbnUrlStateStorage); dashboardContainer.stopSyncingWithUnifiedSearch = () => { + console.log('stopSyncingWithUnifiedSearch'); stopSyncingUnifiedSearchState(); stopSyncingQueryServiceStateWithUrl(); }; diff --git a/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx b/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx index 83e7059d575e3..2d451f64b3063 100644 --- a/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx +++ b/src/plugins/dashboard/public/dashboard_container/embeddable/dashboard_container.tsx @@ -174,6 +174,7 @@ export class DashboardContainer public firstLoad: boolean = true; private hadContentfulRender = false; private scrollPosition?: number; + public ignoreDashboardUnsavedChanges: boolean = false; // cleanup public stopSyncingWithUnifiedSearch?: () => void; diff --git a/src/plugins/dashboard/public/dashboard_container/state/diffing/dashboard_diffing_integration.ts b/src/plugins/dashboard/public/dashboard_container/state/diffing/dashboard_diffing_integration.ts index 4fe3184f619c9..544b842fc91bf 100644 --- a/src/plugins/dashboard/public/dashboard_container/state/diffing/dashboard_diffing_integration.ts +++ b/src/plugins/dashboard/public/dashboard_container/state/diffing/dashboard_diffing_integration.ts @@ -123,10 +123,9 @@ export function startDiffingDashboardState( explicitInput: currentInput, componentState: { lastSavedInput }, } = this.getState(); - const unsavedChanges = await getDashboardUnsavedChanges.bind(this)( - lastSavedInput, - currentInput - ); + const unsavedChanges = this.ignoreDashboardUnsavedChanges + ? {} + : await getDashboardUnsavedChanges.bind(this)(lastSavedInput, currentInput); return unsavedChanges; })(); }) diff --git a/src/plugins/discover/public/embeddable/components/editor/saved_search_edit_flyout.tsx b/src/plugins/discover/public/embeddable/components/editor/saved_search_edit_flyout.tsx index 3416a33904612..454f64b45453d 100644 --- a/src/plugins/discover/public/embeddable/components/editor/saved_search_edit_flyout.tsx +++ b/src/plugins/discover/public/embeddable/components/editor/saved_search_edit_flyout.tsx @@ -89,9 +89,9 @@ export default function SavedSearchEditorFlyout({ {isEsql ? ( - + ) : ( - + )} diff --git a/src/plugins/discover/public/embeddable/components/editor/saved_search_editor_dataview.tsx b/src/plugins/discover/public/embeddable/components/editor/saved_search_editor_dataview.tsx index 66bf45c02c653..32e4566dfb9aa 100644 --- a/src/plugins/discover/public/embeddable/components/editor/saved_search_editor_dataview.tsx +++ b/src/plugins/discover/public/embeddable/components/editor/saved_search_editor_dataview.tsx @@ -7,9 +7,12 @@ */ import React, { useCallback, useEffect, useRef, useState } from 'react'; +import { debounceTime } from 'rxjs'; -import { EuiPanel, EuiSpacer } from '@elastic/eui'; +import { EuiPanel } from '@elastic/eui'; +import { DashboardContainer } from '@kbn/dashboard-plugin/public/dashboard_container'; import { DataViewListItem } from '@kbn/data-views-plugin/common'; +import { Filter, Query } from '@kbn/es-query'; import { i18n } from '@kbn/i18n'; import { useBatchedPublishingSubjects } from '@kbn/presentation-publishing'; import { LazyDataViewPicker, withSuspense } from '@kbn/presentation-util-plugin/public'; @@ -53,6 +56,41 @@ export function SavedSearchDataviewEditor({ const selectedDataView = savedSearch.searchSource.getField('index'); const [dataViews, setDataViews] = useState([]); + useEffect(() => { + (api.parentApi as DashboardContainer).ignoreDashboardUnsavedChanges = true; + + const originalQuery = services.data.query.queryString.getQuery(); + services.data.query.queryString.setQuery(savedSearch.searchSource.getField('query') as Query); + const querySubscription = services.data.query.queryString + .getUpdates$() + .pipe(debounceTime(1)) + .subscribe((newQuery) => { + stateManager.searchSource.next(savedSearch.searchSource.setField('query', newQuery)); + }); + + const originalFilters = services.filterManager.getFilters(); + services.filterManager.setFilters( + (savedSearch.searchSource.getField('filter') ?? []) as Filter[] + ); + const filtersSubscription = services.filterManager + .getUpdates$() + .pipe(debounceTime(1)) + .subscribe(() => { + const newFilters = services.filterManager.getFilters(); + stateManager.searchSource.next(savedSearch.searchSource.setField('filter', newFilters)); + }); + + return () => { + services.data.query.queryString.setQuery(originalQuery); + services.filterManager.setFilters(originalFilters); + + (api.parentApi as DashboardContainer).ignoreDashboardUnsavedChanges = false; + querySubscription.unsubscribe(); + filtersSubscription.unsubscribe(); + }; + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + useEffect(() => { let mounted = true; const fetchDataViews = async () => { @@ -96,7 +134,7 @@ export function SavedSearchDataviewEditor({ }} /> - {selectedDataView ? ( + {selectedDataView && ( name !== field.name)); }} /> - ) : ( - <>error )} - - - test - ); }