import React , { useContext , useEffect , useState } from 'react' ; import { CoreStartContext } from '../contexts/query_input_bar_context' ; import type { IndexPatternValue } from '../../../common/types' ; import { QueryStringInput , QueryStringInputProps } from '../../../../../../plugins/data/public' ; import { getDataStart } from '../../services' ; import { fetchIndexPattern , isStringTypeIndexPattern } from '../../../common/index_patterns_utils' ; type QueryBarWrapperProps = Pick < QueryStringInputProps , 'query' | 'onChange' | 'isInvalid' > & { indexPatterns : IndexPatternValue [ ] ; 'data-test-subj' ? : string ; } ; export function QueryBarWrapper ( { query , onChange , isInvalid , indexPatterns , 'data-test-subj' : dataTestSubj , } ) { const { indexPatterns : indexPatternsService } = getDataStart ( ) ; const [ indexes , setIndexes ] = useState < QueryStringInputProps [ 'indexPatterns' ] > ( [ ] ) ; const coreStartContext = useContext ( CoreStartContext ) ; useEffect ( ( ) => { async function fetchIndexes ( ) { const i = [ ] ; for ( const index of indexPatterns ?? [ ] ) { if ( index ) { if ( isStringTypeIndexPattern ( index ) ) { i . push ( index ) ; } else if ( index ?. id ) { const { indexPattern } = await fetchIndexPattern ( index , indexPatternsService ) ; if ( indexPattern ) { i . push ( indexPattern ) ; } } } else { const defaultIndex = await indexPatternsService . getDefault ( ) ; if ( defaultIndex ) { i . push ( defaultIndex ) ; } } } setIndexes ( i ) ; } fetchIndexes ( ) ; } , [ indexPatterns , indexPatternsService ] ) ; return ( < QueryStringInput query = { query } onChange = { onChange } isInvalid = { isInvalid } indexPatterns = { indexes } { ... coreStartContext } dataTestSubj = { dataTestSubj } / > ) ; }