diff --git a/src-ts/header/Header.tsx b/src-ts/header/Header.tsx index 8431cf677..7a2440f71 100644 --- a/src-ts/header/Header.tsx +++ b/src-ts/header/Header.tsx @@ -3,11 +3,13 @@ import { FC, MutableRefObject, SetStateAction, + useCallback, useContext, useEffect, useRef, useState, } from 'react' +import { NavigateFunction, useNavigate } from 'react-router-dom' import classNames from 'classnames' import { EnvironmentConfig, PageSubheaderPortalId } from '../config' @@ -28,6 +30,11 @@ import UniNavSnippet from './universal-nav-snippet' declare let tcUniNav: any UniNavSnippet(EnvironmentConfig.UNIVERSAL_NAV.URL) +interface NavigationRequest { + label: string + path: string +} + const Header: FC = () => { const { activeToolName, activeToolRoute }: RouteContextData = useContext(routeContext) @@ -35,10 +42,27 @@ const Header: FC = () => { const [ready, setReady]: [boolean, Dispatch>] = useState(false) const headerInit: MutableRefObject = useRef(false) const navElementId: string = 'main-nav-el' + const navigate: NavigateFunction = useNavigate() + + const navigationHandler: (request: NavigationRequest) => void + = useCallback((request: NavigationRequest) => { + + try { + // strip the domain and navigate to the path + navigate(new URL(request.path).pathname) + } catch (error) { + // if we couldn't navigate to the path, just go to the route of the currently active tool + navigate(new URL(activeToolRoute || '/').pathname) + } + + }, [ + activeToolRoute, + navigate, + ]) useEffect(() => { - if (headerInit.current) { + if (headerInit.current || !profileReady) { return } @@ -48,6 +72,7 @@ const Header: FC = () => { 'init', navElementId, { + handleNavigation: navigationHandler, onReady() { setReady(true) document.getElementById('root')?.classList.add('app-ready') @@ -55,24 +80,23 @@ const Header: FC = () => { signIn() { window.location.href = authUrlLogin() }, signOut() { window.location.href = authUrlLogout }, signUp() { window.location.href = authUrlSignup() }, - type: 'tool', - }, - ) - }, []) - - useEffect(() => { - - tcUniNav( - 'update', - navElementId, - { toolName: activeToolName, - toolRoute: activeToolRoute, + toolRoot: activeToolRoute, + type: 'tool', + user: profile ? { + handle: profile.handle, + initials: `${profile.firstName.charAt(0)}${profile.lastName.charAt(0)}`, + photoURL: profile.photoURL, + userId: profile.userId, + } : undefined, }, ) }, [ activeToolName, activeToolRoute, + navigationHandler, + profile, + profileReady, ]) useEffect(() => { @@ -85,17 +109,14 @@ const Header: FC = () => { 'update', navElementId, { - user: profile ? { - handle: profile.handle, - initials: `${profile.firstName.charAt(0)}${profile.lastName.charAt(0)}`, - photoURL: profile.photoURL, - userId: profile.userId, - } : undefined, + toolName: activeToolName, + toolRoute: activeToolRoute, }, ) }, [ + activeToolName, + activeToolRoute, profileReady, - profile, ]) return ( diff --git a/src-ts/lib/route-provider/route.provider.tsx b/src-ts/lib/route-provider/route.provider.tsx index 3a68b60f6..f871d7551 100644 --- a/src-ts/lib/route-provider/route.provider.tsx +++ b/src-ts/lib/route-provider/route.provider.tsx @@ -160,6 +160,7 @@ export const RouteProvider: FC = (props: RouteProviderProps) // eslint-disable-next-line react-hooks/exhaustive-deps }, [ initialized, + location, profile, props.toolsRoutes, props.utilsRoutes,