import React , { useCallback , useEffect , useMemo , useState } from 'react' ; import { DiscoverUrlGeneratorState , DISCOVER_APP_URL_GENERATOR , } from '../../../../../../../../../src/plugins/discover/public' ; import { TransformListAction , TransformListRow } from '../../../../common' ; import { useSearchItems } from '../../../../hooks/use_search_items' ; import { useAppDependencies } from '../../../../app_dependencies' ; import { isDiscoverActionDisabled , discoverActionNameText , DiscoverActionName , } from './discover_action_name' ; const getIndexPatternTitleFromTargetIndex = ( item ) => Array . isArray ( item . config . dest . index ) ? item . config . dest . index . join ( ',' ) : item . config . dest . index ; export type DiscoverAction = ReturnType < typeof useDiscoverAction > ; export const useDiscoverAction = ( forceDisable ) => { const appDeps = useAppDependencies ( ) ; const savedObjectsClient = appDeps . savedObjects . client ; const indexPatterns = appDeps . data . indexPatterns ; const { getUrlGenerator } = appDeps . share . urlGenerators ; const isDiscoverAvailable = ! ! appDeps . application . capabilities . discover ?. show ; const { getIndexPatternIdByTitle , loadIndexPatterns } = useSearchItems ( undefined ) ; const [ indexPatternsLoaded , setIndexPatternsLoaded ] = useState ( false ) ; useEffect ( ( ) => { async function checkIndexPatternAvailability ( ) { await loadIndexPatterns ( savedObjectsClient , indexPatterns ) ; setIndexPatternsLoaded ( true ) ; } checkIndexPatternAvailability ( ) ; } , [ indexPatterns , loadIndexPatterns , savedObjectsClient ] ) ; const clickHandler = useCallback ( async ( item ) => { let discoverUrlGenerator ; try { discoverUrlGenerator = getUrlGenerator ( DISCOVER_APP_URL_GENERATOR ) ; } catch ( error ) { return ; } const indexPatternTitle = getIndexPatternTitleFromTargetIndex ( item ) ; const indexPatternId = getIndexPatternIdByTitle ( indexPatternTitle ) ; const state = { indexPatternId , } ; const path = await discoverUrlGenerator . createUrl ( state ) ; appDeps . application . navigateToApp ( 'discover' , { path } ) ; } , [ appDeps . application , getIndexPatternIdByTitle , getUrlGenerator ] ) ; const indexPatternExists = useCallback ( ( item ) => { const indexPatternTitle = getIndexPatternTitleFromTargetIndex ( item ) ; const indexPatternId = getIndexPatternIdByTitle ( indexPatternTitle ) ; return indexPatternId !== undefined ; } , [ getIndexPatternIdByTitle ] ) ; const action = useMemo ( ( ) => ( { name : ( item ) => { return < DiscoverActionName items = { [ item ] } indexPatternExists = { indexPatternExists ( item ) } / > ; } , available : ( ) => isDiscoverAvailable , enabled : ( item ) => indexPatternsLoaded && ! isDiscoverActionDisabled ( [ item ] , forceDisable , indexPatternExists ( item ) ) , description : discoverActionNameText , icon : 'visTable' , type : 'icon' , onClick : clickHandler , 'data-test-subj' : 'transformActionDiscover' , } ) , [ forceDisable , indexPatternExists , indexPatternsLoaded , isDiscoverAvailable , clickHandler ] ) ; return { action } ; } ;