"use client"
import { useSelectedLayoutSegment, useSelectedLayoutSegments } from "next/navigation"
import FadeIn from "@components/fade-in"
import { setDriftTheme } from "src/app/lib/set-theme"
import {
Home,
Moon,
PlusCircle,
Settings,
Sun,
User,
UserX
} from "react-feather"
import { signOut } from "next-auth/react"
import Button from "@components/button"
import Link from "@components/link"
import { useSessionSWR } from "@lib/use-session-swr"
import { useTheme } from "next-themes"
import styles from "./buttons.module.css"
// constant width for sign in / sign out buttons to avoid CLS
const SIGN_IN_WIDTH = 110
type Tab = {
name: string
icon: JSX.Element
value: string
width?: number
} & (
| {
onClick: () => void
href?: undefined
}
| {
onClick?: undefined
href: string
}
)
export function HeaderButtons({
isAuthenticated,
theme: initialTheme
}: {
isAuthenticated: boolean
theme: string
}) {
const { isAdmin, userId } = useSessionSWR()
const { resolvedTheme } = useTheme();
return getButtons({
isAuthenticated,
theme: resolvedTheme ? resolvedTheme : initialTheme,
isAdmin,
userId
})
}
function NavButton(tab: Tab) {
const segment = useSelectedLayoutSegments().slice(-1)[0]
const isActive = segment === tab.value.toLowerCase()
const activeStyle = isActive ? styles.active : undefined
if (tab.onClick) {
return (
)
} else {
return (
)
}
}
function ThemeButton({ theme }: { theme: string }) {
const { setTheme } = useTheme()
return (
: }
value="dark"
onClick={() => {
setDriftTheme(theme === "dark" ? "light" : "dark", setTheme)
}}
key="theme"
/>
)
}
/** For use by mobile */
export function getButtons({
isAuthenticated,
theme,
// mutate: mutateSession,
isAdmin,
userId
}: {
isAuthenticated: boolean
theme: string
// mutate: KeyedMutator
isAdmin?: boolean,
userId?: string
}) {
return [
}
value="home"
href="/home"
/>,
}
value="new"
href="/new"
/>,
}
value="mine"
href="/mine"
/>,
}
value="settings"
href="/settings"
key="settings"
/>,
,
isAuthenticated === true ? (
}
value="signout"
onClick={() => {
signOut({
callbackUrl: `/signedout${
userId ? "?userId=" + userId : ""
}`
})
}}
width={SIGN_IN_WIDTH}
/>
) : undefined,
isAuthenticated === false ? (
}
value="signin"
href="/signin"
width={SIGN_IN_WIDTH}
/>
) : undefined,
isAdmin ? (
}
value="admin"
href="/admin"
/>
) : undefined
].filter(Boolean)
}