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

[Backport 2.x] [Multiple Datasource] Add component to show single selected data source in read only mode #6262

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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;
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,98 @@
/*
* 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 {
selectedOption: DataSourceOption[];
isPopoverOpen: boolean;
}

export class DataSourceView extends React.Component<DataSourceViewProps, DataSourceViewState> {
constructor(props: DataSourceViewProps) {
super(props);

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

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

Check warning on line 32 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#L32

Added line #L32 was not covered by tests
}

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

Check warning on line 36 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#L36

Added line #L36 was not covered by tests
}

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}
>
{this.props.selectedOption && this.props.selectedOption.length > 0
? this.props.selectedOption[0].label
: ''}
</EuiButtonEmpty>
<EuiPopover
id={'dataSourceViewContextMenuPopover'}
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