Skip to content

Commit

Permalink
[BUG][Data Explorer][Discover] Allow data grid to auto adjust size ba…
Browse files Browse the repository at this point in the history
…sed on fetched data count (#5191)

* This PR adds a new rows state to the DiscoverCanvas component and updated it whenever
the data$ observable emitted new row data.
* The DiscoverTable component was then refactored to accept rows as a prop, making it
dependent on the parent component to provide the correct set of data. This ensures that the table
renders correctly based on the current data and doesn't rely on its internal state,
which could be outdated.

Issue Resolve
#5181

Signed-off-by: ananzh <ananzh@amazon.com>
  • Loading branch information
ananzh authored Oct 3, 2023
1 parent 9601c6c commit 677fdf5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [BUG][Discover] Fix misc navigation issues ([#5168](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5168))
- [BUG][Discover] Fix mobile view ([#5168](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5168))
- [BUG][Data Explorer][Discover] Automatically load solo added default index pattern ([#5171](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5171))
- [BUG][Data Explorer][Discover] Allow data grid to auto adjust size based on fetched data count ([#5191](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5191))

### 🚞 Infrastructure

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export interface DataGridTableProps {
onSetColumns: (columns: string[]) => void;
sort: SortOrder[];
displayTimeColumn: boolean;
services: DiscoverServices;
title?: string;
description?: string;
isToolbarVisible?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useState, useEffect, useCallback, useMemo } from 'react';
import { History } from 'history';
import React, { useCallback, useMemo } from 'react';
import { DiscoverViewServices } from '../../../build_services';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { DataGridTable } from '../../components/data_grid/data_grid_table';
Expand All @@ -17,30 +16,27 @@ import {
useDispatch,
useSelector,
} from '../../utils/state_management';
import { ResultStatus, SearchData, useSearch } from '../utils/use_search';
import { useSearch } from '../utils/use_search';
import { IndexPatternField, opensearchFilters } from '../../../../../data/public';
import { DocViewFilterFn } from '../../doc_views/doc_views_types';
import { SortOrder } from '../../../saved_searches/types';
import { DOC_HIDE_TIME_COLUMN_SETTING } from '../../../../common';
import { OpenSearchSearchHit } from '../../doc_views/doc_views_types';

interface Props {
history: History;
rows: OpenSearchSearchHit[];
}

export const DiscoverTable = ({ history }: Props) => {
export const DiscoverTable = ({ rows }: Props) => {
const { services } = useOpenSearchDashboards<DiscoverViewServices>();
const {
uiSettings,
data: {
query: { filterManager },
},
} = services;
const { data$, refetch$, indexPattern, savedSearch } = useDiscoverContext();
const [fetchState, setFetchState] = useState<SearchData>({
status: data$.getValue().status,
rows: [],
});

const { refetch$, indexPattern, savedSearch } = useDiscoverContext();
const { columns, sort } = useSelector((state) => state.discover);
const dispatch = useDispatch();
const onAddColumn = (col: string) => dispatch(addColumn({ column: col }));
Expand Down Expand Up @@ -70,20 +66,6 @@ export const DiscoverTable = ({ history }: Props) => {
[indexPattern, uiSettings]
);

const { rows } = fetchState || {};

useEffect(() => {
const subscription = data$.subscribe((next) => {
if (next.status === ResultStatus.LOADING) return;
if (next.status !== fetchState.status || (next.rows && next.rows !== fetchState.rows)) {
setFetchState({ ...fetchState, ...next });
}
});
return () => {
subscription.unsubscribe();
};
}, [data$, fetchState]);

if (indexPattern === undefined) {
// TODO: handle better
return null;
Expand All @@ -106,7 +88,6 @@ export const DiscoverTable = ({ history }: Props) => {
sort={sort}
rows={rows}
displayTimeColumn={displayTimeColumn}
services={services}
title={savedSearch?.id ? savedSearch.title : ''}
description={savedSearch?.id ? savedSearch.description : ''}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useEffect, useState, useRef, useCallback } from 'react';
import React, { useEffect, useState, useRef, useCallback, useMemo } from 'react';
import { EuiPanel } from '@elastic/eui';
import { TopNav } from './top_nav';
import { ViewProps } from '../../../../../data_explorer/public';
Expand All @@ -19,6 +19,7 @@ import { DiscoverViewServices } from '../../../build_services';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { filterColumns } from '../utils/filter_columns';
import { DEFAULT_COLUMNS_SETTING } from '../../../../common';
import { OpenSearchSearchHit } from '../../../application/doc_views/doc_views_types';
import './discover_canvas.scss';

// eslint-disable-next-line import/no-default-export
Expand All @@ -42,7 +43,6 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro
bucketInterval: {},
});

const { status } = fetchState;
const onQuerySubmit = useCallback(
(payload, isUpdate) => {
if (isUpdate === false) {
Expand All @@ -51,18 +51,30 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro
},
[refetch$]
);
const [rows, setRows] = useState<OpenSearchSearchHit[] | undefined>(undefined);

useEffect(() => {
const subscription = data$.subscribe((next) => {
if (
next.status !== fetchState.status ||
(next.hits && next.hits !== fetchState.hits) ||
(next.bucketInterval && next.bucketInterval !== fetchState.bucketInterval) ||
(next.chartData && next.chartData !== fetchState.chartData)
) {
if (next.status === ResultStatus.LOADING) return;

let shouldUpdateState = false;

if (next.status !== fetchState.status) shouldUpdateState = true;
if (next.hits && next.hits !== fetchState.hits) shouldUpdateState = true;
if (next.bucketInterval && next.bucketInterval !== fetchState.bucketInterval)
shouldUpdateState = true;
if (next.chartData && next.chartData !== fetchState.chartData) shouldUpdateState = true;
if (next.rows && next.rows !== fetchState.rows) {
shouldUpdateState = true;
setRows(next.rows);
}

// Update the state if any condition is met.
if (shouldUpdateState) {
setFetchState({ ...fetchState, ...next });
}
});

return () => {
subscription.unsubscribe();
};
Expand All @@ -77,6 +89,9 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro

const timeField = indexPattern?.timeFieldName ? indexPattern.timeFieldName : undefined;

const MemoizedDiscoverTable = React.memo(DiscoverTable);
const MemoizedDiscoverChartContainer = React.memo(DiscoverChartContainer);

return (
<EuiPanel
hasBorder={false}
Expand All @@ -91,21 +106,21 @@ export default function DiscoverCanvas({ setHeaderActionMenu, history }: ViewPro
onQuerySubmit,
}}
/>
{status === ResultStatus.NO_RESULTS && (
{fetchState.status === ResultStatus.NO_RESULTS && (
<DiscoverNoResults timeFieldName={timeField} queryLanguage={''} />
)}
{status === ResultStatus.UNINITIALIZED && (
{fetchState.status === ResultStatus.UNINITIALIZED && (
<DiscoverUninitialized onRefresh={() => refetch$.next()} />
)}
{status === ResultStatus.LOADING && <LoadingSpinner />}
{status === ResultStatus.READY && (
{fetchState.status === ResultStatus.LOADING && <LoadingSpinner />}
{fetchState.status === ResultStatus.READY && (
<>
<EuiPanel hasBorder={false} hasShadow={false} color="transparent" paddingSize="s">
<EuiPanel>
<DiscoverChartContainer {...fetchState} />
<MemoizedDiscoverChartContainer {...fetchState} />
</EuiPanel>
</EuiPanel>
<DiscoverTable history={history} />
<MemoizedDiscoverTable rows={rows} />
</>
)}
</EuiPanel>
Expand Down

0 comments on commit 677fdf5

Please sign in to comment.