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

203 lines
4.1 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 styles from "./header.module.css"
2022-03-06 19:46:59 -05:00
// import useUserData from "@lib/hooks/use-user-data"
import Link from "@components/link"
2022-11-09 21:38:05 -05:00
import { usePathname } from "next/navigation"
import { signOut, useSession } from "next-auth/react"
import Button from "@components/button"
import clsx from "clsx"
import { useTheme } from "@wits/next-themes"
import {
GitHub,
Home,
Menu,
Moon,
PlusCircle,
Settings,
Sun,
User,
UserPlus,
UserX
} from "react-feather"
import * as DropdownMenu from "@radix-ui/react-dropdown-menu"
import buttonStyles from "@components/button/button.module.css"
import { useEffect, useMemo, useState } from "react"
type Tab = {
2022-04-09 20:48:19 -04:00
name: string
icon: JSX.Element
value: string
onClick?: () => void
href?: string
}
const Header = () => {
const session = useSession()
2022-12-04 04:31:51 -05:00
const isSignedIn = session?.status === "authenticated"
const isAdmin = session?.data?.user?.role === "admin"
2022-12-04 04:31:51 -05:00
const isLoading = session?.status === "loading"
2022-11-09 21:38:05 -05:00
const pathname = usePathname()
const { setTheme, resolvedTheme } = useTheme()
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={tab.onClick}
className={clsx(styles.tab, activeStyle)}
aria-label={tab.name}
aria-current={isActive ? "page" : undefined}
data-tab={tab.value}
>
{tab.name ? tab.name : undefined}
</Button>
)
} else if (tab.href) {
return (
<Link
key={tab.value}
href={tab.href}
className={clsx(styles.tab, activeStyle)}
data-tab={tab.value}
>
<Button iconLeft={tab.icon}>{tab.name ? tab.name : undefined}</Button>
</Link>
)
2022-04-09 20:48:19 -04:00
}
}
const pages = useMemo(() => {
2022-04-09 20:48:19 -04:00
const defaultPages: Tab[] = [
{
name: "GitHub",
2022-04-09 20:48:19 -04:00
href: "https://github.com/maxleiter/drift",
icon: <GitHub />,
2022-04-09 20:48:19 -04:00
value: "github"
}
]
if (isAdmin) {
defaultPages.push({
name: "Admin",
icon: <Settings />,
value: "admin",
href: "/admin"
})
}
defaultPages.push({
name: "Theme",
onClick: function () {
setTheme(resolvedTheme === "light" ? "dark" : "light")
},
icon: resolvedTheme === "light" ? <Moon /> : <Sun />,
value: "theme"
})
2022-12-04 04:31:51 -05:00
if (isSignedIn)
return [
2022-04-09 20:48:19 -04:00
{
name: "New",
icon: <PlusCircle />,
2022-04-09 20:48:19 -04:00
value: "new",
href: "/new"
},
{
name: "Yours",
icon: <User />,
2022-04-09 20:48:19 -04:00
value: "yours",
href: "/mine"
},
{
name: "Settings",
icon: <Settings />,
value: "settings",
href: "/settings"
},
...defaultPages,
2022-04-09 20:48:19 -04:00
{
name: "Sign Out",
icon: <UserX />,
2022-04-09 20:48:19 -04:00
value: "signout",
onClick: () =>
signOut({
callbackUrl: "/"
})
}
]
2022-04-09 20:48:19 -04:00
else
return [
2022-04-09 20:48:19 -04:00
{
name: "Home",
icon: <Home />,
2022-04-09 20:48:19 -04:00
value: "home",
href: "/"
},
...defaultPages,
2022-04-09 20:48:19 -04:00
{
name: "Sign in",
icon: <User />,
2022-04-09 20:48:19 -04:00
value: "signin",
href: "/signin"
},
{
name: "Sign up",
icon: <UserPlus />,
2022-04-09 20:48:19 -04:00
value: "signup",
href: "/signup"
}
]
2022-12-04 04:31:51 -05:00
}, [isAdmin, resolvedTheme, isSignedIn, setTheme])
// // TODO: this should not be necessary.
// if (!clientHydrated) {
// return (
// <header>
// <div className={styles.tabs}>{getPages(true).map(getButton)}</div>
// </header>
// )
// }
const buttons = pages.map(getButton)
2022-04-09 20:48:19 -04:00
return (
2022-12-04 04:31:51 -05:00
<header className={clsx(styles.header, {
[styles.loading]: isLoading,
})}>
2022-04-09 20:48:19 -04:00
<div className={styles.tabs}>
<div className={styles.buttons}>{buttons}</div>
</div>
<DropdownMenu.Root>
<DropdownMenu.Trigger
className={clsx(buttonStyles.button, styles.mobile)}
asChild
>
<Button aria-label="Menu">
<Menu />
</Button>
</DropdownMenu.Trigger>
<DropdownMenu.Portal>
<DropdownMenu.Content className={styles.contentWrapper}>
{buttons.map((button) => (
<DropdownMenu.Item
key={button?.key}
className={styles.dropdownItem}
>
{button}
</DropdownMenu.Item>
))}
</DropdownMenu.Content>
</DropdownMenu.Portal>
</DropdownMenu.Root>
</header>
2022-04-09 20:48:19 -04:00
)
}
2022-11-12 03:58:21 -05:00
export default Header