From 9eeca435c187b68e38a4b7547088b0e1482cc8b7 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Thu, 14 Sep 2023 22:29:32 +0000 Subject: [PATCH 1/2] fixes add filter and dismissible callout Signed-off-by: Ashwin P Chandran --- .../canvas/discover_chart_container.tsx | 32 +++++++++---------- .../view_components/canvas/discover_table.tsx | 6 ++-- .../view_components/canvas/index.tsx | 11 +++++-- .../view_components/panel/index.tsx | 29 +++++++++++++++-- .../view_components/utils/filter_columns.ts | 2 +- .../components/discover_legacy.tsx | 11 +++++-- 6 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx b/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx index cae81b6604c0..568d61c2e4e0 100644 --- a/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/discover_chart_container.tsx @@ -20,22 +20,22 @@ export const DiscoverChartContainer = ({ hits, bucketInterval, chartData }: Sear indexPattern, ]); + if (!hits || !chartData || !bucketInterval) return null; + return ( - hits && ( - { - window.location.href = `#/view/${savedSearch?.id}`; - window.location.reload(); - }} - services={services} - showResetButton={!!savedSearch && !!savedSearch.id} - isTimeBased={isTimeBased} - /> - ) + { + window.location.href = `#/view/${savedSearch?.id}`; + window.location.reload(); + }} + services={services} + showResetButton={!!savedSearch && !!savedSearch.id} + isTimeBased={isTimeBased} + /> ); }; diff --git a/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx b/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx index c4e05cd6bdf3..e292303d9f8d 100644 --- a/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/discover_table.tsx @@ -51,13 +51,15 @@ export const DiscoverTable = ({ history }: Props) => { refetch$.next(); }; const onAddFilter = useCallback( - (field: IndexPatternField, values: string, operation: '+' | '-') => { + (field: string | IndexPatternField, values: string, operation: '+' | '-') => { + if (!indexPattern) return; + const newFilters = opensearchFilters.generateFilters( filterManager, field, values, operation, - indexPattern.id + indexPattern.id ?? '' ); return filterManager.addFilters(newFilters); }, diff --git a/src/plugins/discover/public/application/view_components/canvas/index.tsx b/src/plugins/discover/public/application/view_components/canvas/index.tsx index 6c26763d5736..b5adc9596321 100644 --- a/src/plugins/discover/public/application/view_components/canvas/index.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/index.tsx @@ -20,6 +20,8 @@ import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_re import { filterColumns } from '../utils/filter_columns'; import { DEFAULT_COLUMNS_SETTING } from '../../../../common'; +const KEY_SHOW_NOTICE = 'discover:deprecation-notice:show'; + // eslint-disable-next-line import/no-default-export export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewProps) { const { data$, refetch$, indexPattern } = useDiscoverContext(); @@ -41,8 +43,13 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro bucketInterval: {}, }); - const [isCallOutVisible, setIsCallOutVisible] = useState(true); - const closeCallOut = () => setIsCallOutVisible(false); + const [isCallOutVisible, setIsCallOutVisible] = useState( + localStorage.getItem(KEY_SHOW_NOTICE) !== 'false' + ); + const closeCallOut = () => { + localStorage.setItem(KEY_SHOW_NOTICE, 'false'); + setIsCallOutVisible(false); + }; let callOut; diff --git a/src/plugins/discover/public/application/view_components/panel/index.tsx b/src/plugins/discover/public/application/view_components/panel/index.tsx index 6f2e04b9a9ba..203b24b8819f 100644 --- a/src/plugins/discover/public/application/view_components/panel/index.tsx +++ b/src/plugins/discover/public/application/view_components/panel/index.tsx @@ -3,7 +3,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useState } from 'react'; import { ViewProps } from '../../../../../data_explorer/public'; import { addColumn, @@ -15,9 +15,18 @@ import { import { DiscoverSidebar } from '../../components/sidebar'; import { useDiscoverContext } from '../context'; import { ResultStatus, SearchData } from '../utils/use_search'; +import { IndexPatternField, opensearchFilters } from '../../../../../data/public'; +import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public'; +import { DiscoverViewServices } from '../../../build_services'; // eslint-disable-next-line import/no-default-export export default function DiscoverPanel(props: ViewProps) { + const { services } = useOpenSearchDashboards(); + const { + data: { + query: { filterManager }, + }, + } = services; const { data$, indexPattern } = useDiscoverContext(); const [fetchState, setFetchState] = useState(data$.getValue()); @@ -36,6 +45,22 @@ export default function DiscoverPanel(props: ViewProps) { }; }, [data$, fetchState]); + const onAddFilter = useCallback( + (field: string | IndexPatternField, values: string, operation: '+' | '-') => { + if (!indexPattern) return; + + const newFilters = opensearchFilters.generateFilters( + filterManager, + field, + values, + operation, + indexPattern.id ?? '' + ); + return filterManager.addFilters(newFilters); + }, + [filterManager, indexPattern] + ); + return ( {}} + onAddFilter={onAddFilter} /> ); } diff --git a/src/plugins/discover/public/application/view_components/utils/filter_columns.ts b/src/plugins/discover/public/application/view_components/utils/filter_columns.ts index 47c0921ddf3a..1b2c7554910a 100644 --- a/src/plugins/discover/public/application/view_components/utils/filter_columns.ts +++ b/src/plugins/discover/public/application/view_components/utils/filter_columns.ts @@ -16,7 +16,7 @@ import { IndexPattern } from '../../../opensearch_dashboards_services'; */ export function filterColumns( columns: string[], - indexPattern: IndexPattern, + indexPattern: IndexPattern | undefined, defaultColumns: string[] ) { const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || []; diff --git a/src/plugins/discover_legacy/public/application/components/discover_legacy.tsx b/src/plugins/discover_legacy/public/application/components/discover_legacy.tsx index d3bf5b6ffeb8..d4a3d235a188 100644 --- a/src/plugins/discover_legacy/public/application/components/discover_legacy.tsx +++ b/src/plugins/discover_legacy/public/application/components/discover_legacy.tsx @@ -101,6 +101,8 @@ export interface DiscoverLegacyProps { vis?: Vis; } +const KEY_SHOW_NOTICE = 'discover:deprecation-notice:show'; + export function DiscoverLegacy({ addColumn, fetch, @@ -132,7 +134,9 @@ export function DiscoverLegacy({ vis, }: DiscoverLegacyProps) { const [isSidebarClosed, setIsSidebarClosed] = useState(false); - const [isCallOutVisible, setIsCallOutVisible] = useState(true); + const [isCallOutVisible, setIsCallOutVisible] = useState( + localStorage.getItem(KEY_SHOW_NOTICE) !== 'false' + ); const { TopNavMenu } = getServices().navigation.ui; const { savedSearch, indexPatternList } = opts; const bucketAggConfig = vis?.data?.aggs?.aggs[1]; @@ -142,7 +146,10 @@ export function DiscoverLegacy({ : undefined; const [fixedScrollEl, setFixedScrollEl] = useState(); - const closeCallOut = () => setIsCallOutVisible(false); + const closeCallOut = () => { + localStorage.setItem(KEY_SHOW_NOTICE, 'false'); + setIsCallOutVisible(false); + }; let callOut; From 8e24607c98dec0431a6be1640dec6a5cfdf18200 Mon Sep 17 00:00:00 2001 From: Ashwin P Chandran Date: Fri, 15 Sep 2023 00:51:51 +0000 Subject: [PATCH 2/2] fixes filters applying when loading a saved search Signed-off-by: Ashwin P Chandran --- .../view_components/utils/use_search.ts | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) 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 44cf47f2c06f..2560fc6d3567 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 @@ -8,6 +8,7 @@ import { BehaviorSubject, Subject, merge } from 'rxjs'; import { debounceTime } from 'rxjs/operators'; import { i18n } from '@osd/i18n'; import { useEffect } from 'react'; +import { cloneDeep } from 'lodash'; import { RequestAdapter } from '../../../../../inspector/public'; import { DiscoverServices } from '../../../build_services'; import { search } from '../../../../../data/public'; @@ -30,7 +31,6 @@ import { getResponseInspectorStats, } from '../../../opensearch_dashboards_services'; import { SEARCH_ON_PAGE_LOAD_SETTING } from '../../../../common'; -import { SortOrder } from '../../../saved_searches/types'; export enum ResultStatus { UNINITIALIZED = 'uninitialized', @@ -253,9 +253,29 @@ export const useSearch = (services: DiscoverServices) => { (async () => { const savedSearchInstance = await getSavedSearchById(savedSearchId || ''); setSavedSearch(savedSearchInstance); + + // 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) { + const result = typeof filters === 'function' ? filters() : filters; + if (result !== undefined) { + actualFilters.push(...(Array.isArray(result) ? result : [result])); + } + } + + filterManager.setAppFilters(actualFilters); + data.query.queryString.setQuery(query); })(); 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 }, [getSavedSearchById, savedSearchId]); return {