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] Use data source filter function before rendering #6265

Merged
merged 1 commit into from
Mar 25, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
}

interface DataSourceSelectorState {
dataSourceOptions: DataSourceOption[];
selectedOption: DataSourceOption[];
allDataSources: Array<SavedObject<DataSourceAttributes>>;
}

export interface DataSourceOption {
Expand All @@ -52,11 +52,7 @@
super(props);

this.state = {
dataSourceOptions: this.props.defaultOption
? this.props.defaultOption
: this.props.hideLocalCluster
? []
: [LocalCluster],
allDataSources: [],
selectedOption: this.props.defaultOption
? this.props.defaultOption
: this.props.hideLocalCluster
Expand All @@ -74,26 +70,10 @@
getDataSourcesWithFields(this.props.savedObjectsClient, ['id', 'title', 'auth.type'])
.then((fetchedDataSources) => {
if (fetchedDataSources?.length) {
let filteredDataSources = fetchedDataSources;
if (this.props.dataSourceFilter) {
filteredDataSources = fetchedDataSources.filter((ds) =>
this.props.dataSourceFilter!(ds)
);
}

const dataSourceOptions = filteredDataSources.map((dataSource) => ({
id: dataSource.id,
label: dataSource.attributes?.title || '',
}));

if (!this.props.hideLocalCluster) {
dataSourceOptions.unshift(LocalCluster);
}

if (!this._isMounted) return;
this.setState({
...this.state,
dataSourceOptions,
allDataSources: fetchedDataSources,
});
}
})
Expand All @@ -119,6 +99,16 @@
this.props.placeholderText === undefined
? 'Select a data source'
: this.props.placeholderText;

const dataSources = this.props.dataSourceFilter
? this.state.allDataSources.filter((ds) => this.props.dataSourceFilter!(ds))

Check warning on line 104 in src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx#L104

Added line #L104 was not covered by tests
: this.state.allDataSources;

const options = dataSources.map((ds) => ({ id: ds.id, label: ds.attributes?.title || '' }));
if (!this.props.hideLocalCluster) {
options.unshift(LocalCluster);

Check warning on line 109 in src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx#L109

Added line #L109 was not covered by tests
}

return (
<EuiComboBox
aria-label={
Expand All @@ -136,7 +126,7 @@
: ''
}
singleSelection={{ asPlainText: true }}
options={this.state.dataSourceOptions}
options={options}
selectedOptions={this.state.selectedOption}
onChange={(e) => this.onChange(e)}
prepend={
Expand Down
Loading