import React from 'react' ; import { useRouteMatch , useParams , useLocation } from 'react-router' ; import cfg , { UrlSshParams } from 'teleport/config' ; import ConsoleContext from './consoleContext' ; export default function useRouting ( ctx ) { const { pathname } = useLocation ( ) ; const { clusterId } = useParams < { clusterId : string ; } > ( ) ; const sshRouteMatch = useRouteMatch < UrlSshParams > ( cfg . routes . consoleConnect ) ; const nodesRouteMatch = useRouteMatch ( cfg . routes . consoleNodes ) ; const joinSshRouteMatch = useRouteMatch < UrlSshParams > ( cfg . routes . consoleSession ) ; React . useMemo ( ( ) => { if ( ctx . getActiveDocId ( pathname ) !== - 1 ) { return ; } if ( sshRouteMatch ) { ctx . addSshDocument ( sshRouteMatch . params ) ; } else if ( joinSshRouteMatch ) { ctx . addSshDocument ( joinSshRouteMatch . params ) ; } else if ( nodesRouteMatch ) { ctx . addNodeDocument ( clusterId ) ; } } , [ ctx , pathname ] ) ; return { clusterId , activeDocId : ctx . getActiveDocId ( pathname ) , } ; }