import { useApmPluginContext } from '../apm_plugin/use_apm_plugin_context' ; import { useFetcher } from '../../hooks/use_fetcher' ; import type { APMServiceAlert } from './apm_service_context' ; export function useServiceAlertsFetcher ( { serviceName , transactionType , environment , start , end , } ) { const { plugins : { observability } , } = useApmPluginContext ( ) ; const experimentalAlertsEnabled = observability . isAlertingExperienceEnabled ( ) ; const fetcherStatus = useFetcher ( ( callApmApi ) => { if ( ! start || ! end || ! serviceName || ! transactionType || ! experimentalAlertsEnabled ) { return ; } return callApmApi ( { endpoint : 'GET /internal/apm/services/{serviceName}/alerts' , params : { path : { serviceName , } , query : { start , end , transactionType , environment , } , } , } ) . catch ( ( error ) => { console . error ( error ) ; return { alerts : [ ] as APMServiceAlert [ ] , } ; } ) ; } , [ start , end , serviceName , transactionType , environment , experimentalAlertsEnabled , ] ) ; const { data , ... rest } = fetcherStatus ; return { ... rest , alerts : data ?. alerts ?? [ ] , } ; }