mango-ui-v3/components/MarketMenuItem.tsx

79 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-08-25 15:22:40 -07:00
import { useState } from 'react'
import { useRouter } from 'next/router'
import { QuestionMarkCircleIcon } from '@heroicons/react/outline'
import Link from 'next/link'
import * as MonoIcons from './icons'
2022-02-01 16:58:47 -08:00
import { initialMarket } from './SettingsModal'
// const isMarketSelected = ()
export default function MarketMenuItem({ menuTitle = '', linksArray = [] }) {
const { asPath } = useRouter()
const [openState, setOpenState] = useState(false)
2022-02-01 16:58:47 -08:00
const iconName = `${menuTitle.slice(0, 1)}${menuTitle
.slice(1, 4)
2021-07-18 07:17:52 -07:00
.toLowerCase()}MonoIcon`
const SymbolIcon = MonoIcons[iconName] || QuestionMarkCircleIcon
2021-07-18 07:17:52 -07:00
const onHover = (open, action) => {
if (
2021-08-25 15:22:40 -07:00
(!open && action === 'onMouseEnter') ||
(open && action === 'onMouseLeave')
) {
2021-08-25 15:22:40 -07:00
setOpenState((openState) => !openState)
}
}
2022-02-01 16:58:47 -08:00
const isSelected =
asPath.includes(`=${menuTitle}`) ||
(asPath === '/' && initialMarket.name.includes(menuTitle))
return (
2021-08-25 15:22:40 -07:00
<div className="relative">
<div
onMouseEnter={() => onHover(openState, 'onMouseEnter')}
onMouseLeave={() => onHover(openState, 'onMouseLeave')}
className="flex h-10 cursor-pointer flex-col px-3"
2021-08-25 15:22:40 -07:00
>
2022-02-01 16:58:47 -08:00
<div
className={`flex h-10 items-center text-th-fgd-3 hover:text-th-primary focus:outline-none ${
2022-02-01 16:58:47 -08:00
isSelected ? 'text-th-primary' : ''
}`}
>
<SymbolIcon className={`mr-1.5 hidden h-3.5 w-auto lg:block `} />
<span className={`text-xs font-normal`}>{menuTitle}</span>
2021-08-25 15:22:40 -07:00
</div>
{openState ? (
<div className="absolute top-10 z-10">
<div className="relative divide-y divide-th-fgd-4 rounded rounded-t-none bg-th-bkg-3 px-3">
2021-08-25 15:22:40 -07:00
{linksArray.map((m) => (
<Link
href={{
2022-01-31 09:48:48 -08:00
pathname: '/',
query: { name: m.name },
}}
2021-08-25 15:22:40 -07:00
key={m.name}
2021-11-14 06:48:09 -08:00
shallow={true}
2021-08-25 15:22:40 -07:00
>
<a
className={`block whitespace-nowrap py-2 text-xs hover:text-th-primary ${
2022-02-01 16:58:47 -08:00
asPath.includes(m.name) ||
(asPath === '/' && initialMarket.name === m.name)
? 'text-th-primary'
: 'text-th-fgd-1'
2021-08-25 15:22:40 -07:00
}`}
>
2021-08-25 15:22:40 -07:00
{m.name}
</a>
</Link>
))}
</div>
</div>
2021-08-25 15:22:40 -07:00
) : null}
</div>
</div>
)
}