Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OSCI][Fix][Discover]Prevent Adding Timefield to the Side Nav Selected Fields on Column Removal #5537

Merged
merged 11 commits into from
Dec 14, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG] Fix filtering issue in data source selector ([5484](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5484))
- [BUG][Data] Support for custom filters with heterogeneous data fields ([5577](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5577))
- [BUG][Data] Fix empty suggestion history when querying in search bar [#5349](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5349)
- [BUG][Discover] Fix what is displayed in `selected fields` when removing columns from canvas [#5537](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5537)

### 🚞 Infrastructure

Expand Down
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
Loading