2021-12-20 03:12:12 +00:00
|
|
|
import Link from 'next/link'
|
|
|
|
|
2022-02-01 01:02:17 +00:00
|
|
|
import {
|
|
|
|
HomeIcon,
|
2022-03-31 05:35:20 +00:00
|
|
|
MenuAlt3Icon,
|
2022-02-01 01:02:17 +00:00
|
|
|
SearchIcon,
|
2022-03-31 05:35:20 +00:00
|
|
|
XIcon,
|
2022-02-01 01:02:17 +00:00
|
|
|
} from '@heroicons/react/outline'
|
2022-03-31 05:35:20 +00:00
|
|
|
import { Transition, Dialog } from '@headlessui/react'
|
|
|
|
import { useState, Fragment } from 'react'
|
2022-09-17 23:40:45 +00:00
|
|
|
import Sidebar from './sidebar'
|
|
|
|
import { Item } from './sidebar-item'
|
2022-08-24 21:42:09 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { formatMoney } from 'common/util/format'
|
2022-04-04 21:49:14 +00:00
|
|
|
import { Avatar } from '../avatar'
|
2022-05-18 19:45:08 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import { useRouter } from 'next/router'
|
2022-06-01 13:11:25 +00:00
|
|
|
import NotificationsIcon from 'web/components/notifications-icon'
|
2022-05-28 04:27:37 +00:00
|
|
|
import { useIsIframe } from 'web/hooks/use-is-iframe'
|
2022-06-15 21:34:34 +00:00
|
|
|
import { trackCallback } from 'web/lib/service/analytics'
|
2022-09-16 13:32:15 +00:00
|
|
|
import { User } from 'common/user'
|
|
|
|
|
2022-06-29 17:33:20 +00:00
|
|
|
function getNavigation() {
|
2022-05-18 19:45:08 +00:00
|
|
|
return [
|
|
|
|
{ name: 'Home', href: '/home', icon: HomeIcon },
|
2022-09-16 22:24:30 +00:00
|
|
|
{ name: 'Search', href: '/search', icon: SearchIcon },
|
2022-06-01 13:11:25 +00:00
|
|
|
{
|
|
|
|
name: 'Notifications',
|
|
|
|
href: `/notifications`,
|
|
|
|
icon: NotificationsIcon,
|
|
|
|
},
|
2022-05-18 19:45:08 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
const signedOutNavigation = [
|
|
|
|
{ name: 'Home', href: '/', icon: HomeIcon },
|
2022-09-16 22:24:30 +00:00
|
|
|
{ name: 'Explore', href: '/search', icon: SearchIcon },
|
2022-05-18 19:45:08 +00:00
|
|
|
]
|
2022-03-31 03:51:15 +00:00
|
|
|
|
2022-09-16 13:32:15 +00:00
|
|
|
export const userProfileItem = (user: User) => ({
|
|
|
|
name: formatMoney(user.balance),
|
|
|
|
trackingEventName: 'profile',
|
2022-09-27 21:16:48 +00:00
|
|
|
href: `/${user.username}?tab=portfolio`,
|
2022-09-16 13:32:15 +00:00
|
|
|
icon: () => (
|
|
|
|
<Avatar
|
|
|
|
className="mx-auto my-1"
|
|
|
|
size="xs"
|
|
|
|
username={user.username}
|
|
|
|
avatarUrl={user.avatarUrl}
|
|
|
|
noLink
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
})
|
|
|
|
|
2022-03-31 03:51:15 +00:00
|
|
|
// From https://codepen.io/chris__sev/pen/QWGvYbL
|
2022-03-31 05:35:20 +00:00
|
|
|
export function BottomNavBar() {
|
|
|
|
const [sidebarOpen, setSidebarOpen] = useState(false)
|
2022-04-04 21:49:14 +00:00
|
|
|
|
2022-05-18 19:45:08 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const currentPage = router.pathname
|
|
|
|
|
2022-04-04 21:49:14 +00:00
|
|
|
const user = useUser()
|
|
|
|
|
2022-05-28 04:27:37 +00:00
|
|
|
const isIframe = useIsIframe()
|
|
|
|
if (isIframe) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2022-05-18 19:45:08 +00:00
|
|
|
const navigationOptions =
|
2022-06-29 17:33:20 +00:00
|
|
|
user === null ? signedOutNavigation : getNavigation()
|
2022-05-18 19:45:08 +00:00
|
|
|
|
2022-02-01 01:02:17 +00:00
|
|
|
return (
|
2022-03-31 05:35:20 +00:00
|
|
|
<nav className="fixed inset-x-0 bottom-0 z-20 flex justify-between border-t-2 bg-white text-xs text-gray-700 lg:hidden">
|
2022-05-18 19:45:08 +00:00
|
|
|
{navigationOptions.map((item) => (
|
|
|
|
<NavBarItem key={item.name} item={item} currentPage={currentPage} />
|
|
|
|
))}
|
2022-06-29 17:33:20 +00:00
|
|
|
|
|
|
|
{user && (
|
|
|
|
<NavBarItem
|
|
|
|
key={'profile'}
|
|
|
|
currentPage={currentPage}
|
2022-09-16 13:32:15 +00:00
|
|
|
item={userProfileItem(user)}
|
2022-06-29 17:33:20 +00:00
|
|
|
/>
|
|
|
|
)}
|
2022-04-04 21:49:14 +00:00
|
|
|
<div
|
2022-04-10 05:26:19 +00:00
|
|
|
className="w-full select-none py-1 px-3 text-center hover:cursor-pointer hover:bg-indigo-200 hover:text-indigo-700"
|
2022-03-31 05:35:20 +00:00
|
|
|
onClick={() => setSidebarOpen(true)}
|
|
|
|
>
|
2022-07-07 21:52:28 +00:00
|
|
|
<MenuAlt3Icon className=" my-1 mx-auto h-6 w-6" aria-hidden="true" />
|
2022-08-24 21:42:09 +00:00
|
|
|
More
|
2022-04-04 21:49:14 +00:00
|
|
|
</div>
|
2022-03-31 05:35:20 +00:00
|
|
|
|
|
|
|
<MobileSidebar
|
|
|
|
sidebarOpen={sidebarOpen}
|
|
|
|
setSidebarOpen={setSidebarOpen}
|
|
|
|
/>
|
2021-12-20 03:12:12 +00:00
|
|
|
</nav>
|
|
|
|
)
|
|
|
|
}
|
2022-03-31 03:51:15 +00:00
|
|
|
|
2022-05-18 19:45:08 +00:00
|
|
|
function NavBarItem(props: { item: Item; currentPage: string }) {
|
|
|
|
const { item, currentPage } = props
|
2022-07-04 03:43:18 +00:00
|
|
|
const track = trackCallback(`navbar: ${item.trackingEventName ?? item.name}`)
|
2022-05-18 19:45:08 +00:00
|
|
|
|
|
|
|
return (
|
2022-09-16 13:32:15 +00:00
|
|
|
<Link href={item.href ?? '#'}>
|
2022-05-18 19:45:08 +00:00
|
|
|
<a
|
|
|
|
className={clsx(
|
|
|
|
'block w-full py-1 px-3 text-center hover:bg-indigo-200 hover:text-indigo-700',
|
2022-05-23 21:49:30 +00:00
|
|
|
currentPage === item.href && 'bg-gray-200 text-indigo-700'
|
2022-05-18 19:45:08 +00:00
|
|
|
)}
|
2022-07-04 03:43:18 +00:00
|
|
|
onClick={track}
|
2022-05-18 19:45:08 +00:00
|
|
|
>
|
2022-06-29 17:33:20 +00:00
|
|
|
{item.icon && <item.icon className="my-1 mx-auto h-6 w-6" />}
|
2022-05-18 19:45:08 +00:00
|
|
|
{item.name}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-31 05:35:20 +00:00
|
|
|
// Sidebar that slides out on mobile
|
|
|
|
export function MobileSidebar(props: {
|
|
|
|
sidebarOpen: boolean
|
|
|
|
setSidebarOpen: (open: boolean) => void
|
2022-03-31 03:51:15 +00:00
|
|
|
}) {
|
2022-03-31 05:35:20 +00:00
|
|
|
const { sidebarOpen, setSidebarOpen } = props
|
2022-03-31 03:51:15 +00:00
|
|
|
return (
|
2022-03-31 05:35:20 +00:00
|
|
|
<div>
|
|
|
|
<Transition.Root show={sidebarOpen} as={Fragment}>
|
|
|
|
<Dialog
|
|
|
|
as="div"
|
|
|
|
className="fixed inset-0 z-40 flex"
|
|
|
|
onClose={setSidebarOpen}
|
2022-03-31 03:51:15 +00:00
|
|
|
>
|
2022-03-31 05:35:20 +00:00
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="transition-opacity ease-linear duration-300"
|
|
|
|
enterFrom="opacity-0"
|
|
|
|
enterTo="opacity-100"
|
|
|
|
leave="transition-opacity ease-linear duration-300"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
|
|
|
<Dialog.Overlay className="fixed inset-0 bg-gray-600 bg-opacity-75" />
|
|
|
|
</Transition.Child>
|
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="transition ease-in-out duration-300 transform"
|
|
|
|
enterFrom="-translate-x-full"
|
|
|
|
enterTo="translate-x-0"
|
|
|
|
leave="transition ease-in-out duration-300 transform"
|
|
|
|
leaveFrom="translate-x-0"
|
|
|
|
leaveTo="-translate-x-full"
|
2022-03-31 03:51:15 +00:00
|
|
|
>
|
2022-07-11 23:40:25 +00:00
|
|
|
<div className="relative flex w-full max-w-xs flex-1 flex-col bg-white">
|
2022-03-31 05:35:20 +00:00
|
|
|
<Transition.Child
|
|
|
|
as={Fragment}
|
|
|
|
enter="ease-in-out duration-300"
|
|
|
|
enterFrom="opacity-0"
|
|
|
|
enterTo="opacity-100"
|
|
|
|
leave="ease-in-out duration-300"
|
|
|
|
leaveFrom="opacity-100"
|
|
|
|
leaveTo="opacity-0"
|
|
|
|
>
|
|
|
|
<div className="absolute top-0 right-0 -mr-12 pt-2">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="ml-1 flex h-10 w-10 items-center justify-center rounded-full focus:outline-none focus:ring-2 focus:ring-inset focus:ring-white"
|
|
|
|
onClick={() => setSidebarOpen(false)}
|
|
|
|
>
|
|
|
|
<span className="sr-only">Close sidebar</span>
|
|
|
|
<XIcon className="h-6 w-6 text-white" aria-hidden="true" />
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</Transition.Child>
|
2022-07-11 23:40:25 +00:00
|
|
|
<div className="mx-2 h-0 flex-1 overflow-y-auto">
|
2022-05-13 23:47:50 +00:00
|
|
|
<Sidebar className="pl-2" />
|
2022-03-31 05:35:20 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Transition.Child>
|
|
|
|
<div className="w-14 flex-shrink-0" aria-hidden="true">
|
|
|
|
{/* Dummy element to force sidebar to shrink to fit close icon */}
|
|
|
|
</div>
|
|
|
|
</Dialog>
|
|
|
|
</Transition.Root>
|
|
|
|
</div>
|
2022-03-31 03:51:15 +00:00
|
|
|
)
|
|
|
|
}
|