2022-11-09 21:38:05 -05:00
|
|
|
"use client"
|
2022-11-29 03:43:04 -05:00
|
|
|
import { useBodyScroll, useMediaQuery } from "@geist-ui/core/dist"
|
2022-03-21 23:30:45 -04:00
|
|
|
|
2022-11-15 23:50:54 -05:00
|
|
|
import { useEffect, useState } from "react"
|
2022-04-09 20:48:19 -04:00
|
|
|
import styles from "./header.module.css"
|
2022-03-06 19:46:59 -05:00
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
// import useUserData from "@lib/hooks/use-user-data"
|
2022-04-09 20:48:19 -04:00
|
|
|
import Link from "next/link"
|
2022-11-09 21:38:05 -05:00
|
|
|
import { usePathname } from "next/navigation"
|
2022-11-12 03:58:21 -05:00
|
|
|
import { signOut } from "next-auth/react"
|
2022-11-15 23:50:54 -05:00
|
|
|
import Button from "@components/button"
|
2022-11-20 23:54:34 -05:00
|
|
|
import clsx from "clsx"
|
2022-11-29 03:43:04 -05:00
|
|
|
import { useTheme } from "@wits/next-themes"
|
|
|
|
import {
|
|
|
|
GitHub,
|
|
|
|
Home,
|
|
|
|
Menu,
|
|
|
|
Moon,
|
|
|
|
PlusCircle,
|
|
|
|
Settings,
|
|
|
|
Sun,
|
|
|
|
User,
|
|
|
|
UserPlus,
|
|
|
|
UserX
|
|
|
|
} from "react-feather"
|
2022-04-02 03:45:26 -04:00
|
|
|
|
|
|
|
type Tab = {
|
2022-04-09 20:48:19 -04:00
|
|
|
name: string
|
|
|
|
icon: JSX.Element
|
|
|
|
value: string
|
|
|
|
onClick?: () => void
|
|
|
|
href?: string
|
2022-04-02 03:45:26 -04:00
|
|
|
}
|
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const Header = ({ signedIn = false, isAdmin = false }) => {
|
2022-11-09 21:38:05 -05:00
|
|
|
const pathname = usePathname()
|
2022-04-09 20:48:19 -04:00
|
|
|
const [expanded, setExpanded] = useState<boolean>(false)
|
|
|
|
const [, setBodyHidden] = useBodyScroll(null, { scrollLayer: true })
|
|
|
|
const isMobile = useMediaQuery("xs", { match: "down" })
|
2022-11-11 22:17:44 -05:00
|
|
|
// const { status } = useSession()
|
|
|
|
// const signedIn = status === "authenticated"
|
2022-11-15 23:50:54 -05:00
|
|
|
const { setTheme, theme } = useTheme()
|
2022-04-09 20:48:19 -04:00
|
|
|
useEffect(() => {
|
|
|
|
setBodyHidden(expanded)
|
|
|
|
}, [expanded, setBodyHidden])
|
2022-04-02 03:45:26 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
useEffect(() => {
|
|
|
|
if (!isMobile) {
|
|
|
|
setExpanded(false)
|
|
|
|
}
|
|
|
|
}, [isMobile])
|
2022-04-02 03:45:26 -04:00
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const getPages = () => {
|
2022-04-09 20:48:19 -04:00
|
|
|
const defaultPages: Tab[] = [
|
|
|
|
{
|
|
|
|
name: isMobile ? "GitHub" : "",
|
|
|
|
href: "https://github.com/maxleiter/drift",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <GitHub />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "github"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: isMobile ? "Change theme" : "",
|
|
|
|
onClick: function () {
|
|
|
|
if (typeof window !== "undefined")
|
2022-11-15 23:50:54 -05:00
|
|
|
setTheme(theme === "light" ? "dark" : "light")
|
2022-04-09 20:48:19 -04:00
|
|
|
},
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: theme === "light" ? <Moon /> : <Sun />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "theme"
|
|
|
|
}
|
|
|
|
]
|
2022-04-02 03:45:26 -04:00
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
if (isAdmin) {
|
|
|
|
defaultPages.push({
|
2022-11-15 23:50:54 -05:00
|
|
|
name: "Admin",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <Settings />,
|
2022-11-14 04:28:40 -05:00
|
|
|
value: "admin",
|
|
|
|
href: "/admin"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-11 22:17:44 -05:00
|
|
|
if (signedIn)
|
2022-11-14 04:28:40 -05:00
|
|
|
return [
|
2022-04-09 20:48:19 -04:00
|
|
|
{
|
2022-11-15 23:50:54 -05:00
|
|
|
name: "New",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <PlusCircle />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "new",
|
|
|
|
href: "/new"
|
|
|
|
},
|
|
|
|
{
|
2022-11-15 23:50:54 -05:00
|
|
|
name: "Yours",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <User />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "yours",
|
|
|
|
href: "/mine"
|
|
|
|
},
|
2022-04-20 01:14:08 -04:00
|
|
|
{
|
2022-11-15 23:50:54 -05:00
|
|
|
name: "Settings",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <Settings />,
|
2022-04-22 01:01:59 -04:00
|
|
|
value: "settings",
|
|
|
|
href: "/settings"
|
2022-04-20 01:14:08 -04:00
|
|
|
},
|
2022-04-09 20:48:19 -04:00
|
|
|
{
|
2022-11-15 23:50:54 -05:00
|
|
|
name: "Sign Out",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <UserX />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "signout",
|
2022-11-29 03:43:04 -05:00
|
|
|
onClick: () =>
|
|
|
|
signOut({
|
|
|
|
callbackUrl: "/"
|
|
|
|
})
|
2022-04-09 20:48:19 -04:00
|
|
|
},
|
|
|
|
...defaultPages
|
2022-11-14 04:28:40 -05:00
|
|
|
]
|
2022-04-09 20:48:19 -04:00
|
|
|
else
|
2022-11-14 04:28:40 -05:00
|
|
|
return [
|
2022-04-09 20:48:19 -04:00
|
|
|
{
|
2022-11-15 23:50:54 -05:00
|
|
|
name: "Home",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <Home />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "home",
|
|
|
|
href: "/"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Sign in",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <User />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "signin",
|
|
|
|
href: "/signin"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Sign up",
|
2022-11-29 03:43:04 -05:00
|
|
|
icon: <UserPlus />,
|
2022-04-09 20:48:19 -04:00
|
|
|
value: "signup",
|
|
|
|
href: "/signup"
|
|
|
|
},
|
|
|
|
...defaultPages
|
2022-11-14 04:28:40 -05:00
|
|
|
]
|
|
|
|
}
|
2022-04-02 03:45:26 -04:00
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const pages = getPages()
|
|
|
|
|
|
|
|
const onTabChange = (tab: string) => {
|
|
|
|
if (typeof window === "undefined") return
|
|
|
|
const match = pages.find((page) => page.value === tab)
|
|
|
|
if (match?.onClick) {
|
|
|
|
match.onClick()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const getButton = (tab: Tab) => {
|
2022-11-15 23:50:54 -05:00
|
|
|
const isActive = pathname === tab.href
|
|
|
|
const activeStyle = isActive ? styles.active : ""
|
2022-11-14 04:28:40 -05:00
|
|
|
if (tab.onClick) {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
key={tab.value}
|
2022-11-15 23:50:54 -05:00
|
|
|
iconLeft={tab.icon}
|
2022-11-14 04:28:40 -05:00
|
|
|
onClick={() => onTabChange(tab.value)}
|
2022-11-20 23:54:34 -05:00
|
|
|
className={clsx(styles.tab, activeStyle)}
|
2022-11-15 23:50:54 -05:00
|
|
|
aria-label={tab.name}
|
|
|
|
aria-current={isActive ? "page" : undefined}
|
2022-11-14 04:28:40 -05:00
|
|
|
>
|
|
|
|
{tab.name ? tab.name : undefined}
|
|
|
|
</Button>
|
|
|
|
)
|
|
|
|
} else if (tab.href) {
|
|
|
|
return (
|
2022-11-15 23:50:54 -05:00
|
|
|
<Link
|
|
|
|
key={tab.value}
|
|
|
|
href={tab.href}
|
2022-11-20 23:54:34 -05:00
|
|
|
className={clsx(styles.tab, activeStyle)}
|
2022-11-15 23:50:54 -05:00
|
|
|
>
|
|
|
|
<Button iconLeft={tab.icon}>{tab.name ? tab.name : undefined}</Button>
|
2022-11-14 04:28:40 -05:00
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 03:45:26 -04:00
|
|
|
|
2022-11-14 04:28:40 -05:00
|
|
|
const buttons = pages.map(getButton)
|
2022-04-02 03:45:26 -04:00
|
|
|
|
2022-04-09 20:48:19 -04:00
|
|
|
return (
|
2022-11-28 21:33:06 -05:00
|
|
|
<header>
|
2022-04-09 20:48:19 -04:00
|
|
|
<div className={styles.tabs}>
|
|
|
|
<div className={styles.buttons}>{buttons}</div>
|
|
|
|
</div>
|
|
|
|
<div className={styles.controls}>
|
2022-11-15 23:50:54 -05:00
|
|
|
<Button onClick={() => setExpanded(!expanded)} aria-label="Menu">
|
2022-11-29 03:43:04 -05:00
|
|
|
<Menu />
|
2022-04-09 20:48:19 -04:00
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{/* setExpanded should occur elsewhere; we don't want to close if they change themes */}
|
|
|
|
{isMobile && expanded && (
|
|
|
|
<div className={styles.mobile} onClick={() => setExpanded(!expanded)}>
|
2022-11-15 23:50:54 -05:00
|
|
|
{buttons}
|
2022-04-09 20:48:19 -04:00
|
|
|
</div>
|
|
|
|
)}
|
2022-11-28 21:33:06 -05:00
|
|
|
</header>
|
2022-04-09 20:48:19 -04:00
|
|
|
)
|
2022-04-02 03:45:26 -04:00
|
|
|
}
|
2022-03-26 03:05:05 -04:00
|
|
|
|
2022-11-12 03:58:21 -05:00
|
|
|
export default Header
|