66import React , { useEffect , useCallback , useMemo } from 'react' ;
77import { EuiButtonIcon , EuiComboBox , EuiText , EuiToolTip } from '@elastic/eui' ;
88import { i18n } from '@osd/i18n' ;
9- import { DataSource , DataSourceDataSet , IndexPatternOption } from '../datasource' ;
10- import { DataSourceGroup , DataSourceSelectableProps } from './types' ;
9+ import { DataSource , DataSetWithDataSource , IndexPatternOption } from '../datasource' ;
10+ import { DataSourceGroup , DataSourceOption , DataSourceSelectableProps } from './types' ;
1111
12- // Get Index patterns for local cluster .
12+ // Asynchronously retrieves and formats dataset from a given data source .
1313const getAndFormatDataSetFromDataSource = async (
1414 ds : DataSource
15- ) : Promise < DataSourceDataSet < IndexPatternOption [ ] > > => {
15+ ) : Promise < DataSetWithDataSource < IndexPatternOption [ ] > > => {
1616 const { dataSets } = await ds . getDataSet ( ) ;
17- return { ds, list : dataSets } as DataSourceDataSet < IndexPatternOption [ ] > ;
17+ return { ds, list : dataSets } as DataSetWithDataSource < IndexPatternOption [ ] > ;
1818} ;
1919
2020// Map through all data sources and get their respective data sets.
@@ -28,11 +28,11 @@ export const isIndexPatterns = (dataSet: unknown) =>
2828 ! ! ( dataSet as any ) . title &&
2929 ! ! ( dataSet as any ) . id ;
3030
31- // Mapping function for datasets to get the option format for the combo box from the dataSource and dataSet.
31+ // Mapping function to get the option format for the combo box from the dataSource and dataSet.
3232const mapToOption = (
3333 dataSource : DataSource ,
34- dataSet : DataSourceDataSet | undefined = undefined
35- ) => {
34+ dataSet : DataSetWithDataSource | undefined = undefined
35+ ) : DataSourceOption => {
3636 const baseOption = {
3737 type : dataSource . getType ( ) ,
3838 name : dataSource . getName ( ) ,
@@ -41,9 +41,9 @@ const mapToOption = (
4141 if ( dataSet && 'title' in dataSet && 'id' in dataSet && isIndexPatterns ( dataSet ) ) {
4242 return {
4343 ...baseOption ,
44- label : dataSet . title ,
45- value : dataSet . id ,
46- key : dataSet . id ,
44+ label : dataSet . title as string ,
45+ value : dataSet . id as string ,
46+ key : dataSet . id as string ,
4747 } ;
4848 }
4949 return {
@@ -55,9 +55,13 @@ const mapToOption = (
5555} ;
5656
5757// Function to add or update groups in a reduction process
58- const addOrUpdateGroup = ( acc : DataSourceGroup [ ] , dataSource : DataSource , option ) => {
58+ const addOrUpdateGroup = (
59+ existingGroups : DataSourceGroup [ ] ,
60+ dataSource : DataSource ,
61+ option : DataSourceOption
62+ ) => {
5963 const metadata = dataSource . getMetadata ( ) ;
60- const groupType = metadata . ui . typeGroup ;
64+ const groupType = metadata . ui . groupType ;
6165 let groupName =
6266 metadata . ui . typeLabel ||
6367 i18n . translate ( 'dataExplorer.dataSourceSelector.defaultGroupTitle' , {
@@ -70,34 +74,34 @@ const addOrUpdateGroup = (acc: DataSourceGroup[], dataSource: DataSource, option
7074 } ) ;
7175 }
7276
73- const group = acc . find ( ( g : DataSourceGroup ) => g . typeGroup === groupType ) ;
74- if ( group ) {
75- if ( ! group . options . some ( ( opt ) => opt . key === option . key ) ) {
76- group . options . push ( option ) ;
77- }
77+ const group = existingGroups . find ( ( g : DataSourceGroup ) => g . id === groupType ) ;
78+ if ( group && ! group . options . some ( ( opt ) => opt . key === option . key ) ) {
79+ group . options . push ( option ) ;
7880 } else {
79- acc . push ( {
80- typeGroup : groupType ,
81+ existingGroups . push ( {
82+ groupType,
8183 label : groupName ,
8284 options : [ option ] ,
83- id : metadata . ui . typeGroup , // id for each group
85+ id : metadata . ui . groupType , // id for each group
8486 } ) ;
8587 }
86- return acc ;
8788} ;
8889
89- const consolidateDataSourceGroups = ( dataSets : DataSourceDataSet [ ] , dataSources : DataSource [ ] ) => {
90- return [ ...dataSets , ...dataSources ] . reduce ( ( acc , item ) => {
90+ const consolidateDataSourceGroups = (
91+ dataSets : DataSetWithDataSource [ ] ,
92+ dataSources : DataSource [ ]
93+ ) => {
94+ return [ ...dataSets , ...dataSources ] . reduce ( ( dsGroup , item ) => {
9195 if ( 'list' in item && item . ds ) {
9296 // Confirm item is a DataSet
9397 const options = item . list . map ( ( dataset ) => mapToOption ( item . ds , dataset ) ) ;
94- options . forEach ( ( option ) => addOrUpdateGroup ( acc , item . ds , option ) ) ;
98+ options . forEach ( ( option ) => addOrUpdateGroup ( dsGroup , item . ds , option ) ) ;
9599 } else {
96100 // Handle DataSource directly
97101 const option = mapToOption ( item as InstanceType < typeof DataSource > ) ;
98- addOrUpdateGroup ( acc , item as InstanceType < typeof DataSource > , option ) ;
102+ addOrUpdateGroup ( dsGroup , item as InstanceType < typeof DataSource > , option ) ;
99103 }
100- return acc ;
104+ return dsGroup ;
101105 } , [ ] ) ;
102106} ;
103107
@@ -125,7 +129,7 @@ export const DataSourceSelectable = ({
125129 . then ( ( dataSetResults ) => {
126130 setDataSourceOptionList (
127131 consolidateDataSourceGroups (
128- dataSetResults as DataSourceDataSet [ ] ,
132+ dataSetResults as DataSetWithDataSource [ ] ,
129133 dataSources . filter ( ( ds ) => ! ds . getMetadata ( ) . ui . selector . displayDatasetsAsSource )
130134 )
131135 ) ;
0 commit comments