manifold/web/components/header.tsx

148 lines
3.3 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'
2021-12-18 01:29:09 +00:00
import { firebaseLogin, 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'
const mobileNavigation = [
{
name: 'Home',
href: '/',
},
{
name: 'Account',
href: '/account',
},
{
name: 'Your bets',
href: '/bets',
},
{
name: 'Create a market',
href: '/create',
},
]
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="/create">
<a
className={clsx(
'text-base font-medium hidden md:block',
themeClasses
)}
>
2021-12-17 23:40:17 +00:00
Create a market
</a>
</Link>
2021-12-17 23:40:17 +00:00
<Link href="/bets">
<a
className={clsx(
'text-base font-medium hidden md:block',
themeClasses
)}
>
Your bets
</a>
2021-12-17 23:40:17 +00:00
</Link>
<Link href="/account">
<a className={clsx('text-base font-medium hidden md:block')}>
<ProfileSummary user={user} />
2021-12-17 23:40:17 +00:00
</a>
</Link>
<MenuButton
className="md:hidden"
menuItems={mobileNavigation}
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', themeClasses)}
onClick={firebaseLogin}
>
Sign in
</div>
</>
)
}
export function Header(props: { darkBackground?: boolean; children?: any }) {
const { darkBackground, 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="max-w-7xl w-full flex flex-row justify-between md:justify-start mx-auto pt-5 px-4 sm:px-6"
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(
'font-major-mono lowercase mt-1 sm:text-2xl',
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-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>
)
}