From 56e800d4661b6f2d9e646726306ab7c0c5cf25be Mon Sep 17 00:00:00 2001 From: ananzh Date: Mon, 9 Oct 2023 22:48:18 +0000 Subject: [PATCH] [BUG][Discover] Allow default columns settings This is a missing functionality * When user sets up `Default columns` in advanced settings, discover should display default columns if the selected idp has the columns. * If selected idp has no such columns, display `_source` column. Issue Resolve https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5246 Signed-off-by: ananzh --- .../application/utils/state_management/discover_slice.tsx | 5 ++++- .../public/application/view_components/canvas/index.tsx | 2 +- .../public/application/view_components/panel/index.tsx | 2 +- .../application/view_components/utils/filter_columns.ts | 6 ++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx b/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx index 270198149e6b..f90d400ff3d5 100644 --- a/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx +++ b/src/plugins/discover/public/application/utils/state_management/discover_slice.tsx @@ -11,7 +11,7 @@ import { RootState, DefaultViewState } from '../../../../../data_explorer/public import { buildColumns } from '../columns'; import * as utils from './common'; import { SortOrder } from '../../../saved_searches/types'; -import { PLUGIN_ID } from '../../../../common'; +import { DEFAULT_COLUMNS_SETTING, PLUGIN_ID } from '../../../../common'; export interface DiscoverState { /** @@ -57,6 +57,7 @@ const initialState: DiscoverState = { export const getPreloadedState = async ({ getSavedSearchById, + uiSettings: config, }: DiscoverServices): Promise> => { const preloadedState: DefaultViewState = { state: { @@ -86,6 +87,8 @@ export const getPreloadedState = async ({ savedSearchInstance.destroy(); // this instance is no longer needed, will create another one later } + } else if (config.get(DEFAULT_COLUMNS_SETTING)) { + preloadedState.state.columns = config.get(DEFAULT_COLUMNS_SETTING); } return preloadedState; 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 fabe2373bfea..b6f30ac43bd6 100644 --- a/src/plugins/discover/public/application/view_components/canvas/index.tsx +++ b/src/plugins/discover/public/application/view_components/canvas/index.tsx @@ -24,7 +24,7 @@ import './discover_canvas.scss'; // eslint-disable-next-line import/no-default-export export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewProps) { - const { data$, refetch$, indexPattern } = useDiscoverContext(); + const { data$, refetch$, indexPattern, savedSearch } = useDiscoverContext(); const { services: { uiSettings }, } = useOpenSearchDashboards(); 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 203b24b8819f..14eb2809c50c 100644 --- a/src/plugins/discover/public/application/view_components/panel/index.tsx +++ b/src/plugins/discover/public/application/view_components/panel/index.tsx @@ -27,7 +27,7 @@ export default function DiscoverPanel(props: ViewProps) { query: { filterManager }, }, } = services; - const { data$, indexPattern } = useDiscoverContext(); + const { data$, indexPattern, savedSearch } = useDiscoverContext(); const [fetchState, setFetchState] = useState(data$.getValue()); const { columns } = useSelector((state) => ({ 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 1b2c7554910a..f9776eb0151e 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 @@ -20,6 +20,8 @@ export function filterColumns( defaultColumns: string[] ) { const fieldsName = indexPattern?.fields.getAll().map((fld) => fld.name) || []; - const filteredColumns = columns.filter((column) => fieldsName.includes(column)); - return filteredColumns.length > 0 ? filteredColumns : defaultColumns; + // combine columns and defaultColumns without duplicates + const combinedColumns = [...new Set([...columns, ...defaultColumns])]; + const filteredColumns = combinedColumns.filter((column) => fieldsName.includes(column)); + return filteredColumns.length > 0 ? filteredColumns : ['_source']; }