@@ -23,8 +23,7 @@ import React, { Component } from 'react';
23
23
import { Required } from '@kbn/utility-types' ;
24
24
import { EuiComboBox , EuiComboBoxProps } from '@elastic/eui' ;
25
25
26
- import { SavedObjectsClientContract , SimpleSavedObject } from 'src/core/public' ;
27
- import { getTitle } from '../../../common/index_patterns/lib' ;
26
+ import { IndexPatternsContract } from 'src/plugins/data/public' ;
28
27
29
28
export type IndexPatternSelectProps = Required <
30
29
Omit <
@@ -40,7 +39,7 @@ export type IndexPatternSelectProps = Required<
40
39
} ;
41
40
42
41
export type IndexPatternSelectInternalProps = IndexPatternSelectProps & {
43
- savedObjectsClient : SavedObjectsClientContract ;
42
+ indexPatternService : IndexPatternsContract ;
44
43
} ;
45
44
46
45
interface IndexPatternSelectState {
@@ -50,21 +49,6 @@ interface IndexPatternSelectState {
50
49
searchValue : string | undefined ;
51
50
}
52
51
53
- const getIndexPatterns = async (
54
- client : SavedObjectsClientContract ,
55
- search : string ,
56
- fields : string [ ]
57
- ) => {
58
- const resp = await client . find ( {
59
- type : 'index-pattern' ,
60
- fields,
61
- search : `${ search } *` ,
62
- searchFields : [ 'title' ] ,
63
- perPage : 100 ,
64
- } ) ;
65
- return resp . savedObjects ;
66
- } ;
67
-
68
52
// Needed for React.lazy
69
53
// eslint-disable-next-line import/no-default-export
70
54
export default class IndexPatternSelect extends Component < IndexPatternSelectInternalProps > {
@@ -109,7 +93,8 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
109
93
110
94
let indexPatternTitle ;
111
95
try {
112
- indexPatternTitle = await getTitle ( this . props . savedObjectsClient , indexPatternId ) ;
96
+ const indexPattern = await this . props . indexPatternService . get ( indexPatternId ) ;
97
+ indexPatternTitle = indexPattern . title ;
113
98
} catch ( err ) {
114
99
// index pattern no longer exists
115
100
return ;
@@ -128,49 +113,36 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
128
113
} ;
129
114
130
115
debouncedFetch = _ . debounce ( async ( searchValue : string ) => {
131
- const { fieldTypes, onNoIndexPatterns, savedObjectsClient } = this . props ;
132
-
133
- const savedObjectFields = [ 'title' ] ;
134
- if ( fieldTypes ) {
135
- savedObjectFields . push ( 'fields' ) ;
136
- }
137
- let savedObjects = await getIndexPatterns ( savedObjectsClient , searchValue , savedObjectFields ) ;
138
-
139
- if ( fieldTypes ) {
140
- savedObjects = savedObjects . filter ( ( savedObject : SimpleSavedObject < any > ) => {
141
- try {
142
- const indexPatternFields = JSON . parse ( savedObject . attributes . fields as any ) ;
143
- return indexPatternFields . some ( ( field : any ) => {
144
- return fieldTypes ?. includes ( field . type ) ;
145
- } ) ;
146
- } catch ( err ) {
147
- // Unable to parse fields JSON, invalid index pattern
148
- return false ;
149
- }
150
- } ) ;
151
- }
116
+ const { fieldTypes, onNoIndexPatterns, indexPatternService } = this . props ;
117
+ const indexPatterns = await indexPatternService . find ( `${ searchValue } *` , 100 ) ;
152
118
153
- if ( ! this . isMounted ) {
119
+ // We need this check to handle the case where search results come back in a different
120
+ // order than they were sent out. Only load results for the most recent search.
121
+ if ( searchValue !== this . state . searchValue || ! this . isMounted ) {
154
122
return ;
155
123
}
156
124
157
- // We need this check to handle the case where search results come back in a different
158
- // order than they were sent out. Only load results for the most recent search.
159
- if ( searchValue === this . state . searchValue ) {
160
- const options = savedObjects . map ( ( indexPatternSavedObject : SimpleSavedObject < any > ) => {
125
+ const options = indexPatterns
126
+ . filter ( ( indexPattern ) => {
127
+ return fieldTypes
128
+ ? indexPattern . fields . some ( ( field ) => {
129
+ return fieldTypes . includes ( field . type ) ;
130
+ } )
131
+ : true ;
132
+ } )
133
+ . map ( ( indexPattern ) => {
161
134
return {
162
- label : indexPatternSavedObject . attributes . title ,
163
- value : indexPatternSavedObject . id ,
135
+ label : indexPattern . title ,
136
+ value : indexPattern . id ,
164
137
} ;
165
138
} ) ;
166
- this . setState ( {
167
- isLoading : false ,
168
- options,
169
- } ) ;
139
+ this . setState ( {
140
+ isLoading : false ,
141
+ options,
142
+ } ) ;
170
143
171
- if ( onNoIndexPatterns && searchValue === '' && options . length === 0 ) {
172
- onNoIndexPatterns ( ) ;
173
- }
144
+ if ( onNoIndexPatterns && searchValue === '' && options . length === 0 ) {
145
+ onNoIndexPatterns ( ) ;
174
146
}
175
147
} , 300 ) ;
176
148
@@ -195,7 +167,7 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectInte
195
167
indexPatternId,
196
168
placeholder,
197
169
onNoIndexPatterns,
198
- savedObjectsClient ,
170
+ indexPatternService ,
199
171
...rest
200
172
} = this . props ;
201
173
0 commit comments