import { useEffect , useState } from 'react' ; import { RecursiveReadonly } from '@kbn/utility-types' ; import { Capabilities } from 'src/core/types' ; export interface UseGetUserAlertsPermissionsProps { crud : boolean ; read : boolean ; loading ? : boolean ; featureId : string | null ; } export const getAlertsPermissions = ( uiCapabilities , featureId ) => { if ( ! featureId || ! uiCapabilities [ featureId ] ) { return { crud : false , read : false , loading : false , featureId , } ; } return { crud : ( featureId === 'apm' ? uiCapabilities [ featureId ] [ 'alerting:save' ] : uiCapabilities [ featureId ] . save ) as boolean , read : ( featureId === 'apm' ? uiCapabilities [ featureId ] [ 'alerting:show' ] : uiCapabilities [ featureId ] . show ) as boolean , loading : false , featureId , } ; } ; export const useGetUserAlertsPermissions = ( uiCapabilities , featureId ? ) : UseGetUserAlertsPermissionsProps => { const [ alertsPermissions , setAlertsPermissions ] = useState < UseGetUserAlertsPermissionsProps > ( { crud : false , read : false , loading : true , featureId : null , } ) ; useEffect ( ( ) => { if ( ! featureId || ! uiCapabilities [ featureId ] ) { setAlertsPermissions ( { crud : false , read : false , loading : false , featureId : null , } ) ; } else { setAlertsPermissions ( ( currentAlertPermissions ) => { if ( currentAlertPermissions . featureId === featureId ) { return currentAlertPermissions ; } return getAlertsPermissions ( uiCapabilities , featureId ) ; } ) ; } } , [ alertsPermissions . featureId , featureId , uiCapabilities ] ) ; return alertsPermissions ; } ;