@@ -6,19 +6,19 @@ import {
66 useCallback ,
77 useContext ,
88 useEffect ,
9+ useMemo ,
910 useRef ,
1011 useState ,
1112} from 'react'
1213import { NavigateFunction , useNavigate } from 'react-router-dom'
13- import { type TcUniNavFn } from 'universal-navigation'
14+ import type { AuthUser as NavAuthUser , TcUniNavFn } from 'universal-navigation'
1415import classNames from 'classnames'
1516
1617import { EnvironmentConfig , PageSubheaderPortalId } from '../config'
1718import {
1819 authUrlLogin ,
1920 authUrlLogout ,
2021 authUrlSignup ,
21- LoadingSpinner ,
2222 profileContext ,
2323 ProfileContextData ,
2424 routeContext ,
@@ -44,6 +44,22 @@ const Header: FC = () => {
4444 const navElementId : string = 'main-nav-el'
4545 const navigate : NavigateFunction = useNavigate ( )
4646
47+ // userinfo will be an empty object until profileReady=true
48+ // userinfo will be {user: undefined} if user is logged out
49+ // userinfo will have all user's details when user is logged in
50+ const userInfo : { } | undefined | NavAuthUser = useMemo ( ( ) => (
51+ ! profileReady ? { } : ( {
52+ user : profile ? {
53+ email : profile . email ,
54+ firstName : profile . firstName ,
55+ handle : profile . handle ,
56+ lastName : profile . lastName ,
57+ photoUrl : profile . photoURL ,
58+ userId : profile . userId ,
59+ } : undefined ,
60+ } )
61+ ) , [ profile , profileReady ] )
62+
4763 const navigationHandler : ( request : NavigationRequest ) => void
4864 = useCallback ( ( request : NavigationRequest ) => {
4965
@@ -60,9 +76,10 @@ const Header: FC = () => {
6076 navigate ,
6177 ] )
6278
79+ // initialize uni-nav elements
6380 useEffect ( ( ) => {
6481
65- if ( headerInit . current || ! profileReady ) {
82+ if ( headerInit . current ) {
6683 return
6784 }
6885
@@ -73,40 +90,27 @@ const Header: FC = () => {
7390 navElementId ,
7491 {
7592 handleNavigation : navigationHandler ,
76- onReady ( ) {
77- setReady ( true )
78- document . getElementById ( 'root' ) ?. classList . add ( 'app-ready' )
79- } ,
93+ onReady ( ) { setReady ( true ) } ,
8094 signIn ( ) { window . location . href = authUrlLogin ( ) } ,
8195 signOut ( ) { window . location . href = authUrlLogout } ,
8296 signUp ( ) { window . location . href = authUrlSignup ( ) } ,
8397 toolName : activeToolName ,
8498 toolRoot : activeToolRoute ,
8599 type : 'tool' ,
86- user : profile ? {
87- email : profile . email ,
88- firstName : profile . firstName ,
89- handle : profile . handle ,
90- lastName : profile . lastName ,
91- photoUrl : profile . photoURL ,
92- userId : profile . userId ,
93- } : undefined ,
100+ ...userInfo ,
94101 } ,
95102 )
96103 } , [
97104 activeToolName ,
98105 activeToolRoute ,
99106 navigationHandler ,
100- profile ,
107+ userInfo ,
101108 profileReady ,
102109 ] )
103110
111+ // update uni-nav's tool details
104112 useEffect ( ( ) => {
105113
106- if ( ! profileReady ) {
107- return
108- }
109-
110114 tcUniNav (
111115 'update' ,
112116 navElementId ,
@@ -118,16 +122,33 @@ const Header: FC = () => {
118122 } , [
119123 activeToolName ,
120124 activeToolRoute ,
125+ ] )
126+
127+ // update uni-nav's user/auth details
128+ useEffect ( ( ) => {
129+
130+ if ( ! profileReady ) {
131+ return
132+ }
133+
134+ tcUniNav (
135+ 'update' ,
136+ navElementId ,
137+ {
138+ ...userInfo ,
139+ } ,
140+ )
141+ } , [
121142 profileReady ,
143+ userInfo ,
122144 ] )
123145
124146 return (
125147 < >
126- < LoadingSpinner hide = { ready } />
127148 < div id = { navElementId } />
128149 < div
129150 id = { PageSubheaderPortalId }
130- className = { classNames ( 'full-width-relative' ) }
151+ className = { classNames ( 'full-width-relative' , ! ready && 'hidden' ) }
131152 />
132153 </ >
133154 )
0 commit comments