@@ -6,19 +6,19 @@ import {
6
6
useCallback ,
7
7
useContext ,
8
8
useEffect ,
9
+ useMemo ,
9
10
useRef ,
10
11
useState ,
11
12
} from 'react'
12
13
import { NavigateFunction , useNavigate } from 'react-router-dom'
13
- import { type TcUniNavFn } from 'universal-navigation'
14
+ import type { AuthUser as NavAuthUser , TcUniNavFn } from 'universal-navigation'
14
15
import classNames from 'classnames'
15
16
16
17
import { EnvironmentConfig , PageSubheaderPortalId } from '../config'
17
18
import {
18
19
authUrlLogin ,
19
20
authUrlLogout ,
20
21
authUrlSignup ,
21
- LoadingSpinner ,
22
22
profileContext ,
23
23
ProfileContextData ,
24
24
routeContext ,
@@ -44,6 +44,22 @@ const Header: FC = () => {
44
44
const navElementId : string = 'main-nav-el'
45
45
const navigate : NavigateFunction = useNavigate ( )
46
46
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
+
47
63
const navigationHandler : ( request : NavigationRequest ) => void
48
64
= useCallback ( ( request : NavigationRequest ) => {
49
65
@@ -60,9 +76,10 @@ const Header: FC = () => {
60
76
navigate ,
61
77
] )
62
78
79
+ // initialize uni-nav elements
63
80
useEffect ( ( ) => {
64
81
65
- if ( headerInit . current || ! profileReady ) {
82
+ if ( headerInit . current ) {
66
83
return
67
84
}
68
85
@@ -73,40 +90,27 @@ const Header: FC = () => {
73
90
navElementId ,
74
91
{
75
92
handleNavigation : navigationHandler ,
76
- onReady ( ) {
77
- setReady ( true )
78
- document . getElementById ( 'root' ) ?. classList . add ( 'app-ready' )
79
- } ,
93
+ onReady ( ) { setReady ( true ) } ,
80
94
signIn ( ) { window . location . href = authUrlLogin ( ) } ,
81
95
signOut ( ) { window . location . href = authUrlLogout } ,
82
96
signUp ( ) { window . location . href = authUrlSignup ( ) } ,
83
97
toolName : activeToolName ,
84
98
toolRoot : activeToolRoute ,
85
99
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 ,
94
101
} ,
95
102
)
96
103
} , [
97
104
activeToolName ,
98
105
activeToolRoute ,
99
106
navigationHandler ,
100
- profile ,
107
+ userInfo ,
101
108
profileReady ,
102
109
] )
103
110
111
+ // update uni-nav's tool details
104
112
useEffect ( ( ) => {
105
113
106
- if ( ! profileReady ) {
107
- return
108
- }
109
-
110
114
tcUniNav (
111
115
'update' ,
112
116
navElementId ,
@@ -118,16 +122,33 @@ const Header: FC = () => {
118
122
} , [
119
123
activeToolName ,
120
124
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
+ } , [
121
142
profileReady ,
143
+ userInfo ,
122
144
] )
123
145
124
146
return (
125
147
< >
126
- < LoadingSpinner hide = { ready } />
127
148
< div id = { navElementId } />
128
149
< div
129
150
id = { PageSubheaderPortalId }
130
- className = { classNames ( 'full-width-relative' ) }
151
+ className = { classNames ( 'full-width-relative' , ! ready && 'hidden' ) }
131
152
/>
132
153
</ >
133
154
)
0 commit comments