From d3715cd58c46d9337cf4a837e9c3a8ea6e74693a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 14 Dec 2023 23:12:35 +0000 Subject: [PATCH] [OSCI][Fix][Discover]Prevent Adding Timefield to the Side Nav Selected Fields on Column Removal (#5537) * added in a filter for columns before rendering the panel, remove timeField if it was not previously chosen. * fix issue 5538 too --------- Signed-off-by: qiwen li Signed-off-by: Qiwen Li (cherry picked from commit 0a33d4aed571120f34e7608a171e144df159d011) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md --- .../view_components/panel/index.tsx | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 bbf5331fb16e..6b4cd2a87c91 100644 --- a/src/plugins/discover/public/application/view_components/panel/index.tsx +++ b/src/plugins/discover/public/application/view_components/panel/index.tsx @@ -3,12 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useRef, useState } from 'react'; import { ViewProps } from '../../../../../data_explorer/public'; import { addColumn, removeColumn, reorderColumn, + setColumns, useDispatch, useSelector, } from '../../utils/state_management'; @@ -19,6 +20,7 @@ import { IndexPatternField, opensearchFilters } from '../../../../../data/public import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public'; import { DiscoverViewServices } from '../../../build_services'; import { popularizeField } from '../../helpers/popularize_field'; +import { buildColumns } from '../../utils/columns'; // eslint-disable-next-line import/no-default-export export default function DiscoverPanel(props: ViewProps) { @@ -36,7 +38,27 @@ export default function DiscoverPanel(props: ViewProps) { const { columns } = useSelector((state) => ({ columns: state.discover.columns, })); + + const prevColumns = useRef(columns); const dispatch = useDispatch(); + useEffect(() => { + const timeFieldname = indexPattern?.timeFieldName; + + if (columns !== prevColumns.current) { + let updatedColumns = buildColumns(columns); + if ( + timeFieldname && + !prevColumns.current.includes(timeFieldname) && + columns.includes(timeFieldname) + ) { + // Remove timeFieldname from columns if previously chosen columns does not include time field + updatedColumns = columns.filter((column) => column !== timeFieldname); + } + // Update the ref with the new columns + dispatch(setColumns({ columns: updatedColumns })); + prevColumns.current = columns; + } + }, [columns, dispatch, indexPattern?.timeFieldName]); useEffect(() => { const subscription = data$.subscribe((next) => {