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

[Multiple Datasource] Add component to show single selected data source in read only mode #6125

Merged
merged 4 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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 @@ -21,7 +21,7 @@ describe('DataSourceMenu', () => {
} as any;
});

it('should render normally with local cluster not hidden', () => {
it('should render data source selectable only with local cluster not hidden', () => {
component = shallow(
<DataSourceMenu
showDataSourceSelectable={true}
Expand All @@ -32,12 +32,13 @@ describe('DataSourceMenu', () => {
hideLocalCluster={false}
disableDataSourceSelectable={false}
className={'myclass'}
dataSourceCallBackFunc={jest.fn()}
/>
);
expect(component).toMatchSnapshot();
});

it('should render normally with local cluster is hidden', () => {
it('should render data source selectable only with local cluster is hidden', () => {
component = shallow(
<DataSourceMenu
showDataSourceSelectable={true}
Expand All @@ -48,6 +49,19 @@ describe('DataSourceMenu', () => {
hideLocalCluster={true}
disableDataSourceSelectable={false}
className={'myclass'}
dataSourceCallBackFunc={jest.fn()}
/>
);
expect(component).toMatchSnapshot();
});

it('should render data source view only', () => {
component = shallow(
<DataSourceMenu
showDataSourceView={true}
appName={'myapp'}
fullWidth={true}
className={'myclass'}
/>
);
expect(component).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ import {
import { MountPointPortal } from '../../../../opensearch_dashboards_react/public';
import { DataSourceSelectable } from './data_source_selectable';
import { DataSourceOption } from '../data_source_selector/data_source_selector';
import { DataSourceView } from '../data_source_view';

export interface DataSourceMenuProps {
showDataSourceSelectable: boolean;
showDataSourceSelectable?: boolean;
showDataSourceView?: boolean;
appName: string;
savedObjects: SavedObjectsClientContract;
notifications: NotificationsStart;
savedObjects?: SavedObjectsClientContract;
notifications?: NotificationsStart;
fullWidth: boolean;
hideLocalCluster: boolean;
dataSourceCallBackFunc: (dataSource: DataSourceOption) => void;
hideLocalCluster?: boolean;
dataSourceCallBackFunc?: (dataSource: DataSourceOption) => void;
Copy link
Member

@xinruiba xinruiba Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the callback function doesn't make sense for read only component, since there is no change to the selection, but only makes sense for selectables, hope it makes sense

Copy link
Member

@xinruiba xinruiba Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disableDataSourceSelectable?: boolean;
className?: string;
selectedOption?: DataSourceOption[];
Expand All @@ -41,35 +43,41 @@ export function DataSourceMenu(props: DataSourceMenuProps): ReactElement | null
fullWidth,
hideLocalCluster,
selectedOption,
showDataSourceView,
filterFn,
} = props;

if (!showDataSourceSelectable) {
if (!showDataSourceSelectable && !showDataSourceView) {
return null;
}

function renderMenu(className: string): ReactElement | null {
if (!showDataSourceSelectable) return null;
function renderDataSourceView(className: string): ReactElement | null {
if (!showDataSourceView) return null;
return (
<EuiHeaderLinks data-test-subj="top-nav" gutterSize="xs" className={className}>
{renderDataSourceSelectable()}
<DataSourceView
fullWidth={fullWidth}
selectedOption={selectedOption && selectedOption.length > 0 ? selectedOption : undefined}
/>
</EuiHeaderLinks>
);
}

function renderDataSourceSelectable(): ReactElement | null {
function renderDataSourceSelectable(className: string): ReactElement | null {
if (!showDataSourceSelectable) return null;
return (
<DataSourceSelectable
fullWidth={fullWidth}
hideLocalCluster={hideLocalCluster}
savedObjectsClient={savedObjects}
notifications={notifications.toasts}
onSelectedDataSource={dataSourceCallBackFunc}
disabled={disableDataSourceSelectable || false}
selectedOption={selectedOption && selectedOption.length > 0 ? selectedOption : undefined}
filterFn={filterFn}
/>
<EuiHeaderLinks data-test-subj="top-nav" gutterSize="xs" className={className}>
<DataSourceSelectable
fullWidth={fullWidth}
hideLocalCluster={hideLocalCluster || false}
savedObjectsClient={savedObjects!}
notifications={notifications!.toasts}
onSelectedDataSource={dataSourceCallBackFunc!}
disabled={disableDataSourceSelectable || false}
selectedOption={selectedOption && selectedOption.length > 0 ? selectedOption : undefined}
filterFn={filterFn}
/>
</EuiHeaderLinks>
);
}

Expand All @@ -80,12 +88,18 @@ export function DataSourceMenu(props: DataSourceMenuProps): ReactElement | null
return (
<>
<MountPointPortal setMountPoint={setMenuMountPoint}>
{renderMenu(menuClassName)}
{renderDataSourceSelectable(menuClassName)}
{renderDataSourceView(menuClassName)}
</MountPointPortal>
</>
);
} else {
return <>{renderMenu(menuClassName)}</>;
return (
<>
{renderDataSourceSelectable(menuClassName)}
{renderDataSourceView(menuClassName)}
</>
);
}
}

Expand All @@ -94,4 +108,6 @@ export function DataSourceMenu(props: DataSourceMenuProps): ReactElement | null

DataSourceMenu.defaultProps = {
disableDataSourceSelectable: false,
showDataSourceView: false,
showDataSourceSelectable: false,
};

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
@@ -0,0 +1,19 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { ShallowWrapper, shallow } from 'enzyme';
import React from 'react';
import { DataSourceView } from './data_source_view';

describe('DataSourceView', () => {
let component: ShallowWrapper<any, Readonly<{}>, React.Component<{}, {}, any>>;

it('should render normally with local cluster not hidden', () => {
component = shallow(
<DataSourceView fullWidth={false} selectedOption={[{ id: 'test1', label: 'test1' }]} />
);
expect(component).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { i18n } from '@osd/i18n';
import { EuiPopover, EuiButtonEmpty, EuiButtonIcon, EuiContextMenu } from '@elastic/eui';
import { DataSourceOption } from '../data_source_selector/data_source_selector';

interface DataSourceViewProps {
fullWidth: boolean;
selectedOption?: DataSourceOption[];
}

interface DataSourceViewState {
dataSourceOptions: DataSourceOption[];
selectedOption: DataSourceOption[];
isPopoverOpen: boolean;
}

export class DataSourceView extends React.Component<DataSourceViewProps, DataSourceViewState> {
private _isMounted: boolean = false;

constructor(props: DataSourceViewProps) {
super(props);

this.state = {
isPopoverOpen: false,
selectedOption: this.props.selectedOption ? this.props.selectedOption : [],
};
}

componentWillUnmount() {
this._isMounted = false;

Check warning on line 35 in src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx#L35

Added line #L35 was not covered by tests
}

onClick() {
this.setState({ ...this.state, isPopoverOpen: !this.state.isPopoverOpen });

Check warning on line 39 in src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx#L39

Added line #L39 was not covered by tests
}

closePopover() {
this.setState({ ...this.state, isPopoverOpen: false });

Check warning on line 43 in src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/data_source_view/data_source_view.tsx#L43

Added line #L43 was not covered by tests
}

async componentDidMount() {
BionIT marked this conversation as resolved.
Show resolved Hide resolved
this._isMounted = true;
}

render() {
const button = (
<EuiButtonIcon
iconType="iInCircle"
display="empty"
aria-label="Next"
onClick={this.onClick.bind(this)}
/>
);

let items = [];

if (this.props.selectedOption) {
items = this.props.selectedOption.map((option) => {
return {
name: option.label,
disabled: true,
};
});
}

const panels = [
{
id: 0,
title: 'Selected data source',
items,
},
];

return (
<>
<EuiButtonEmpty
className="euiHeaderLink"
data-test-subj="dataSourceViewContextMenuHeaderLink"
aria-label={i18n.translate('dataSourceView.dataSourceOptionsButtonAriaLabel', {
defaultMessage: 'dataSourceViewMenuButton',
})}
iconType="database"
iconSide="left"
size="s"
disabled={true}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we have some condition here to dynamically compute the value of disabled? like as somethingthis.props.selectedOption ? Thanks

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always disabled based on requirements, since this component is only used in readyonly mode

>
{this.props.selectedOption && this.props.selectedOption.length > 0
? this.props.selectedOption[0].label
: ''}
</EuiButtonEmpty>
<EuiPopover
id={'dataSourceSViewContextMenuPopover'}
BionIT marked this conversation as resolved.
Show resolved Hide resolved
button={button}
isOpen={this.state.isPopoverOpen}
closePopover={this.closePopover.bind(this)}
panelPaddingSize="none"
anchorPosition="downLeft"
>
<EuiContextMenu initialPanelId={0} panels={panels} />
</EuiPopover>
</>
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export { DataSourceView } from './data_source_view';
Loading