mango-v4-ui/components/SideNav.tsx

357 lines
10 KiB
TypeScript
Raw Normal View History

2022-07-14 16:36:31 -07:00
import Link from 'next/link'
2022-08-02 19:15:17 -07:00
import TradeIcon from './icons/TradeIcon'
2022-07-14 16:36:31 -07:00
import {
ChartBarIcon,
DotsHorizontalIcon,
LibraryIcon,
LightBulbIcon,
ExternalLinkIcon,
ChevronDownIcon,
ReceiptTaxIcon,
HomeIcon,
2022-07-18 20:58:21 -07:00
CogIcon,
2022-07-14 16:36:31 -07:00
} from '@heroicons/react/solid'
import { useRouter } from 'next/router'
import { useTranslation } from 'next-i18next'
import { Fragment, ReactNode, useEffect, useState } from 'react'
import { Disclosure, Popover, Transition } from '@headlessui/react'
2022-08-02 19:15:17 -07:00
import MangoAccountSummary from './account/MangoAccountSummary'
import HealthHeart from './account/HealthHeart'
import mangoStore from '../store/state'
2022-07-14 16:36:31 -07:00
const SideNav = ({ collapsed }: { collapsed: boolean }) => {
const { t } = useTranslation('common')
2022-08-02 19:15:17 -07:00
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
2022-07-14 16:36:31 -07:00
const router = useRouter()
const { pathname } = router
return (
<div
className={`flex flex-col justify-between transition-all duration-500 ease-in-out ${
2022-07-17 17:56:05 -07:00
collapsed ? 'w-[72px]' : 'w-44 lg:w-56'
2022-07-14 16:36:31 -07:00
} min-h-screen border-r border-th-bkg-3 bg-th-bkg-1`}
>
<div className="my-2">
<Link href={'/'} shallow={true} passHref>
2022-07-14 16:36:31 -07:00
<div
className={`h-14 items-center transition-all duration-500 ease-in-out ${
collapsed ? 'justify-center' : 'justify-start'
2022-07-17 17:56:05 -07:00
} px-4 py-1`}
2022-07-14 16:36:31 -07:00
>
<div className={`flex flex-shrink-0 cursor-pointer items-center`}>
<img
2022-07-17 17:56:05 -07:00
className={`ml-0.5 h-9 w-auto`}
2022-07-14 16:36:31 -07:00
src="/logos/logo-mark.svg"
alt="next"
/>
<Transition
show={!collapsed}
as={Fragment}
enter="transition-all ease-in duration-200"
2022-07-14 16:36:31 -07:00
enterFrom="opacity-50"
enterTo="opacity-100"
leave="transition ease-out duration-200"
2022-07-14 16:36:31 -07:00
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<span className="ml-4 text-lg font-bold text-th-fgd-1">
2022-07-14 16:36:31 -07:00
Mango
</span>
</Transition>
</div>
</div>
</Link>
2022-07-17 17:56:05 -07:00
<div className="flex flex-col items-start">
2022-07-14 16:36:31 -07:00
<MenuItem
active={pathname === '/'}
collapsed={collapsed}
icon={<HomeIcon className="h-6 w-6" />}
2022-07-14 16:36:31 -07:00
title={t('portfolio')}
pagePath="/"
/>
<MenuItem
active={pathname === '/trade'}
collapsed={collapsed}
icon={<TradeIcon className="h-6 w-6" />}
2022-07-14 16:36:31 -07:00
title={t('trade')}
pagePath="/trade"
/>
<MenuItem
active={pathname === '/stats'}
collapsed={collapsed}
icon={<ChartBarIcon className="h-6 w-6" />}
2022-07-14 16:36:31 -07:00
title={t('stats')}
pagePath="/stats"
/>
2022-07-18 20:58:21 -07:00
<MenuItem
active={pathname === '/settings'}
collapsed={collapsed}
icon={<CogIcon className="h-6 w-6" />}
title={t('settings')}
pagePath="/settings"
/>
2022-07-14 16:36:31 -07:00
<ExpandableMenuItem
collapsed={collapsed}
icon={<DotsHorizontalIcon className="h-6 w-6" />}
2022-07-14 16:36:31 -07:00
title={t('more')}
>
<MenuItem
active={pathname === '/fees'}
collapsed={false}
2022-07-17 17:56:05 -07:00
icon={<ReceiptTaxIcon className="h-5 w-5" />}
2022-07-14 16:36:31 -07:00
title={t('fees')}
pagePath="/fees"
hideIconBg
/>
<MenuItem
collapsed={false}
2022-07-17 17:56:05 -07:00
icon={<LightBulbIcon className="h-5 w-5" />}
2022-07-14 16:36:31 -07:00
title={t('learn')}
pagePath="https://docs.mango.markets"
hideIconBg
isExternal
/>
<MenuItem
collapsed={false}
2022-07-17 17:56:05 -07:00
icon={<LibraryIcon className="h-5 w-5" />}
2022-07-14 16:36:31 -07:00
title={t('governance')}
pagePath="https://dao.mango.markets"
hideIconBg
isExternal
/>
</ExpandableMenuItem>
</div>
</div>
2022-08-02 19:15:17 -07:00
{mangoAccount ? (
<div className="border-t border-th-bkg-3">
<ExpandableMenuItem
collapsed={collapsed}
icon={<HealthHeart health={50} size={32} />}
title={
<div className="text-left">
<p className="whitespace-nowrap text-xs">Account Summary</p>
<p className="text-sm font-bold text-th-fgd-1">
{mangoAccount.name}
</p>
</div>
}
alignBottom
hideIconBg
>
<div className="px-4 pb-4 pt-2">
<MangoAccountSummary />
</div>
</ExpandableMenuItem>
</div>
) : null}
2022-07-14 16:36:31 -07:00
</div>
)
}
export default SideNav
const MenuItem = ({
active,
collapsed,
icon,
title,
pagePath,
hideIconBg,
isExternal,
}: {
active?: boolean
collapsed: boolean
icon: ReactNode
title: string
pagePath: string
hideIconBg?: boolean
isExternal?: boolean
}) => {
return (
2022-07-14 16:36:31 -07:00
<Link href={pagePath} shallow={true}>
<a
2022-07-17 17:56:05 -07:00
className={`flex cursor-pointer px-4 hover:brightness-[1.1] ${
active ? 'text-th-primary' : 'text-th-fgd-1'
2022-07-17 17:56:05 -07:00
} ${hideIconBg ? 'py-1' : 'py-2'}`}
>
<div className="flex w-full items-center justify-between">
<div className="flex items-center">
<div
className={
hideIconBg
? ''
: 'flex h-10 w-10 items-center justify-center rounded-full bg-th-bkg-3'
}
>
{icon}
</div>
<Transition
show={!collapsed}
as={Fragment}
enter="transition-all ease-in duration-300"
enterFrom="opacity-50"
enterTo="opacity-100"
leave="transition ease-out duration-300"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<span className="ml-3 lg:text-base">{title}</span>
</Transition>
2022-07-14 16:36:31 -07:00
</div>
{isExternal ? <ExternalLinkIcon className="h-4 w-4" /> : null}
2022-07-14 16:36:31 -07:00
</div>
</a>
</Link>
2022-07-14 16:36:31 -07:00
)
}
const ExpandableMenuItem = ({
alignBottom,
children,
collapsed,
hideIconBg,
icon,
title,
}: {
alignBottom?: boolean
children: ReactNode
collapsed: boolean
hideIconBg?: boolean
icon: ReactNode
title: string | ReactNode
}) => {
const [showMenu, setShowMenu] = useState(false)
const onHoverMenu = (open: boolean, action: string) => {
if (
(!open && action === 'onMouseEnter') ||
(open && action === 'onMouseLeave')
) {
setShowMenu(!open)
}
}
useEffect(() => {
if (collapsed) {
setShowMenu(false)
}
}, [collapsed])
2022-07-14 16:36:31 -07:00
const toggleMenu = () => {
setShowMenu(!showMenu)
}
return collapsed ? (
<Popover>
<div
onMouseEnter={
!alignBottom ? () => onHoverMenu(showMenu, 'onMouseEnter') : undefined
}
onMouseLeave={
!alignBottom ? () => onHoverMenu(showMenu, 'onMouseLeave') : undefined
}
2022-07-17 17:56:05 -07:00
className={`relative z-30 ${alignBottom ? '' : 'px-4 py-2'}`}
2022-07-14 16:36:31 -07:00
role="button"
>
<Popover.Button
className="hover:text-th-primary"
onClick={() => toggleMenu()}
>
<div
className={` ${
hideIconBg
? ''
: 'flex h-10 w-10 items-center justify-center rounded-full bg-th-bkg-3'
2022-07-14 16:36:31 -07:00
} ${
alignBottom
2022-07-17 17:56:05 -07:00
? 'flex h-[72px] w-[72px] items-center justify-center hover:bg-th-bkg-2'
2022-07-14 16:36:31 -07:00
: ''
}`}
>
{icon}
</div>
</Popover.Button>
<Transition
show={showMenu}
as={Fragment}
enter="transition-all ease-in duration-300"
enterFrom="opacity-0 transform scale-90"
enterTo="opacity-100 transform scale-100"
leave="transition ease-out duration-300"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Popover.Panel
2022-07-17 17:56:05 -07:00
className={`absolute z-20 rounded-md rounded-l-none border border-th-bkg-3 bg-th-bkg-1 py-2 ${
2022-07-14 16:36:31 -07:00
alignBottom
2022-07-17 17:56:05 -07:00
? 'bottom-0 left-[71px] w-72 rounded-b-none p-0'
: 'top-1/2 left-[71px] w-56 -translate-y-1/2 transform'
2022-07-14 16:36:31 -07:00
}`}
>
{children}
</Popover.Panel>
</Transition>
</div>
</Popover>
) : (
<Disclosure>
<div
onClick={() => setShowMenu(!showMenu)}
role="button"
2022-07-17 17:56:05 -07:00
className={`w-full px-4 py-2 ${
alignBottom ? 'h-[72px] hover:bg-th-bkg-2' : ''
}`}
2022-07-14 16:36:31 -07:00
>
<Disclosure.Button
className={`flex h-full w-full items-center justify-between rounded-none hover:text-th-primary`}
>
<div className="flex items-center">
<div
className={
hideIconBg
? ''
: 'flex h-10 w-10 items-center justify-center rounded-full bg-th-bkg-3'
2022-07-14 16:36:31 -07:00
}
>
{icon}
</div>
<Transition
appear={true}
show={!collapsed}
as={Fragment}
enter="transition-all ease-in duration-300"
enterFrom="opacity-50"
enterTo="opacity-100"
leave="transition ease-out duration-300"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<span className="ml-3 lg:text-base">{title}</span>
2022-07-14 16:36:31 -07:00
</Transition>
</div>
<ChevronDownIcon
className={`${
showMenu ? 'rotate-180 transform' : 'rotate-360 transform'
2022-07-14 20:38:02 -07:00
} h-5 w-5 flex-shrink-0`}
2022-07-14 16:36:31 -07:00
/>
</Disclosure.Button>
</div>
<Transition
appear={true}
show={showMenu}
as={Fragment}
enter="transition-all ease-in duration-500"
enterFrom="opacity-100 max-h-0"
enterTo="opacity-100 max-h-80"
leave="transition-all ease-out duration-500"
leaveFrom="opacity-100 max-h-80"
leaveTo="opacity-0 max-h-0"
>
<Disclosure.Panel className="w-full overflow-hidden">
2022-07-17 17:56:05 -07:00
<div className={`${!alignBottom ? 'ml-2' : ''}`}>{children}</div>
2022-07-14 16:36:31 -07:00
</Disclosure.Panel>
</Transition>
</Disclosure>
)
}