CoastalCommitsPastes/client/app/components/header/index.tsx

198 lines
4.4 KiB
TypeScript
Raw Normal View History

2022-11-09 21:38:05 -05:00
"use client"
2022-04-09 20:48:19 -04:00
import {
Page,
useBodyScroll,
useMediaQuery
2022-11-09 21:38:05 -05:00
} from "@geist-ui/core/dist"
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-04-09 20:48:19 -04:00
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"
import SettingsIcon from "@geist-ui/icons/settings"
import SunIcon from "@geist-ui/icons/sun"
// 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"
import { useTheme } from "@components/theme/ThemeClientContextProvider"
import Button from "@components/button"
type Tab = {
2022-04-09 20:48:19 -04:00
name: string
icon: JSX.Element
value: string
onClick?: () => void
href?: string
}
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" })
// const { status } = useSession()
// const signedIn = status === "authenticated"
const { setTheme, theme } = useTheme()
2022-04-09 20:48:19 -04:00
useEffect(() => {
setBodyHidden(expanded)
}, [expanded, setBodyHidden])
2022-04-09 20:48:19 -04:00
useEffect(() => {
if (!isMobile) {
setExpanded(false)
}
}, [isMobile])
const getPages = () => {
2022-04-09 20:48:19 -04:00
const defaultPages: Tab[] = [
{
name: isMobile ? "GitHub" : "",
href: "https://github.com/maxleiter/drift",
icon: <GitHubIcon />,
value: "github"
},
{
name: isMobile ? "Change theme" : "",
onClick: function () {
if (typeof window !== "undefined")
setTheme(theme === "light" ? "dark" : "light")
2022-04-09 20:48:19 -04:00
},
icon: theme === "light" ? <MoonIcon /> : <SunIcon />,
2022-04-09 20:48:19 -04:00
value: "theme"
}
]
if (isAdmin) {
defaultPages.push({
name: "Admin",
icon: <SettingsIcon />,
value: "admin",
href: "/admin"
})
}
if (signedIn)
return [
2022-04-09 20:48:19 -04:00
{
name: "New",
2022-04-09 20:48:19 -04:00
icon: <NewIcon />,
value: "new",
href: "/new"
},
{
name: "Yours",
2022-04-09 20:48:19 -04:00
icon: <YourIcon />,
value: "yours",
href: "/mine"
},
{
name: "Settings",
icon: <SettingsIcon />,
value: "settings",
href: "/settings"
},
2022-04-09 20:48:19 -04:00
{
name: "Sign Out",
2022-04-09 20:48:19 -04:00
icon: <SignOutIcon />,
value: "signout",
2022-11-12 03:58:21 -05:00
onClick: () => signOut()
2022-04-09 20:48:19 -04:00
},
...defaultPages
]
2022-04-09 20:48:19 -04:00
else
return [
2022-04-09 20:48:19 -04:00
{
name: "Home",
2022-04-09 20:48:19 -04:00
icon: <HomeIcon />,
value: "home",
href: "/"
},
{
name: "Sign in",
icon: <SignInIcon />,
value: "signin",
href: "/signin"
},
{
name: "Sign up",
icon: <SignUpIcon />,
value: "signup",
href: "/signup"
},
...defaultPages
]
}
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) => {
const isActive = pathname === tab.href
const activeStyle = isActive ? styles.active : ""
if (tab.onClick) {
return (
<Button
key={tab.value}
iconLeft={tab.icon}
onClick={() => onTabChange(tab.value)}
className={`${styles.tab} ${activeStyle}`}
aria-label={tab.name}
aria-current={isActive ? "page" : undefined}
>
{tab.name ? tab.name : undefined}
</Button>
)
} else if (tab.href) {
return (
<Link
key={tab.value}
href={tab.href}
className={`${styles.tab} ${activeStyle}`}
>
<Button iconLeft={tab.icon}>{tab.name ? tab.name : undefined}</Button>
</Link>
)
}
}
const buttons = pages.map(getButton)
2022-04-09 20:48:19 -04:00
return (
<Page.Header>
<div className={styles.tabs}>
<div className={styles.buttons}>{buttons}</div>
</div>
<div className={styles.controls}>
<Button onClick={() => setExpanded(!expanded)} aria-label="Menu">
2022-04-09 20:48:19 -04:00
<MenuIcon />
</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)}>
{buttons}
2022-04-09 20:48:19 -04:00
</div>
)}
</Page.Header>
)
}
2022-11-12 03:58:21 -05:00
export default Header