-
Notifications
You must be signed in to change notification settings - Fork 909
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
Add default icon for selectable component and make sure the default datasource shows automatically #6327
Add default icon for selectable component and make sure the default datasource shows automatically #6327
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -12,9 +12,16 @@ | |
EuiButtonEmpty, | ||
EuiSelectable, | ||
EuiSpacer, | ||
EuiFlexGroup, | ||
EuiFlexItem, | ||
EuiBadge, | ||
} from '@elastic/eui'; | ||
import { SavedObjectsClientContract, ToastsStart } from 'opensearch-dashboards/public'; | ||
import { getDataSourcesWithFields } from '../utils'; | ||
import { | ||
IUiSettingsClient, | ||
SavedObjectsClientContract, | ||
ToastsStart, | ||
} from 'opensearch-dashboards/public'; | ||
import { getDataSourcesWithFields, getDefaultDataSource } from '../utils'; | ||
import { LocalCluster } from '../data_source_selector/data_source_selector'; | ||
import { SavedObject } from '../../../../../core/public'; | ||
import { DataSourceAttributes } from '../../types'; | ||
|
@@ -29,16 +36,18 @@ | |
fullWidth: boolean; | ||
selectedOption?: DataSourceOption[]; | ||
dataSourceFilter?: (dataSource: SavedObject<DataSourceAttributes>) => boolean; | ||
uiSettings?: IUiSettingsClient; | ||
} | ||
|
||
interface DataSourceSelectableState { | ||
dataSourceOptions: SelectedDataSourceOption[]; | ||
isPopoverOpen: boolean; | ||
selectedOption?: SelectedDataSourceOption[]; | ||
defaultDataSource: string | null; | ||
} | ||
|
||
interface SelectedDataSourceOption extends DataSourceOption { | ||
checked?: boolean; | ||
checked?: string; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked should be a string as "on" or "off" |
||
} | ||
|
||
export class DataSourceSelectable extends React.Component< | ||
|
@@ -53,11 +62,8 @@ | |
this.state = { | ||
dataSourceOptions: [], | ||
isPopoverOpen: false, | ||
selectedOption: this.props.selectedOption | ||
? this.props.selectedOption | ||
: this.props.hideLocalCluster | ||
? [] | ||
: [LocalCluster], | ||
selectedOption: [], | ||
defaultDataSource: null, | ||
}; | ||
|
||
this.onChange.bind(this); | ||
|
@@ -77,44 +83,72 @@ | |
|
||
async componentDidMount() { | ||
this._isMounted = true; | ||
getDataSourcesWithFields(this.props.savedObjectsClient, ['id', 'title', 'auth.type']) | ||
.then((fetchedDataSources) => { | ||
if (fetchedDataSources?.length) { | ||
let filteredDataSources: Array<SavedObject<DataSourceAttributes>> = []; | ||
if (this.props.dataSourceFilter) { | ||
filteredDataSources = fetchedDataSources.filter((ds) => | ||
this.props.dataSourceFilter!(ds) | ||
); | ||
} | ||
|
||
if (filteredDataSources.length === 0) { | ||
filteredDataSources = fetchedDataSources; | ||
} | ||
|
||
const dataSourceOptions = filteredDataSources | ||
.map((dataSource) => ({ | ||
id: dataSource.id, | ||
label: dataSource.attributes?.title || '', | ||
})) | ||
.sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase())); | ||
if (!this.props.hideLocalCluster) { | ||
dataSourceOptions.unshift(LocalCluster); | ||
} | ||
|
||
if (!this._isMounted) return; | ||
this.setState({ | ||
...this.state, | ||
dataSourceOptions, | ||
}); | ||
} | ||
}) | ||
.catch(() => { | ||
this.props.notifications.addWarning( | ||
i18n.translate('dataSource.fetchDataSourceError', { | ||
defaultMessage: 'Unable to fetch existing data sources', | ||
try { | ||
let filteredDataSources: Array<SavedObject<DataSourceAttributes>> = []; | ||
let dataSourceOptions: DataSourceOption[] = []; | ||
|
||
// Fetch data sources with fields | ||
const fetchedDataSources = await getDataSourcesWithFields(this.props.savedObjectsClient, [ | ||
'id', | ||
'title', | ||
'auth.type', | ||
]); | ||
|
||
if (fetchedDataSources?.length) { | ||
filteredDataSources = this.props.dataSourceFilter | ||
? fetchedDataSources.filter((ds) => this.props.dataSourceFilter!(ds)) | ||
: fetchedDataSources; | ||
dataSourceOptions = filteredDataSources | ||
.map((dataSource) => ({ | ||
id: dataSource.id, | ||
label: dataSource.attributes?.title || '', | ||
})) | ||
.sort((a, b) => a.label.toLowerCase().localeCompare(b.label.toLowerCase())); | ||
} | ||
|
||
// Add local cluster to the list of data sources if it is not hidden. | ||
if (!this.props.hideLocalCluster) { | ||
dataSourceOptions.unshift(LocalCluster); | ||
} | ||
|
||
const defaultDataSource = this.props.uiSettings?.get('defaultDataSource', null) ?? null; | ||
const selectedDataSource = getDefaultDataSource( | ||
filteredDataSources, | ||
LocalCluster, | ||
this.props.uiSettings, | ||
this.props.hideLocalCluster, | ||
this.props.selectedOption | ||
); | ||
|
||
if (selectedDataSource.length === 0) { | ||
this.props.notifications.addWarning('No connected data source available.'); | ||
} else { | ||
// Update the checked status of the selected data source. | ||
const updatedDataSourceOptions: SelectedDataSourceOption[] = dataSourceOptions.map( | ||
(option) => ({ | ||
...option, | ||
...(option.id === selectedDataSource[0].id && { checked: 'on' }), | ||
}) | ||
); | ||
}); | ||
|
||
if (!this._isMounted) return; | ||
|
||
this.setState({ | ||
...this.state, | ||
dataSourceOptions: updatedDataSourceOptions, | ||
selectedOption: selectedDataSource, | ||
defaultDataSource, | ||
}); | ||
|
||
this.props.onSelectedDataSources(selectedDataSource); | ||
} | ||
} catch (error) { | ||
this.props.notifications.addWarning( | ||
Check warning on line 146 in src/plugins/data_source_management/public/components/data_source_selectable/data_source_selectable.tsx Codecov / codecov/patchsrc/plugins/data_source_management/public/components/data_source_selectable/data_source_selectable.tsx#L146
|
||
i18n.translate('dataSource.fetchDataSourceError', { | ||
defaultMessage: 'Unable to fetch existing data sources', | ||
}) | ||
); | ||
} | ||
} | ||
|
||
onChange(options: SelectedDataSourceOption[]) { | ||
|
@@ -168,7 +202,7 @@ | |
data-test-subj={'dataSourceSelectableContextMenuPopover'} | ||
> | ||
<EuiContextMenuPanel> | ||
<EuiPanel color="transparent" paddingSize="s"> | ||
<EuiPanel color="transparent" paddingSize="s" style={{ width: '300px' }}> | ||
<EuiSpacer size="s" /> | ||
<EuiSelectable | ||
aria-label="Search" | ||
|
@@ -180,6 +214,16 @@ | |
onChange={(newOptions) => this.onChange(newOptions)} | ||
singleSelection={true} | ||
data-test-subj={'dataSourceSelectable'} | ||
renderOption={(option) => ( | ||
<EuiFlexGroup alignItems="center"> | ||
Check warning on line 218 in src/plugins/data_source_management/public/components/data_source_selectable/data_source_selectable.tsx Codecov / codecov/patchsrc/plugins/data_source_management/public/components/data_source_selectable/data_source_selectable.tsx#L218
|
||
<EuiFlexItem grow={1}>{option.label}</EuiFlexItem> | ||
{option.id === this.state.defaultDataSource && ( | ||
<EuiFlexItem grow={false}> | ||
<EuiBadge iconSide="left">Default</EuiBadge> | ||
</EuiFlexItem> | ||
)} | ||
</EuiFlexGroup> | ||
)} | ||
> | ||
{(list, search) => ( | ||
<> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The description here appears to be more descriptive than the PR title.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will modify the PR title and commit message