-
Notifications
You must be signed in to change notification settings - Fork 13
PROD-3232 Adds the uni nav snippet -> PROD-3115_uni-nav #421
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
Changes from 3 commits
0fd1610
d3cfb58
edcc130
8cba9e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,63 @@ | ||
import { FC } from 'react' | ||
|
||
import { Logo } from './logo' | ||
import { ToolSelectors } from './tool-selectors' | ||
import { UtilitySelectors } from './utility-selectors' | ||
import styles from './Header.module.scss' | ||
|
||
const Header: FC<{}> = () => ( | ||
<div className={styles['header-wrap']}> | ||
<header className={styles.header}> | ||
<ToolSelectors isWide={false} /> | ||
<Logo /> | ||
<ToolSelectors isWide /> | ||
<UtilitySelectors /> | ||
</header> | ||
<div id='page-subheader-portal-el' className={styles.subheader} /> | ||
</div> | ||
) | ||
import { Dispatch, FC, MutableRefObject, SetStateAction, useContext, useEffect, useRef, useState } from 'react' | ||
import classNames from 'classnames' | ||
|
||
import { EnvironmentConfig, PagePortalId } from '../config' | ||
import { authUrlLogin, authUrlLogout, authUrlSignup, profileContext, ProfileContextData } from '../lib' | ||
|
||
import UniNavSnippet from './universal-nav-snippet' | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
declare let tcUniNav: any | ||
|
||
const Header: FC = () => { | ||
|
||
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' | ||
|
||
useEffect(() => { | ||
|
||
UniNavSnippet(EnvironmentConfig.UNIVERSAL_NAV.URL) | ||
|
||
if (headerInit.current || !profileReady || !tcUniNav) { | ||
return | ||
} | ||
|
||
headerInit.current = true | ||
|
||
tcUniNav( | ||
'tool', | ||
navElementId, | ||
{ | ||
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() }, | ||
// TODO: make this dynamic based on the current URL | ||
toolName: 'Topcoder Academy', | ||
user: profileReady && profile ? { | ||
handle: profile.handle, | ||
initials: `${profile.firstName.charAt(0)}${profile.lastName.charAt(0)}`, | ||
photoURL: profile.photoURL, | ||
userId: profile.userId, | ||
} : undefined, | ||
}, | ||
) | ||
}, [profileReady, profile]) | ||
|
||
return ( | ||
<> | ||
<div id={navElementId} /> | ||
<div | ||
id={PagePortalId} | ||
className={classNames('full-width-relative', !ready && 'hidden')} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
export default Header |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to move this into a separate useEffect with no dependencies. We don't want the script to run multiple times.
Better yet, you can call
UniNavSnippet
even before the Header declaration - there's no need to wait for react to run before we can initialize the script (basically we can let the script download and react do it's thing in parallel).