2022-03-22 23:06:15 -04:00
|
|
|
|
|
|
|
import { ButtonGroup, Page, Spacer, Tabs, useBodyScroll, useMediaQuery, } from "@geist-ui/core";
|
|
|
|
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
import styles from './header.module.css';
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import useSignedIn from "../../lib/hooks/use-signed-in";
|
|
|
|
|
|
|
|
import HomeIcon from '@geist-ui/icons/home';
|
|
|
|
import MenuIcon from '@geist-ui/icons/menu';
|
|
|
|
import GitHubIcon from '@geist-ui/icons/github';
|
|
|
|
import SignOutIcon from '@geist-ui/icons/userX';
|
|
|
|
import SignInIcon from '@geist-ui/icons/user';
|
|
|
|
import SignUpIcon from '@geist-ui/icons/userPlus';
|
|
|
|
import NewIcon from '@geist-ui/icons/plusCircle';
|
|
|
|
import YourIcon from '@geist-ui/icons/list'
|
|
|
|
import MoonIcon from '@geist-ui/icons/moon';
|
2022-03-28 15:13:22 -04:00
|
|
|
import SettingsIcon from '@geist-ui/icons/settings';
|
2022-03-22 23:06:15 -04:00
|
|
|
import SunIcon from '@geist-ui/icons/sun';
|
2022-03-23 21:21:46 -04:00
|
|
|
import { useTheme } from "next-themes"
|
2022-03-22 23:06:15 -04:00
|
|
|
import { Button } from "@geist-ui/core";
|
2022-03-28 15:13:22 -04:00
|
|
|
import useUserData from "@lib/hooks/use-user-data";
|
2022-03-22 23:06:15 -04:00
|
|
|
|
|
|
|
type Tab = {
|
|
|
|
name: string
|
|
|
|
icon: JSX.Element
|
|
|
|
value: string
|
|
|
|
onClick?: () => void
|
|
|
|
href?: string
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const Header = () => {
|
|
|
|
const router = useRouter();
|
|
|
|
const [selectedTab, setSelectedTab] = useState<string>(router.pathname === '/' ? 'home' : router.pathname.split('/')[1]);
|
|
|
|
const [expanded, setExpanded] = useState<boolean>(false)
|
|
|
|
const [, setBodyHidden] = useBodyScroll(null, { scrollLayer: true })
|
|
|
|
const isMobile = useMediaQuery('xs', { match: 'down' })
|
2022-03-23 15:36:29 -04:00
|
|
|
const { signedIn: isSignedIn, signout } = useSignedIn()
|
2022-03-28 15:13:22 -04:00
|
|
|
const userData = useUserData();
|
2022-03-22 23:06:15 -04:00
|
|
|
const [pages, setPages] = useState<Tab[]>([])
|
2022-03-23 21:21:46 -04:00
|
|
|
const { setTheme, theme } = useTheme()
|
2022-03-29 03:11:02 -04:00
|
|
|
|
2022-03-22 23:06:15 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setBodyHidden(expanded)
|
|
|
|
}, [expanded, setBodyHidden])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isMobile) {
|
|
|
|
setExpanded(false)
|
|
|
|
}
|
|
|
|
}, [isMobile])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-03-23 15:36:29 -04:00
|
|
|
const defaultPages: Tab[] = [
|
2022-03-22 23:06:15 -04:00
|
|
|
{
|
|
|
|
name: isMobile ? "GitHub" : "",
|
|
|
|
href: "https://github.com/maxleiter/drift",
|
|
|
|
icon: <GitHubIcon />,
|
|
|
|
value: "github"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: isMobile ? "Change theme" : "",
|
|
|
|
onClick: function () {
|
2022-03-23 21:21:46 -04:00
|
|
|
if (typeof window !== 'undefined')
|
|
|
|
setTheme(theme === 'light' ? 'dark' : 'light');
|
2022-03-22 23:06:15 -04:00
|
|
|
},
|
|
|
|
icon: theme === 'light' ? <MoonIcon /> : <SunIcon />,
|
|
|
|
value: "theme",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2022-03-23 15:36:29 -04:00
|
|
|
if (isSignedIn)
|
|
|
|
setPages([
|
|
|
|
{
|
2022-03-23 21:21:46 -04:00
|
|
|
name: 'new',
|
|
|
|
icon: <NewIcon />,
|
|
|
|
value: 'new',
|
2022-03-29 16:21:58 -04:00
|
|
|
href: '/new'
|
2022-03-23 15:36:29 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'yours',
|
|
|
|
icon: <YourIcon />,
|
|
|
|
value: 'yours',
|
|
|
|
href: '/mine'
|
|
|
|
},
|
2022-03-26 03:05:05 -04:00
|
|
|
// {
|
|
|
|
// name: 'settings',
|
|
|
|
// icon: <SettingsIcon />,
|
|
|
|
// value: 'settings',
|
|
|
|
// href: '/settings'
|
|
|
|
// },
|
2022-03-23 15:36:29 -04:00
|
|
|
{
|
|
|
|
name: 'sign out',
|
|
|
|
icon: <SignOutIcon />,
|
|
|
|
value: 'signout',
|
|
|
|
onClick: signout
|
|
|
|
},
|
|
|
|
...defaultPages
|
|
|
|
])
|
|
|
|
else
|
|
|
|
setPages([
|
|
|
|
{
|
|
|
|
name: 'home',
|
|
|
|
icon: <HomeIcon />,
|
|
|
|
value: 'home',
|
|
|
|
href: '/'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Sign in',
|
|
|
|
icon: <SignInIcon />,
|
|
|
|
value: 'signin',
|
|
|
|
href: '/signin'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Sign up',
|
|
|
|
icon: <SignUpIcon />,
|
|
|
|
value: 'signup',
|
|
|
|
href: '/signup'
|
|
|
|
},
|
|
|
|
...defaultPages
|
|
|
|
])
|
2022-03-28 15:13:22 -04:00
|
|
|
if (userData?.role === "admin") {
|
|
|
|
setPages((pages) => [
|
|
|
|
...pages,
|
|
|
|
{
|
|
|
|
name: 'admin',
|
|
|
|
icon: <SettingsIcon />,
|
|
|
|
value: 'admin',
|
|
|
|
href: '/admin'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
}
|
2022-03-23 15:36:29 -04:00
|
|
|
// TODO: investigate deps causing infinite loop
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2022-03-28 15:13:22 -04:00
|
|
|
}, [isMobile, isSignedIn, theme, userData])
|
2022-03-22 23:06:15 -04:00
|
|
|
|
|
|
|
const onTabChange = useCallback((tab: string) => {
|
|
|
|
if (typeof window === 'undefined') return
|
|
|
|
const match = pages.find(page => page.value === tab)
|
|
|
|
if (match?.onClick) {
|
|
|
|
match.onClick()
|
|
|
|
} else {
|
|
|
|
router.push(match?.href || '/')
|
|
|
|
}
|
|
|
|
}, [pages, router])
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2022-03-24 22:25:02 -04:00
|
|
|
<Page.Header height={'var(--page-nav-height)'} marginBottom={2}>
|
2022-03-22 23:06:15 -04:00
|
|
|
<div className={styles.tabs}>
|
|
|
|
<Tabs
|
|
|
|
value={selectedTab}
|
|
|
|
leftSpace={0}
|
|
|
|
align="center"
|
|
|
|
hideDivider
|
|
|
|
hideBorder
|
|
|
|
onChange={onTabChange}>
|
|
|
|
{pages.map((tab) => {
|
|
|
|
return <Tabs.Item
|
|
|
|
font="14px"
|
|
|
|
label={<>{tab.icon} {tab.name}</>}
|
|
|
|
value={tab.value}
|
|
|
|
key={`${tab.value}`}
|
|
|
|
/>
|
|
|
|
})}
|
|
|
|
</Tabs>
|
|
|
|
</div>
|
|
|
|
<div className={styles.controls}>
|
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
type="abort"
|
|
|
|
onClick={() => setExpanded(!expanded)}
|
|
|
|
aria-label="Menu"
|
|
|
|
>
|
|
|
|
<Spacer height={5 / 6} width={0} />
|
|
|
|
<MenuIcon />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{isMobile && expanded && (<div className={styles.mobile}>
|
|
|
|
<ButtonGroup vertical>
|
|
|
|
{pages.map((tab, index) => {
|
|
|
|
return <Button
|
|
|
|
key={`${tab.name}-${index}`}
|
|
|
|
onClick={() => onTabChange(tab.value)}
|
|
|
|
icon={tab.icon}
|
|
|
|
>
|
|
|
|
{tab.name}
|
|
|
|
</Button>
|
|
|
|
})}
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>)}
|
|
|
|
</Page.Header >
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Header
|