@@ -3,11 +3,13 @@ import {
3
3
FC ,
4
4
MutableRefObject ,
5
5
SetStateAction ,
6
+ useCallback ,
6
7
useContext ,
7
8
useEffect ,
8
9
useRef ,
9
10
useState ,
10
11
} from 'react'
12
+ import { NavigateFunction , useNavigate } from 'react-router-dom'
11
13
import classNames from 'classnames'
12
14
13
15
import { EnvironmentConfig , PageSubheaderPortalId } from '../config'
@@ -28,17 +30,39 @@ import UniNavSnippet from './universal-nav-snippet'
28
30
declare let tcUniNav : any
29
31
UniNavSnippet ( EnvironmentConfig . UNIVERSAL_NAV . URL )
30
32
33
+ interface NavigationRequest {
34
+ label : string
35
+ path : string
36
+ }
37
+
31
38
const Header : FC = ( ) => {
32
39
33
40
const { activeToolName, activeToolRoute } : RouteContextData = useContext ( routeContext )
34
41
const { profile, initialized : profileReady } : ProfileContextData = useContext ( profileContext )
35
42
const [ ready , setReady ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState < boolean > ( false )
36
43
const headerInit : MutableRefObject < boolean > = useRef ( false )
37
44
const navElementId : string = 'main-nav-el'
45
+ const navigate : NavigateFunction = useNavigate ( )
46
+
47
+ const navigationHandler : ( request : NavigationRequest ) => void
48
+ = useCallback ( ( request : NavigationRequest ) => {
49
+
50
+ try {
51
+ // strip the domain and navigate to the path
52
+ navigate ( new URL ( request . path ) . pathname )
53
+ } catch ( error ) {
54
+ // if we couldn't navigate to the path, just go to the route of the currently active tool
55
+ navigate ( new URL ( activeToolRoute || '/' ) . pathname )
56
+ }
57
+
58
+ } , [
59
+ activeToolRoute ,
60
+ navigate ,
61
+ ] )
38
62
39
63
useEffect ( ( ) => {
40
64
41
- if ( headerInit . current ) {
65
+ if ( headerInit . current || ! profileReady ) {
42
66
return
43
67
}
44
68
@@ -48,31 +72,31 @@ const Header: FC = () => {
48
72
'init' ,
49
73
navElementId ,
50
74
{
75
+ handleNavigation : navigationHandler ,
51
76
onReady ( ) {
52
77
setReady ( true )
53
78
document . getElementById ( 'root' ) ?. classList . add ( 'app-ready' )
54
79
} ,
55
80
signIn ( ) { window . location . href = authUrlLogin ( ) } ,
56
81
signOut ( ) { window . location . href = authUrlLogout } ,
57
82
signUp ( ) { window . location . href = authUrlSignup ( ) } ,
58
- type : 'tool' ,
59
- } ,
60
- )
61
- } , [ ] )
62
-
63
- useEffect ( ( ) => {
64
-
65
- tcUniNav (
66
- 'update' ,
67
- navElementId ,
68
- {
69
83
toolName : activeToolName ,
70
- toolRoute : activeToolRoute ,
84
+ toolRoot : activeToolRoute ,
85
+ type : 'tool' ,
86
+ user : profile ? {
87
+ handle : profile . handle ,
88
+ initials : `${ profile . firstName . charAt ( 0 ) } ${ profile . lastName . charAt ( 0 ) } ` ,
89
+ photoURL : profile . photoURL ,
90
+ userId : profile . userId ,
91
+ } : undefined ,
71
92
} ,
72
93
)
73
94
} , [
74
95
activeToolName ,
75
96
activeToolRoute ,
97
+ navigationHandler ,
98
+ profile ,
99
+ profileReady ,
76
100
] )
77
101
78
102
useEffect ( ( ) => {
@@ -85,17 +109,14 @@ const Header: FC = () => {
85
109
'update' ,
86
110
navElementId ,
87
111
{
88
- user : profile ? {
89
- handle : profile . handle ,
90
- initials : `${ profile . firstName . charAt ( 0 ) } ${ profile . lastName . charAt ( 0 ) } ` ,
91
- photoURL : profile . photoURL ,
92
- userId : profile . userId ,
93
- } : undefined ,
112
+ toolName : activeToolName ,
113
+ toolRoute : activeToolRoute ,
94
114
} ,
95
115
)
96
116
} , [
117
+ activeToolName ,
118
+ activeToolRoute ,
97
119
profileReady ,
98
- profile ,
99
120
] )
100
121
101
122
return (
0 commit comments