Skip to content

PROD-3115 handle navigation -> PROD-3115_uni-nav #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 23, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions src-ts/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -28,17 +30,39 @@ 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)
const { profile, initialized: profileReady }: ProfileContextData = useContext(profileContext)
const [ready, setReady]: [boolean, Dispatch<SetStateAction<boolean>>] = useState<boolean>(false)
const headerInit: MutableRefObject<boolean> = 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
}

Expand All @@ -48,31 +72,31 @@ const Header: FC = () => {
'init',
navElementId,
{
handleNavigation: navigationHandler,
onReady() {
setReady(true)
document.getElementById('root')?.classList.add('app-ready')
},
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(() => {
Expand All @@ -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 (
Expand Down