Skip to content

Commit

Permalink
[OSCI][Fix][Discover]Prevent Adding Timefield to the Side Nav Selecte…
Browse files Browse the repository at this point in the history
…d 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 <qiwen_li@brown.edu>
Signed-off-by: Qiwen Li <qiwen_li@brown.edu>
(cherry picked from commit 0a33d4a)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

# Conflicts:
#	CHANGELOG.md
  • Loading branch information
github-actions[bot] committed Jan 26, 2024
1 parent 095d5a1 commit 0e43b37
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand All @@ -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) => {
Expand Down

0 comments on commit 0e43b37

Please sign in to comment.