import { useState , useEffect } from 'react' ; import useAttempt from 'shared/hooks/useAttemptNext' ; import Ctx from 'teleport/teleportContext' ; import useStickyClusterId from 'teleport/useStickyClusterId' ; import { Desktop } from 'teleport/services/desktops' ; import cfg from 'teleport/config' ; import { openNewTab } from 'teleport/lib/util' ; export default function useDesktops ( ctx ) { const { attempt , run } = useAttempt ( 'processing' ) ; const { clusterId } = useStickyClusterId ( ) ; const username = ctx . storeUser . state . username ; const windowsLogins = ctx . storeUser . getWindowsLogins ( ) ; const [ desktops , setDesktops ] = useState < Desktop [ ] > ( [ ] ) ; const getWindowsLoginOptions = ( desktopName ) => makeOptions ( clusterId , desktopName , windowsLogins ) ; useEffect ( ( ) => { run ( ( ) => ctx . desktopService . fetchDesktops ( clusterId ) . then ( setDesktops ) ) ; } , [ clusterId ] ) ; const openRemoteDesktopTab = ( username , desktopName ) => { const url = cfg . getDesktopRoute ( { clusterId , desktopName , username , } ) ; openNewTab ( url ) ; } ; return { desktops , attempt , username , clusterId , getWindowsLoginOptions , openRemoteDesktopTab , } ; } function makeOptions ( clusterId , desktopName = '' , logins = [ ] as string [ ] ) { return logins . map ( username => { const url = cfg . getDesktopRoute ( { clusterId , desktopName , username , } ) ; return { login : username , url , } ; } ) ; } export type State = ReturnType < typeof useDesktops > ;