Skip to content

Commit 5a88174

Browse files
Merge pull request #425 from topcoder-platform/PROD-3115_handle-navigation
PROD-3115 handle navigation -> PROD-3115_uni-nav
2 parents 2450490 + 757803d commit 5a88174

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

src-ts/header/Header.tsx

Lines changed: 41 additions & 20 deletions
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'
@@ -28,17 +30,39 @@ import UniNavSnippet from './universal-nav-snippet'
2830
declare let tcUniNav: any
2931
UniNavSnippet(EnvironmentConfig.UNIVERSAL_NAV.URL)
3032

33+
interface NavigationRequest {
34+
label: string
35+
path: string
36+
}
37+
3138
const Header: FC = () => {
3239

3340
const { activeToolName, activeToolRoute }: RouteContextData = useContext(routeContext)
3441
const { profile, initialized: profileReady }: ProfileContextData = useContext(profileContext)
3542
const [ready, setReady]: [boolean, Dispatch<SetStateAction<boolean>>] = useState<boolean>(false)
3643
const headerInit: MutableRefObject<boolean> = useRef(false)
3744
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+
])
3862

3963
useEffect(() => {
4064

41-
if (headerInit.current) {
65+
if (headerInit.current || !profileReady) {
4266
return
4367
}
4468

@@ -48,31 +72,31 @@ const Header: FC = () => {
4872
'init',
4973
navElementId,
5074
{
75+
handleNavigation: navigationHandler,
5176
onReady() {
5277
setReady(true)
5378
document.getElementById('root')?.classList.add('app-ready')
5479
},
5580
signIn() { window.location.href = authUrlLogin() },
5681
signOut() { window.location.href = authUrlLogout },
5782
signUp() { window.location.href = authUrlSignup() },
58-
type: 'tool',
59-
},
60-
)
61-
}, [])
62-
63-
useEffect(() => {
64-
65-
tcUniNav(
66-
'update',
67-
navElementId,
68-
{
6983
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,
7192
},
7293
)
7394
}, [
7495
activeToolName,
7596
activeToolRoute,
97+
navigationHandler,
98+
profile,
99+
profileReady,
76100
])
77101

78102
useEffect(() => {
@@ -85,17 +109,14 @@ const Header: FC = () => {
85109
'update',
86110
navElementId,
87111
{
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,
94114
},
95115
)
96116
}, [
117+
activeToolName,
118+
activeToolRoute,
97119
profileReady,
98-
profile,
99120
])
100121

101122
return (

src-ts/lib/route-provider/route.provider.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export const RouteProvider: FC<RouteProviderProps> = (props: RouteProviderProps)
160160
// eslint-disable-next-line react-hooks/exhaustive-deps
161161
}, [
162162
initialized,
163+
location,
163164
profile,
164165
props.toolsRoutes,
165166
props.utilsRoutes,

0 commit comments

Comments
 (0)