From aa7dd9b1394f2058ba9ed7654eef5776e64e20ae Mon Sep 17 00:00:00 2001 From: Lu Yu Date: Sun, 17 Mar 2024 03:51:28 +0000 Subject: [PATCH 1/2] use filter function before rendering Signed-off-by: Lu Yu --- .../data_source_aggregated_view.test.tsx.snap | 222 +++++++++--------- .../data_source_selector.tsx | 38 ++- 2 files changed, 125 insertions(+), 135 deletions(-) diff --git a/src/plugins/data_source_management/public/components/data_source_aggregated_view/__snapshots__/data_source_aggregated_view.test.tsx.snap b/src/plugins/data_source_management/public/components/data_source_aggregated_view/__snapshots__/data_source_aggregated_view.test.tsx.snap index 2be5b7c11b8a..2c789f04da12 100644 --- a/src/plugins/data_source_management/public/components/data_source_aggregated_view/__snapshots__/data_source_aggregated_view.test.tsx.snap +++ b/src/plugins/data_source_management/public/components/data_source_aggregated_view/__snapshots__/data_source_aggregated_view.test.tsx.snap @@ -1,5 +1,59 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`DataSourceAggregatedView should render normally with data source filter 1`] = ` + + + Data sources + + + All + + + } + closePopover={[Function]} + display="inlineBlock" + hasArrow={true} + id="dataSourceSViewContextMenuPopover" + isOpen={false} + ownFocus={true} + panelPaddingSize="none" + > + + + +`; + exports[`DataSourceAggregatedView should render normally with local cluster and actice selections 1`] = ` `; -exports[`DataSourceAggregatedView should render normally with local cluster and actice selections 2`] = ` +exports[`DataSourceAggregatedView should render normally with local cluster hidden and all options 1`] = ` + + +`; + +exports[`DataSourceAggregatedView should render normally with local cluster not hidden and all options 1`] = ` + + + Data sources + + + All + + + } + closePopover={[Function]} + display="inlineBlock" + hasArrow={true} + id="dataSourceSViewContextMenuPopover" + isOpen={false} + ownFocus={true} + panelPaddingSize="none" + > + `; -exports[`DataSourceAggregatedView should render normally with local cluster and actice selections 3`] = ` +exports[`DataSourceAggregatedView should render popup when clicking on info icon 1`] = ` Object { "asFragment": [Function], "baseElement": @@ -339,111 +447,3 @@ Object { "unmount": [Function], } `; - -exports[`DataSourceAggregatedView should render normally with local cluster hidden and all options 1`] = ` - - - Data sources - - - All - - - } - closePopover={[Function]} - display="inlineBlock" - hasArrow={true} - id="dataSourceSViewContextMenuPopover" - isOpen={false} - ownFocus={true} - panelPaddingSize="none" - > - - - -`; - -exports[`DataSourceAggregatedView should render normally with local cluster not hidden and all options 1`] = ` - - - Data sources - - - All - - - } - closePopover={[Function]} - display="inlineBlock" - hasArrow={true} - id="dataSourceSViewContextMenuPopover" - isOpen={false} - ownFocus={true} - panelPaddingSize="none" - > - - - -`; diff --git a/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx b/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx index e7503cba645a..3e9f4c377160 100644 --- a/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx +++ b/src/plugins/data_source_management/public/components/data_source_selector/data_source_selector.tsx @@ -32,8 +32,8 @@ export interface DataSourceSelectorProps { } interface DataSourceSelectorState { - dataSourceOptions: DataSourceOption[]; selectedOption: DataSourceOption[]; + allDataSources: Array>; } export interface DataSourceOption { @@ -52,11 +52,7 @@ export class DataSourceSelector extends React.Component< 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 @@ -74,26 +70,10 @@ export class DataSourceSelector extends React.Component< 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, }); } }) @@ -119,6 +99,16 @@ export class DataSourceSelector extends React.Component< 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)) + : this.state.allDataSources; + + const options = dataSources.map((ds) => ({ id: ds.id, label: ds.attributes?.title || '' })); + if (!this.props.hideLocalCluster) { + options.unshift(LocalCluster); + } + return ( this.onChange(e)} prepend={ From bbf74b0da4d570ce8db37f946fcbe6f3d839f551 Mon Sep 17 00:00:00 2001 From: Lu Yu Date: Mon, 18 Mar 2024 01:48:41 +0000 Subject: [PATCH 2/2] add change log Signed-off-by: Lu Yu --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0adcbc43f515..16cf785dac2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - [Workspace] Add workspace id in basePath ([#6060](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6060)) - Implement new home page ([#6065](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6065)) - Add sidecar service ([#5920](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5920)) +- [Multiple Datasource] Use data source filter function before rendering ([#6175](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6175)) ### 🐛 Bug Fixes