import { matchPath , useHistory } from 'react-router' ; import session from 'teleport/services/session' ; import Ctx from 'teleport/teleportContext' ; import cfg from 'teleport/config' ; import { StickyCluster } from 'teleport/types' ; export default function useTopBar ( ctx , stickyCluster ) { const history = useHistory ( ) ; const { clusterId , hasClusterUrl } = stickyCluster ; const popupItems = ctx . storeNav . getTopMenuItems ( ) ; const { username } = ctx . storeUser . state ; const loc = history . location ; const feature = ctx . features . find ( f => matchPath ( loc . pathname , { path : f . route . path , exact : false , } ) ) ; const title = feature ?. getTopNavTitle ( ) || '' ; function loadClusters ( ) { return ctx . clusterService . fetchClusters ( ) ; } function logout ( ) { session . logout ( ) ; } function changeCluster ( value ) { const newPrefix = cfg . getClusterRoute ( value ) ; const oldPrefix = cfg . getClusterRoute ( clusterId ) ; const newPath = loc . pathname . replace ( oldPrefix , newPrefix ) ; history . push ( newPath ) ; } return { clusterId , hasClusterUrl , popupItems , username , changeCluster , loadClusters , logout , title , } ; }