Skip to content

Commit

Permalink
Refactor read-only component to cover more edge cases
Browse files Browse the repository at this point in the history
Signed-off-by: Yuanqi(Ella) Zhu <zhyuanqi@amazon.com>
  • Loading branch information
zhyuanqi committed Apr 11, 2024
1 parent cd857cb commit a84b784
Show file tree
Hide file tree
Showing 10 changed files with 388 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React from 'react';
import React, { useState, useMemo } from 'react';
import {
EuiBasicTable,
EuiPageBody,
Expand Down Expand Up @@ -34,8 +34,11 @@ export const DataSourceViewExample = ({
dataSourceEnabled,
setActionMenu,
dataSourceManagement,
notifications,
savedObjects,
}: DataSourceViewExampleProps) => {
const DataSourceMenu = dataSourceManagement.ui.getDataSourceMenu<DataSourceViewConfig>();
const [selectedDataSources, setSelectedDataSources] = useState<string[]>([]);
const data: ComponentProp[] = [
{
name: 'savedObjects',
Expand Down Expand Up @@ -68,19 +71,31 @@ export const DataSourceViewExample = ({
},
];

const renderDataSourceComponent = useMemo(() => {
return (
<DataSourceMenu
setMenuMountPoint={setActionMenu}
componentType={'DataSourceView'}
componentConfig={{
notifications,
savedObjects: savedObjects.client,
fullWidth: false,
activeOption: [{ id: '' }],
dataSourceFilter: (ds) => {
return true;
},
onSelectedDataSources: (ds) => {
setSelectedDataSources(ds);
},
}}
/>
);
}, [setActionMenu, notifications, savedObjects]);

return (
<EuiPageBody component="main">
<EuiPageHeader>
{dataSourceEnabled && (
<DataSourceMenu
setMenuMountPoint={setActionMenu}
componentType={'DataSourceView'}
componentConfig={{
fullWidth: false,
activeOption: [{ id: 'example id', label: 'example data source' }],
}}
/>
)}
{dataSourceEnabled && renderDataSourceComponent}
<EuiPageHeaderSection>
<EuiTitle size="l">
<h1>Data Source View Example</h1>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,24 @@ export function DataSourceMenu<T>(props: DataSourceMenuProps<T>): ReactElement |
const { componentType, componentConfig, uiSettings, hideLocalCluster } = props;

function renderDataSourceView(config: DataSourceViewConfig): ReactElement | null {
const { activeOption, fullWidth, savedObjects, notifications } = config;
const {
activeOption,
fullWidth,
savedObjects,
notifications,
dataSourceFilter,
onSelectedDataSources,
} = config;
return (
<DataSourceView
fullWidth={fullWidth}
selectedOption={activeOption}
savedObjectsClient={savedObjects}
notifications={notifications?.toasts}
hideLocalCluster={hideLocalCluster || false}
dataSourceFilter={dataSourceFilter}
onSelectedDataSources={onSelectedDataSources}
uiSettings={uiSettings}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ export const DataSourceComponentType = {
export type DataSourceComponentType = typeof DataSourceComponentType[keyof typeof DataSourceComponentType];

export interface DataSourceViewConfig extends DataSourceBaseConfig {
onSelectedDataSources: (dataSources: DataSourceOption[]) => void;
activeOption: DataSourceOption[];
savedObjects?: SavedObjectsClientContract;
notifications?: NotificationsStart;
dataSourceFilter?: (dataSource: SavedObject<DataSourceAttributes>) => boolean;
}

export interface DataSourceAggregatedViewConfig extends DataSourceBaseConfig {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a84b784

Please sign in to comment.