2022-03-07 21:36:36 -05:00
|
|
|
import { Page, ButtonGroup, Button, useBodyScroll, useMediaQuery, useTheme, Tabs, Loading, Spacer } from "@geist-ui/core";
|
2022-03-08 00:02:51 -05:00
|
|
|
import { Github as GitHubIcon, UserPlus as SignUpIcon, User as SignInIcon, Home as HomeIcon, Menu as MenuIcon, Tool as SettingsIcon, UserX as SignoutIcon, PlusCircle as NewIcon, List as YourIcon } from "@geist-ui/icons";
|
2022-03-06 19:46:59 -05:00
|
|
|
import { DriftProps } from "../../pages/_app";
|
2022-03-07 19:42:47 -05:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
import styles from './header.module.css';
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import useSignedIn from "../../lib/hooks/use-signed-in";
|
|
|
|
|
2022-03-07 19:54:33 -05:00
|
|
|
const Header = ({ changeTheme, theme }: DriftProps) => {
|
2022-03-07 19:42:47 -05:00
|
|
|
const router = useRouter();
|
|
|
|
const [selectedTab, setSelectedTab] = useState<string>();
|
|
|
|
const [expanded, setExpanded] = useState<boolean>(false)
|
|
|
|
const [, setBodyHidden] = useBodyScroll(null, { scrollLayer: true })
|
|
|
|
const isMobile = useMediaQuery('xs', { match: 'down' })
|
|
|
|
const { isLoading, isSignedIn } = useSignedIn({ redirectIfNotAuthed: false })
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setBodyHidden(expanded)
|
|
|
|
}, [expanded, setBodyHidden])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isMobile) {
|
|
|
|
setExpanded(false)
|
|
|
|
}
|
|
|
|
}, [isMobile])
|
|
|
|
const pages = useMemo(() => [
|
|
|
|
{
|
|
|
|
name: "Home",
|
|
|
|
href: "/",
|
|
|
|
icon: <HomeIcon />,
|
|
|
|
condition: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "New",
|
|
|
|
href: "/new",
|
|
|
|
icon: <NewIcon />,
|
|
|
|
condition: isSignedIn
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Yours",
|
|
|
|
href: "/mine",
|
|
|
|
icon: <YourIcon />,
|
|
|
|
condition: isSignedIn
|
|
|
|
},
|
2022-03-07 19:54:33 -05:00
|
|
|
// {
|
|
|
|
// name: "Settings",
|
|
|
|
// href: "/settings",
|
|
|
|
// icon: <SettingsIcon />,
|
|
|
|
// condition: isSignedIn
|
|
|
|
// },
|
2022-03-07 19:42:47 -05:00
|
|
|
{
|
|
|
|
name: "Sign out",
|
|
|
|
action: () => {
|
|
|
|
if (typeof window !== 'undefined') {
|
2022-03-06 19:46:59 -05:00
|
|
|
localStorage.clear();
|
2022-03-07 19:42:47 -05:00
|
|
|
router.push("/signin");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
icon: <SignoutIcon />,
|
|
|
|
condition: isSignedIn
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Sign in",
|
|
|
|
href: "/signin",
|
|
|
|
icon: <SignInIcon />,
|
|
|
|
condition: !isSignedIn
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Sign up",
|
|
|
|
href: "/signup",
|
|
|
|
icon: <SignUpIcon />,
|
|
|
|
condition: !isSignedIn
|
2022-03-08 00:02:51 -05:00
|
|
|
},
|
|
|
|
{
|
2022-03-08 19:39:24 -05:00
|
|
|
name: isMobile ? "GitHub" : "",
|
2022-03-08 00:02:51 -05:00
|
|
|
href: "https://github.com/maxleiter/drift",
|
|
|
|
icon: <GitHubIcon />,
|
|
|
|
condition: true
|
2022-03-07 19:42:47 -05:00
|
|
|
}
|
2022-03-08 19:39:24 -05:00
|
|
|
], [isMobile, isSignedIn, router])
|
2022-03-07 19:42:47 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setSelectedTab(pages.find((page) => {
|
|
|
|
if (page.href && page.href === router.asPath) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
})?.href)
|
|
|
|
}, [pages, router, router.pathname])
|
|
|
|
|
|
|
|
return (
|
2022-03-07 21:36:36 -05:00
|
|
|
<Page.Header height={'var(--page-nav-height)'} margin={0} paddingBottom={0} paddingTop={"var(--gap)"}>
|
2022-03-08 19:39:24 -05:00
|
|
|
<div className={styles.tabs}>
|
2022-03-07 19:42:47 -05:00
|
|
|
<Tabs
|
|
|
|
value={selectedTab}
|
|
|
|
leftSpace={0}
|
|
|
|
activeClassName={styles.current}
|
|
|
|
align="center"
|
|
|
|
hideDivider
|
|
|
|
hideBorder
|
|
|
|
onChange={(tab) => {
|
|
|
|
const nameMatch = pages.find(page => page.name === tab)
|
|
|
|
if (nameMatch?.action) {
|
|
|
|
nameMatch.action()
|
|
|
|
} else {
|
|
|
|
router.push(`${tab}`)
|
|
|
|
}
|
|
|
|
}}>
|
2022-03-08 16:19:42 -05:00
|
|
|
{!isLoading && pages.map((tab, index) => {
|
2022-03-07 19:42:47 -05:00
|
|
|
if (tab.condition)
|
|
|
|
return <Tabs.Item
|
|
|
|
font="14px"
|
|
|
|
label={<>{tab.icon} {tab.name}</>}
|
|
|
|
value={tab.href || tab.name}
|
|
|
|
key={`${tab.name}-${index}`}
|
|
|
|
/>
|
|
|
|
else return null
|
|
|
|
})}
|
|
|
|
</Tabs>
|
2022-03-08 19:39:24 -05:00
|
|
|
</div>
|
|
|
|
<div className={styles.controls}>
|
|
|
|
<Button
|
|
|
|
auto
|
|
|
|
type="abort"
|
|
|
|
onClick={() => setExpanded(!expanded)}>
|
|
|
|
<MenuIcon size="1.125rem" />
|
|
|
|
</Button>
|
2022-03-07 19:42:47 -05:00
|
|
|
</div>
|
2022-03-07 21:36:36 -05:00
|
|
|
{isMobile && expanded && (<div className={styles.mobile}>
|
|
|
|
<ButtonGroup vertical>
|
|
|
|
{pages.map((tab, index) => {
|
|
|
|
if (tab.condition)
|
|
|
|
return <Button
|
|
|
|
key={`${tab.name}-${index}`}
|
|
|
|
onClick={() => {
|
|
|
|
const nameMatch = pages.find(page => page.name === tab.name)
|
|
|
|
if (nameMatch?.action) {
|
|
|
|
nameMatch.action()
|
|
|
|
} else {
|
|
|
|
router.push(`${tab.href}`)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
icon={tab.icon}
|
|
|
|
>
|
|
|
|
{tab.name}
|
|
|
|
</Button>
|
|
|
|
})}
|
|
|
|
</ButtonGroup>
|
|
|
|
</div>)}
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
</Page.Header >
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-07 19:42:47 -05:00
|
|
|
export default Header
|
|
|
|
|
|
|
|
|
|
|
|
// {/* {/* <ButtonGroup>
|
|
|
|
// <Button onClick={() => {
|
|
|
|
|
|
|
|
// }}><Link href="/signin">Sign out</Link></Button>
|
|
|
|
// <Button>
|
|
|
|
// <Link href="/mine">
|
|
|
|
// Yours
|
|
|
|
// </Link>
|
|
|
|
// </Button>
|
|
|
|
// <Button>
|
|
|
|
// {/* TODO: Link outside Button, but seems to break ButtonGroup */}
|
|
|
|
// <Link href="/new">
|
|
|
|
// New
|
|
|
|
// </Link>
|
|
|
|
// </Button >
|
|
|
|
// <Button onClick={() => changeTheme()}>
|
|
|
|
// <ShiftBy y={6}>{theme.type === 'light' ? <Moon /> : <Sun />}</ShiftBy>
|
|
|
|
// </Button>
|
|
|
|
// </ButtonGroup > * /}
|