import { useState , useEffect } from 'react' ; import useAttempt from 'shared/hooks/useAttemptNext' ; import Ctx from 'teleport/teleportContext' ; import useStickyClusterId from 'teleport/useStickyClusterId' ; import { Database } from 'teleport/services/databases' ; export default function useDatabases ( ctx ) { const { attempt , run , setAttempt } = useAttempt ( 'processing' ) ; const { clusterId , isLeafCluster } = useStickyClusterId ( ) ; const username = ctx . storeUser . state . username ; const canCreate = ctx . storeUser . getTokenAccess ( ) . create ; const isEnterprise = ctx . isEnterprise ; const version = ctx . storeUser . state . cluster . authVersion ; const authType = ctx . storeUser . state . authType ; const [ databases , setDatabases ] = useState < Database [ ] > ( [ ] ) ; const [ isAddDialogVisible , setIsAddDialogVisible ] = useState ( false ) ; useEffect ( ( ) => { run ( ( ) => ctx . databaseService . fetchDatabases ( clusterId ) . then ( setDatabases ) ) ; } , [ clusterId ] ) ; const fetchDatabases = ( ) => { return ctx . databaseService . fetchDatabases ( clusterId ) . then ( setDatabases ) . catch ( ( err ) => setAttempt ( { status : 'failed' , statusText : err . message } ) ) ; } ; const hideAddDialog = ( ) => { setIsAddDialogVisible ( false ) ; fetchDatabases ( ) ; } ; const showAddDialog = ( ) => { setIsAddDialogVisible ( true ) ; } ; return { databases , attempt , canCreate , isLeafCluster , isEnterprise , hideAddDialog , showAddDialog , isAddDialogVisible , username , version , clusterId , authType , } ; } export type State = ReturnType < typeof useDatabases > ;