import React from 'react' ; import { useParams } from 'react-router' ; import service from 'teleport/services/apps' ; import useAttempt from 'shared/hooks/useAttemptNext' ; import { UrlLauncherParams } from 'teleport/config' ; import { getUrlParameter } from 'teleport/services/history' ; export default function useAppLauncher ( ) { const params = useParams < UrlLauncherParams > ( ) ; const { attempt , setAttempt } = useAttempt ( 'processing' ) ; React . useEffect ( ( ) => { resolveRedirectUrl ( params ) . then ( url => { window . location . replace ( url ) ; } ) . catch ( ( err ) => { setAttempt ( { status : 'failed' , statusText : err . message , } ) ; } ) ; } , [ ] ) ; return { ... attempt , } ; } function resolveRedirectUrl ( params ) { const location = window . location ; const port = location . port ? ':' + location . port : '' ; const state = getUrlParameter ( 'state' , location . search ) ; const arn = getUrlParameter ( 'awsrole' , location . search ) ; if ( ! state ) { return service . getAppFqdn ( params ) . then ( result => { const url = new URL ( `https://${ result . fqdn }${ port }/x-teleport-auth` ) ; if ( params . clusterId ) { url . searchParams . set ( 'cluster' , params . clusterId ) ; } if ( params . publicAddr ) { url . searchParams . set ( 'addr' , params . publicAddr ) ; } if ( params . arn ) { url . searchParams . set ( 'awsrole' , decodeURIComponent ( params . arn ) ) ; } return url . toString ( ) ; } ) ; } if ( arn ) { params . arn = arn ; } return service . createAppSession ( params ) . then ( result => { const url = new URL ( `https://${ result . fqdn }${ port }/x-teleport-auth` ) ; url . searchParams . set ( 'state' , state ) ; url . hash = `#value=${ result . value }` ; return url . toString ( ) ; } ) ; }