Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions src-ts/lib/route-provider/route.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export const RouteProvider: FC<RouteProviderProps> = (props: RouteProviderProps)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
initialized,
location,
profile,
props.toolsRoutes,
props.utilsRoutes,
Expand Down