import { useEffect , useState } from 'react' ; import { useAttempt } from 'shared/hooks' ; import { Node } from 'teleport/services/nodes' ; import { useConsoleContext } from './../consoleContextProvider' ; import * as stores from './../stores' ; export default function useNodes ( { clusterId , id } ) { const consoleCtx = useConsoleContext ( ) ; const [ attempt , attemptActions ] = useAttempt ( { isProcessing : true } ) ; const [ state , setState ] = useState < { nodes : Node [ ] ; logins : string [ ] ; } > ( { nodes : [ ] , logins : [ ] , } ) ; useEffect ( ( ) => { attemptActions . do ( ( ) => { return consoleCtx . fetchNodes ( clusterId ) . then ( ( { nodes , logins } ) => { setState ( { logins , nodes , } ) ; } ) ; } ) ; } , [ clusterId ] ) ; function createSshSession ( login , serverId ) { const url = consoleCtx . getSshDocumentUrl ( { serverId , login , clusterId , } ) ; consoleCtx . gotoTab ( { url } ) ; consoleCtx . removeDocument ( id ) ; } function changeCluster ( value ) { const clusterId = value ; const url = consoleCtx . getNodeDocumentUrl ( clusterId ) ; consoleCtx . storeDocs . update ( id , { url , clusterId , } ) ; consoleCtx . gotoTab ( { url } ) ; } function getNodeSshLogins ( serverId ) { return state . logins . map ( login => ( { login , url : consoleCtx . getSshDocumentUrl ( { serverId , login , clusterId } ) , } ) ) ; } return { nodes : state . nodes , attempt , createSshSession , changeCluster , getNodeSshLogins , } ; }