|
1 | | -import { FC } from 'react' |
| 1 | +import { Dispatch, FC, MutableRefObject, SetStateAction, useContext, useEffect, useRef, useState } from 'react' |
| 2 | +import classNames from 'classnames' |
2 | 3 |
|
3 | | -import styles from './Header.module.scss' |
4 | | -import { Logo } from './logo' |
5 | | -import { ToolSelectors } from './tool-selectors' |
6 | | -import { UtilitySelectors } from './utility-selectors' |
| 4 | +import { EnvironmentConfig, PagePortalId } from '../config' |
| 5 | +import { authUrlLogin, authUrlLogout, authUrlSignup, profileContext, ProfileContextData } from '../lib' |
| 6 | + |
| 7 | +import UniNavSnippet from './universal-nav-snippet' |
| 8 | + |
| 9 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 10 | +declare let tcUniNav: any |
| 11 | + |
| 12 | +const Header: FC = () => { |
| 13 | + |
| 14 | + const { profile, initialized: profileReady }: ProfileContextData = useContext(profileContext) |
| 15 | + const [ready, setReady]: [boolean, Dispatch<SetStateAction<boolean>>] = useState<boolean>(false) |
| 16 | + const headerInit: MutableRefObject<boolean> = useRef(false) |
| 17 | + const navElementId: string = 'main-nav-el' |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + |
| 21 | + UniNavSnippet(EnvironmentConfig.UNIVERSAL_NAV.URL) |
| 22 | + |
| 23 | + if (headerInit.current || !profileReady || !tcUniNav) { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + headerInit.current = true |
| 28 | + |
| 29 | + tcUniNav( |
| 30 | + 'tool', |
| 31 | + navElementId, |
| 32 | + { |
| 33 | + onReady() { |
| 34 | + setReady(true) |
| 35 | + document.getElementById('root')?.classList.add('app-ready') |
| 36 | + }, |
| 37 | + signIn() { window.location.href = authUrlLogin() }, |
| 38 | + signOut() { window.location.href = authUrlLogout }, |
| 39 | + signUp() { window.location.href = authUrlSignup() }, |
| 40 | + // TODO: make this dynamic based on the current URL |
| 41 | + toolName: 'Topcoder Academy', |
| 42 | + user: profileReady && profile ? { |
| 43 | + handle: profile.handle, |
| 44 | + initials: `${profile.firstName.charAt(0)}${profile.lastName.charAt(0)}`, |
| 45 | + photoURL: profile.photoURL, |
| 46 | + userId: profile.userId, |
| 47 | + } : undefined, |
| 48 | + }, |
| 49 | + ) |
| 50 | + }, [profileReady, profile]) |
7 | 51 |
|
8 | | -const Header: FC<{}> = () => { |
9 | 52 | return ( |
10 | | - <div className={styles['header-wrap']}> |
11 | | - <header className={styles.header}> |
12 | | - <ToolSelectors isWide={false} /> |
13 | | - <Logo /> |
14 | | - <ToolSelectors isWide={true} /> |
15 | | - <UtilitySelectors /> |
16 | | - </header> |
17 | | - <div id='page-subheader-portal-el' className={styles.subheader}></div> |
18 | | - </div> |
| 53 | + <> |
| 54 | + <div id={navElementId} /> |
| 55 | + <div |
| 56 | + id={PagePortalId} |
| 57 | + className={classNames('full-width-relative', !ready && 'hidden')} |
| 58 | + /> |
| 59 | + </> |
19 | 60 | ) |
20 | 61 | } |
21 | 62 |
|
|
0 commit comments