manifold/web/components/header.tsx

171 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-12-10 22:56:26 +00:00
import clsx from 'clsx'
import Link from 'next/link'
2021-12-18 01:13:12 +00:00
import Image from 'next/image'
2021-12-11 00:19:35 +00:00
2021-12-09 23:37:26 +00:00
import { useUser } from '../hooks/use-user'
2021-12-17 23:40:17 +00:00
import { formatMoney } from '../lib/util/format'
import { Row } from './layout/row'
import { firebaseLogin, firebaseLogout, User } from '../lib/firebase/users'
import { MenuButton } from './menu'
2021-12-16 09:53:19 +00:00
const hoverClasses =
'hover:underline hover:decoration-indigo-400 hover:decoration-2'
function getNavigationOptions(user: User, options: { mobile: boolean }) {
const { mobile } = options
return [
{
name: 'Home',
href: '/',
},
...(mobile
? [
2021-12-19 23:39:15 +00:00
{ name: 'About', href: '/about' },
{
name: 'Create a market',
href: '/create',
},
]
: []),
{
name: 'Your bets',
href: '/bets',
},
{
name: 'Your markets',
href: `/${user.username}`,
},
{
name: 'Sign out',
onClick: () => firebaseLogout(),
},
]
}
function ProfileSummary(props: { user: User }) {
const { user } = props
return (
<Row className="avatar items-center">
<div className="rounded-full w-10 h-10 mr-4">
<Image src={user.avatarUrl} width={40} height={40} />
</div>
<div className="truncate" style={{ maxWidth: 175 }}>
{user.name}
<div className="text-gray-700 text-sm">{formatMoney(user.balance)}</div>
</div>
</Row>
)
}
2021-12-17 23:40:17 +00:00
function SignedInHeaders(props: { user: User; themeClasses?: string }) {
const { user, themeClasses } = props
2021-12-10 22:56:26 +00:00
2021-12-17 23:40:17 +00:00
return (
<>
<Link href="/about">
<a
className={clsx(
2021-12-19 01:23:34 +00:00
'text-base hidden md:block whitespace-nowrap',
themeClasses
)}
>
2021-12-19 23:39:15 +00:00
About
2021-12-17 23:40:17 +00:00
</a>
</Link>
<Link href="/create">
<a
className={clsx(
2021-12-19 01:23:34 +00:00
'text-base hidden md:block whitespace-nowrap',
themeClasses
)}
>
Create a market
</a>
2021-12-17 23:40:17 +00:00
</Link>
<MenuButton
className="hidden md:block"
menuItems={getNavigationOptions(user, { mobile: false })}
buttonContent={<ProfileSummary user={user} />}
/>
<MenuButton
className="md:hidden"
menuItems={getNavigationOptions(user, { mobile: true })}
buttonContent={<ProfileSummary user={user} />}
/>
2021-12-17 23:40:17 +00:00
</>
)
}
function SignedOutHeaders(props: { themeClasses?: string }) {
const { themeClasses } = props
2021-12-10 22:56:26 +00:00
return (
<>
2021-12-18 01:29:09 +00:00
<div
className={clsx(
'text-base font-medium cursor-pointer whitespace-nowrap',
themeClasses
)}
2021-12-18 01:29:09 +00:00
onClick={firebaseLogin}
>
Sign in
</div>
</>
)
}
export function Header(props: {
darkBackground?: boolean
className?: string
children?: any
}) {
const { darkBackground, className, children } = props
2021-12-10 22:56:26 +00:00
2021-12-17 23:40:17 +00:00
const user = useUser()
const themeClasses = clsx(darkBackground && 'text-white', hoverClasses)
return (
2021-12-17 23:40:17 +00:00
<nav
className={clsx(
'max-w-7xl w-full flex flex-row justify-between md:justify-start pt-5 pb-4',
className
)}
2021-12-17 23:40:17 +00:00
aria-label="Global"
>
<Link href="/">
<a className="flex flex-row gap-3">
<img
className="sm:h-10 sm:w-10 hover:rotate-12 transition-all"
2021-12-17 23:40:17 +00:00
src="/logo-icon.svg"
width={40}
height={40}
/>
<div
className={clsx(
2021-12-19 23:47:13 +00:00
'font-major-mono mt-1 sm:text-2xl md:whitespace-nowrap',
2021-12-17 23:40:17 +00:00
darkBackground && 'text-white'
)}
>
Mantic Markets
</div>
2021-12-17 23:40:17 +00:00
</a>
</Link>
<Row className="gap-6 sm:gap-8 mt-1 md:ml-16">
2021-12-17 23:40:17 +00:00
{children}
2021-12-17 23:40:17 +00:00
{user ? (
<SignedInHeaders user={user} themeClasses={themeClasses} />
) : (
<SignedOutHeaders themeClasses={themeClasses} />
)}
</Row>
</nav>
)
}