Skip to content

Commit

Permalink
Add toggling between legacy and new table
Browse files Browse the repository at this point in the history
According to opensearch-project#5739, add toggle using storage.

Signed-off-by: abbyhu2000 <[email protected]>
  • Loading branch information
abbyhu2000 authored and AMoo-Miki committed Jan 31, 2024
1 parent 3bc11d5 commit 3cabfa5
Show file tree
Hide file tree
Showing 10 changed files with 235 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import { buildDataGridColumns, computeVisibleColumns } from './data_grid_table_c
import { DocViewInspectButton } from './data_grid_table_docview_inspect_button';
import { DataGridFlyout } from './data_grid_table_flyout';
import { DiscoverGridContextProvider } from './data_grid_table_context';
import { toolbarVisibility } from './constants';
import { DocViewFilterFn, OpenSearchSearchHit } from '../../doc_views/doc_views_types';
import { usePagination } from '../utils/use_pagination';
import { SortOrder } from '../../../saved_searches/types';
import { buildColumns } from '../../utils/columns';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { DiscoverServices } from '../../../build_services';
import { SAMPLE_SIZE_SETTING } from '../../../../common';
import { DefaultDiscoverTable } from '../default_discover_table/default_discover_table';
import { LegacyDiscoverTable } from '../default_discover_table/default_discover_table';
import { toolbarVisibility } from './constants';
import { getDataGridTableSetting } from '../utils/local_storage';

export interface DataGridTableProps {
columns: string[];
Expand All @@ -38,6 +39,7 @@ export interface DataGridTableProps {
isToolbarVisible?: boolean;
isContextView?: boolean;
isLoading?: boolean;
storage: any;
}

export const DataGridTable = ({
Expand All @@ -57,6 +59,7 @@ export const DataGridTable = ({
isToolbarVisible = true,
isContextView = false,
isLoading = false,
storage,
}: DataGridTableProps) => {
const { services } = useOpenSearchDashboards<DiscoverServices>();

Expand Down Expand Up @@ -140,24 +143,11 @@ export const DataGridTable = ({
];
}, []);

console.log("sortOrders", sortingColumns)
console.log('sortOrders', sortingColumns);

const table = useMemo(
const legacyDiscoverTable = useMemo(
() => (
// <EuiDataGrid
// aria-labelledby="aria-labelledby"
// columns={dataGridTableColumns}
// columnVisibility={dataGridTableColumnsVisibility}
// leadingControlColumns={leadingControlColumns}
// data-test-subj="docTable"
// pagination={pagination}
// renderCellValue={renderCellValue}
// rowCount={rowCount}
// sorting={sorting}
// toolbarVisibility={isToolbarVisible ? toolbarVisibility : false}
// rowHeightsOptions={rowHeightsOptions}
// />
<DefaultDiscoverTable
<LegacyDiscoverTable
displayedTableColumns={displayedTableColumns}
columns={adjustedColumns}
rows={rows}
Expand Down Expand Up @@ -185,34 +175,34 @@ export const DataGridTable = ({
]
);

// const dataGridTable = useMemo(
// () => (
// <EuiDataGrid
// aria-labelledby="aria-labelledby"
// columns={displayedTableColumns}
// columnVisibility={dataGridTableColumnsVisibility}
// leadingControlColumns={leadingControlColumns}
// data-test-subj="docTable"
// pagination={pagination}
// renderCellValue={renderCellValue}
// rowCount={rowCount}
// sorting={sorting}
// toolbarVisibility={isToolbarVisible ? toolbarVisibility : false}
// rowHeightsOptions={rowHeightsOptions}
// />
// ),
// [
// displayedTableColumns,
// dataGridTableColumnsVisibility,
// leadingControlColumns,
// pagination,
// renderCellValue,
// rowCount,
// sorting,
// isToolbarVisible,
// rowHeightsOptions,
// ]
// );
const dataGridTable = useMemo(
() => (
<EuiDataGrid
aria-labelledby="aria-labelledby"
columns={displayedTableColumns}
columnVisibility={dataGridTableColumnsVisibility}
leadingControlColumns={leadingControlColumns}
data-test-subj="docTable"
pagination={pagination}
renderCellValue={renderCellValue}
rowCount={rowCount}
sorting={sorting}
toolbarVisibility={isToolbarVisible ? toolbarVisibility : false}
rowHeightsOptions={rowHeightsOptions}
/>
),
[
displayedTableColumns,
dataGridTableColumnsVisibility,
leadingControlColumns,
pagination,
renderCellValue,
rowCount,
sorting,
isToolbarVisible,
rowHeightsOptions,
]
);

console.log('adjustColumns higher level', adjustedColumns);
return (
Expand All @@ -233,7 +223,7 @@ export const DataGridTable = ({
data-test-subj="discoverTable"
>
<EuiPanel hasBorder={false} hasShadow={true} paddingSize="s" style={{ margin: '8px' }}>
{table}
{getDataGridTableSetting(storage) ? dataGridTable : legacyDiscoverTable}
</EuiPanel>
{inspectedHit && (
<DataGridFlyout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export interface DefaultDiscoverTableProps {
columns: string[];
rows: OpenSearchSearchHit[];
indexPattern: IndexPattern;
sortOrder: {
sortOrder: Array<{
id: string;
direction: "asc" | "desc";
}[];
direction: 'asc' | 'desc';
}>;
onChangeSortOrder: (cols: EuiDataGridSorting['columns']) => void;
onRemoveColumn: (column: string) => void;
onReorderColumn: (col: string, source: number, destination: number) => void;
Expand All @@ -30,7 +30,7 @@ export interface DefaultDiscoverTableProps {
onClose: () => void;
}

export const DefaultDiscoverTable = ({
export const LegacyDiscoverTable = ({
displayedTableColumns,
columns,
rows,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import { getSortForSearchSource } from '../../view_components/utils/get_sort_for
import { getRootBreadcrumbs } from '../../helpers/breadcrumbs';
import { syncQueryStateWithUrl } from '../../../../../data/public';

import { showTableFeedbacksPanel } from './show_table_feedbacks_panel';
import { getDataGridTableSetting, setDataGridTableSetting } from '../utils/local_storage';

export const getTopNavLinks = (
services: DiscoverViewServices,
inspectorAdapters: Adapters,
Expand All @@ -38,6 +41,7 @@ export const getTopNavLinks = (
store,
data: { query },
osdUrlStateStorage,
storage,
} = services;

const newSearch = {
Expand Down Expand Up @@ -218,7 +222,35 @@ export const getTopNavLinks = (
},
};

const newTable: TopNavMenuData = {
id: 'table-datagrid',
label: i18n.translate('discover.localMenu.newTableTitle', {
defaultMessage: 'Try new Discover experience',
}),
description: i18n.translate('discover.localMenu.newTableDescription', {
defaultMessage: 'New Data Grid Table Experience',
}),
testId: 'datagridTableButton',
run: async () => {
// Read the current state from localStorage
const useDatagridTable = getDataGridTableSetting(storage);
if (useDatagridTable) {
showTableFeedbacksPanel({
I18nContext: core.i18n.Context,
services,
});
} else {
// Save the new setting to localStorage
setDataGridTableSetting(true, storage);
window.location.reload();
}
},
type: 'toggle' as const,
emphasize: getDataGridTableSetting(storage),
};

return [
newTable,
newSearch,
...(capabilities.discover?.save ? [saveSearch] : []),
openSearch,
Expand Down Expand Up @@ -278,7 +310,7 @@ const getSharingData = async ({
const searchSourceInstance = searchSource.createCopy();
const indexPattern = await searchSourceInstance.getField('index');

const { searchFields, selectFields } = await getSharingDataFields(
const { searchFields } = await getSharingDataFields(
state.columns,
services.uiSettings.get(DOC_HIDE_TIME_COLUMN_SETTING),
indexPattern?.timeFieldName
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import ReactDOM from 'react-dom';

import { I18nStart } from '../../../../../../core/public';
import { OpenSearchDashboardsContextProvider } from '../../../../../opensearch_dashboards_react/public';
import { DiscoverViewServices } from '../../../build_services';
import { setDataGridTableSetting } from '../utils/local_storage';
import { TableFeedbacksPanel } from './table_feedbacks_panel';
let isFeedbackPanelOpen = false;

export function showTableFeedbacksPanel({
I18nContext,
services,
}: {
I18nContext: I18nStart['Context'];
services: DiscoverViewServices;
}) {
if (isFeedbackPanelOpen) {
return;
}

isFeedbackPanelOpen = true;
const container = document.createElement('div');
const onClose = () => {
ReactDOM.unmountComponentAtNode(container);
document.body.removeChild(container);
isFeedbackPanelOpen = false;
};

const onTurnOff = async () => {
// Save the new setting to localStorage
setDataGridTableSetting(false, services.storage);
onClose();
window.location.reload();
};

document.body.appendChild(container);
const element = (
<OpenSearchDashboardsContextProvider services={services}>
<I18nContext>
<TableFeedbacksPanel onClose={onClose} onTurnOff={onTurnOff} />
</I18nContext>
</OpenSearchDashboardsContextProvider>
);
ReactDOM.render(element, container);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { shallow, ShallowWrapper } from 'enzyme';
import { TableFeedbacksPanel } from './table_feedbacks_panel';

describe('TableFeedbacksPanel', () => {
let onCloseMock: jest.Mock;
let onTurnOffMock: jest.Mock;
let wrapper: ShallowWrapper;

beforeEach(() => {
onCloseMock = jest.fn();
onTurnOffMock = jest.fn();
wrapper = shallow(<TableFeedbacksPanel onClose={onCloseMock} onTurnOff={onTurnOffMock} />);
});

it('renders correctly', () => {
expect(wrapper).toMatchSnapshot();
});

it('calls onClose when the Cancel button is clicked', () => {
wrapper.find('EuiButtonEmpty').simulate('click');
expect(onCloseMock).toHaveBeenCalled();
});

it('calls onTurnOff when the Turn off new features button is clicked', () => {
wrapper.find('EuiButton').simulate('click');
expect(onTurnOffMock).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import {
EuiModal,
EuiModalHeader,
EuiModalHeaderTitle,
EuiModalBody,
EuiModalFooter,
EuiButton,
EuiText,
EuiButtonEmpty,
} from '@elastic/eui';

interface TableFeedbacksPanelProps {
onClose: () => void;
onTurnOff: () => Promise<void>;
}

export const TableFeedbacksPanel = ({ onClose, onTurnOff }: TableFeedbacksPanelProps) => (
<EuiModal onClose={onClose} maxWidth={600}>
<EuiModalHeader>
<EuiModalHeaderTitle>
<h4>Share your thoughts on the latest Discover features</h4>
</EuiModalHeaderTitle>
</EuiModalHeader>

<EuiModalBody>
<EuiText>
<p>
Event tables: Documents are now expanded through a flyout. Density, column order, and
sorting controls have been improved.{' '}
<a href="https://survey.opensearch.org">Provide feedbacks</a>.
</p>
</EuiText>
</EuiModalBody>

<EuiModalFooter>
<EuiButtonEmpty onClick={onClose}>Cancel</EuiButtonEmpty>
<EuiButton onClick={onTurnOff} fill>
Turn off new features
</EuiButton>
</EuiModalFooter>
</EuiModal>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { Storage } from '../../../../../opensearch_dashboards_utils/public';

export const DATA_GRID_TABLE_KEY = 'discover:dataGridTable';

export const getDataGridTableSetting = (storage: Storage): boolean => {
const storedValue = storage.get(DATA_GRID_TABLE_KEY);
return storedValue !== null ? JSON.parse(storedValue) : false;
};

export const setDataGridTableSetting = (value: boolean, storage: Storage) => {
storage.set(DATA_GRID_TABLE_KEY, JSON.stringify(value));
};
Loading

0 comments on commit 3cabfa5

Please sign in to comment.