import { useEffect , useState , useAttempt } from 'shared/hooks' ; import { Resource } from 'teleport/services/resources' ; import useTeleport from 'teleport/useTeleport' ; export default function useTrustedClusters ( ) { const teleContext = useTeleport ( ) ; const [ items , setItems ] = useState < Resource < 'trusted_cluster' > [ ] > ( [ ] ) ; const [ attempt , attemptActions ] = useAttempt ( { isProcessing : true } ) ; const canCreate = teleContext . storeUser . getTrustedClusterAccess ( ) . create ; function fetchData ( ) { return teleContext . resourceService . fetchTrustedClusters ( ) . then ( response => { setItems ( response ) ; } ) ; } function save ( yaml , isNew ) { if ( isNew ) { return teleContext . resourceService . createTrustedCluster ( yaml ) . then ( fetchData ) ; } return teleContext . resourceService . updateTrustedCluster ( yaml ) . then ( fetchData ) ; } function remove ( name ) { return teleContext . resourceService . deleteTrustedCluster ( name ) . then ( ( ) => { setItems ( items . filter ( r => r . name !== name ) ) ; } ) ; } useEffect ( ( ) => { attemptActions . do ( ( ) => fetchData ( ) ) ; } , [ ] ) ; return { canCreate , items , save , remove , ... attempt , } ; }