import { useEffect , useState } from 'react' ; import { isEmpty } from 'lodash' ; import { usePluginContext } from '../../hooks/use_plugin_context' ; import { TopAlert , parseAlert } from '../../pages/alerts/' ; import { useKibana } from '../../utils/kibana_react' ; import { Ecs } from '../../../../cases/common' ; export const useFetchAlertData = ( ) : [ boolean , Record < string , Ecs > ] => [ false , { } ] ; export const useFetchAlertDetail = ( alertId ) : [ boolean , TopAlert | null ] => { const { http } = useKibana ( ) . services ; const [ loading , setLoading ] = useState ( false ) ; const { observabilityRuleTypeRegistry } = usePluginContext ( ) ; const [ alert , setAlert ] = useState < TopAlert | null > ( null ) ; useEffect ( ( ) => { const abortCtrl = new AbortController ( ) ; const fetchData = async ( ) => { try { setLoading ( true ) ; const response = await http . get < Record < string , unknown > > ( '/internal/rac/alerts' , { query : { id : alertId , } , } ) ; if ( response ) { const parsedAlert = parseAlert ( observabilityRuleTypeRegistry ) ( response ) ; setAlert ( parsedAlert ) ; setLoading ( false ) ; } } catch ( error ) { setAlert ( null ) ; } } ; if ( ! isEmpty ( alertId ) && loading === false && alert === null ) { fetchData ( ) ; } return ( ) => { abortCtrl . abort ( ) ; } ; } , [ http , alertId , alert , loading , observabilityRuleTypeRegistry ] ) ; return [ loading , alert ] ; } ;