import { useMemo } from 'react' ; import { useFetcher } from './use_fetcher' ; import { ENVIRONMENT_ALL , ENVIRONMENT_NOT_DEFINED , } from '../../common/environment_filter_values' ; function getEnvironmentOptions ( environments ) { const environmentOptions = environments . filter ( ( env ) => env !== ENVIRONMENT_NOT_DEFINED . value ) . map ( ( environment ) => ( { value : environment , text : environment , } ) ) ; return [ ENVIRONMENT_ALL , ... environmentOptions ] ; } const INITIAL_DATA = { environments : [ ] } ; export function useEnvironmentsFetcher ( { serviceName , start , end , } ) { const { data = INITIAL_DATA , status } = useFetcher ( ( callApmApi ) => { if ( start && end ) { return callApmApi ( { endpoint : 'GET /internal/apm/environments' , params : { query : { start , end , serviceName , } , } , } ) ; } } , [ start , end , serviceName ] ) ; const environmentOptions = useMemo ( ( ) => getEnvironmentOptions ( data . environments ) , [ data ?. environments ] ) ; return { environments : data . environments , status , environmentOptions } ; }