Skip to content

Commit 0d5c360

Browse files
PROD-3115 #comment This commit attempts to implement the navigation handling #time 30m
1 parent 2450490 commit 0d5c360

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src-ts/header/Header.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import {
33
FC,
44
MutableRefObject,
55
SetStateAction,
6+
useCallback,
67
useContext,
78
useEffect,
89
useRef,
910
useState,
1011
} from 'react'
12+
import { NavigateFunction, useNavigate } from 'react-router-dom'
1113
import classNames from 'classnames'
1214

1315
import { EnvironmentConfig, PageSubheaderPortalId } from '../config'
@@ -35,6 +37,27 @@ const Header: FC = () => {
3537
const [ready, setReady]: [boolean, Dispatch<SetStateAction<boolean>>] = useState<boolean>(false)
3638
const headerInit: MutableRefObject<boolean> = useRef(false)
3739
const navElementId: string = 'main-nav-el'
40+
const navigate: NavigateFunction = useNavigate()
41+
42+
console.debug('active route', activeToolRoute)
43+
44+
const navigationHandler: (label: string, path: string) => void
45+
= useCallback((label: string, path: string) => {
46+
47+
console.debug(path)
48+
49+
try {
50+
// strip the domain and navigate to the path
51+
navigate(new URL(path).pathname)
52+
} catch (error) {
53+
// if we couldn't navigate to the path, just go to the route of the currently active tool
54+
navigate(new URL(activeToolRoute || '/').pathname)
55+
}
56+
57+
}, [
58+
activeToolRoute,
59+
navigate,
60+
])
3861

3962
useEffect(() => {
4063

@@ -48,17 +71,24 @@ const Header: FC = () => {
4871
'init',
4972
navElementId,
5073
{
74+
navigationHandler,
5175
onReady() {
5276
setReady(true)
5377
document.getElementById('root')?.classList.add('app-ready')
5478
},
5579
signIn() { window.location.href = authUrlLogin() },
5680
signOut() { window.location.href = authUrlLogout },
5781
signUp() { window.location.href = authUrlSignup() },
82+
toolName: activeToolName,
83+
toolRoute: activeToolRoute,
5884
type: 'tool',
5985
},
6086
)
61-
}, [])
87+
}, [
88+
activeToolName,
89+
activeToolRoute,
90+
navigationHandler,
91+
])
6292

6393
useEffect(() => {
6494

0 commit comments

Comments
 (0)