2022-09-29 19:21:31 +00:00
|
|
|
import React, { useMemo } from 'react'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { Popover } from '@headlessui/react'
|
|
|
|
import { ChevronDownIcon } from '@heroicons/react/24/solid'
|
2022-09-02 11:51:00 +00:00
|
|
|
|
2022-09-29 19:21:31 +00:00
|
|
|
import SearchInput from 'components/SearchInput'
|
|
|
|
import ProgressBar from 'components/ProgressBar'
|
|
|
|
import Spinner from 'components/Spinner'
|
|
|
|
import Wallet from 'components/Wallet'
|
|
|
|
import { formatCurrency } from 'utils/formatters'
|
|
|
|
import useCreditAccounts from 'hooks/useCreditAccounts'
|
2022-10-28 11:14:14 +00:00
|
|
|
import useCreateCreditAccount from 'hooks/mutations/useCreateCreditAccount'
|
|
|
|
import useDeleteCreditAccount from 'hooks/mutations/useDeleteCreditAccount'
|
2022-09-29 19:21:31 +00:00
|
|
|
import useCreditManagerStore from 'stores/useCreditManagerStore'
|
2022-10-12 15:41:03 +00:00
|
|
|
import useAccountStats from 'hooks/useAccountStats'
|
|
|
|
import SemiCircleProgress from './SemiCircleProgress'
|
|
|
|
import useWalletStore from 'stores/useWalletStore'
|
|
|
|
import ArrowRightLine from 'components/Icons/arrow-right-line.svg'
|
2022-09-07 19:12:19 +00:00
|
|
|
|
2022-09-29 19:21:31 +00:00
|
|
|
// TODO: will require some tweaks depending on how lower viewport mocks pans out
|
|
|
|
const MAX_VISIBLE_CREDIT_ACCOUNTS = 5
|
|
|
|
|
|
|
|
const navItems = [
|
|
|
|
{ href: '/trade', label: 'Trade' },
|
2022-10-12 15:41:03 +00:00
|
|
|
{ href: '/earn', label: 'Earn' },
|
2022-09-29 19:21:31 +00:00
|
|
|
{ href: '/borrow', label: 'Borrow' },
|
|
|
|
{ href: '/portfolio', label: 'Portfolio' },
|
|
|
|
{ href: '/council', label: 'Council' },
|
|
|
|
]
|
2022-09-06 15:16:58 +00:00
|
|
|
|
2022-09-06 17:56:12 +00:00
|
|
|
const NavLink = ({ href, children }: { href: string; children: string }) => {
|
2022-09-29 19:21:31 +00:00
|
|
|
const router = useRouter()
|
2022-09-06 17:56:12 +00:00
|
|
|
|
2022-09-02 11:51:00 +00:00
|
|
|
return (
|
2022-09-06 17:56:12 +00:00
|
|
|
<Link href={href} passHref>
|
2022-09-29 19:21:31 +00:00
|
|
|
<a className={`${router.pathname === href ? 'text-white' : ''} hover:text-white`}>
|
2022-09-06 17:56:12 +00:00
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
</Link>
|
2022-09-29 19:21:31 +00:00
|
|
|
)
|
|
|
|
}
|
2022-09-06 17:56:12 +00:00
|
|
|
|
2022-09-08 06:23:02 +00:00
|
|
|
const Navigation = () => {
|
2022-10-12 15:41:03 +00:00
|
|
|
const address = useWalletStore((s) => s.address)
|
2022-09-29 19:21:31 +00:00
|
|
|
const selectedAccount = useCreditManagerStore((s) => s.selectedAccount)
|
|
|
|
const setSelectedAccount = useCreditManagerStore((s) => s.actions.setSelectedAccount)
|
|
|
|
const toggleCreditManager = useCreditManagerStore((s) => s.actions.toggleCreditManager)
|
|
|
|
|
|
|
|
const { data: creditAccountsList } = useCreditAccounts()
|
|
|
|
const { mutate: createCreditAccount, isLoading: isLoadingCreate } = useCreateCreditAccount()
|
|
|
|
const { mutate: deleteCreditAccount, isLoading: isLoadingDelete } = useDeleteCreditAccount(
|
|
|
|
selectedAccount || ''
|
|
|
|
)
|
|
|
|
|
2022-10-12 15:41:03 +00:00
|
|
|
const accountStats = useAccountStats()
|
|
|
|
|
2022-09-29 19:21:31 +00:00
|
|
|
const { firstCreditAccounts, restCreditAccounts } = useMemo(() => {
|
|
|
|
return {
|
|
|
|
firstCreditAccounts: creditAccountsList?.slice(0, MAX_VISIBLE_CREDIT_ACCOUNTS) ?? [],
|
|
|
|
restCreditAccounts: creditAccountsList?.slice(MAX_VISIBLE_CREDIT_ACCOUNTS) ?? [],
|
|
|
|
}
|
|
|
|
}, [creditAccountsList])
|
|
|
|
|
2022-09-08 06:23:02 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
{/* Main navigation bar */}
|
2022-09-30 12:50:16 +00:00
|
|
|
<div className="flex items-center justify-between border-b border-white/20 px-6 py-3">
|
2022-09-08 06:23:02 +00:00
|
|
|
<Link href="/" passHref>
|
|
|
|
<a>
|
|
|
|
<img src="/logo.svg" alt="mars" />
|
|
|
|
</a>
|
|
|
|
</Link>
|
2022-09-30 12:50:16 +00:00
|
|
|
<div className="flex gap-5 px-12 text-white/40">
|
2022-09-29 19:21:31 +00:00
|
|
|
{navItems.map((item, index) => (
|
|
|
|
<NavLink key={index} href={item.href}>
|
|
|
|
{item.label}
|
|
|
|
</NavLink>
|
|
|
|
))}
|
2022-09-08 06:23:02 +00:00
|
|
|
</div>
|
2022-09-14 11:28:18 +00:00
|
|
|
<Wallet />
|
2022-09-06 17:56:12 +00:00
|
|
|
</div>
|
2022-09-08 06:23:02 +00:00
|
|
|
{/* Sub navigation bar */}
|
2022-10-12 15:41:03 +00:00
|
|
|
{address && (
|
|
|
|
<div className="flex justify-between border-b border-white/20 px-6 py-3 text-sm text-white/40">
|
|
|
|
<div className="flex items-center">
|
|
|
|
<SearchInput />
|
|
|
|
{firstCreditAccounts.map((account) => (
|
|
|
|
<div
|
|
|
|
key={account}
|
|
|
|
className={`cursor-pointer px-4 hover:text-white ${
|
|
|
|
selectedAccount === account ? 'text-white' : ''
|
|
|
|
}`}
|
|
|
|
onClick={() => setSelectedAccount(account)}
|
|
|
|
>
|
|
|
|
Account {account}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
{restCreditAccounts.length > 0 && (
|
|
|
|
<Popover className="relative">
|
|
|
|
<Popover.Button>
|
|
|
|
<div className="flex cursor-pointer items-center px-3 hover:text-white">
|
|
|
|
More
|
|
|
|
<ChevronDownIcon className="ml-1 h-4 w-4" />
|
|
|
|
</div>
|
|
|
|
</Popover.Button>
|
|
|
|
<Popover.Panel className="absolute z-10 w-[200px] pt-2">
|
|
|
|
<div className="rounded-2xl bg-white p-4 text-gray-900">
|
|
|
|
{restCreditAccounts.map((account) => (
|
|
|
|
<div
|
|
|
|
key={account}
|
|
|
|
className={`cursor-pointer hover:text-orange-500 ${
|
|
|
|
selectedAccount === account ? 'text-orange-500' : ''
|
|
|
|
}`}
|
|
|
|
onClick={() => setSelectedAccount(account)}
|
|
|
|
>
|
|
|
|
Account {account}
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
</Popover.Panel>
|
|
|
|
</Popover>
|
|
|
|
)}
|
2022-09-29 19:21:31 +00:00
|
|
|
<Popover className="relative">
|
|
|
|
<Popover.Button>
|
2022-09-30 12:50:16 +00:00
|
|
|
<div className="flex cursor-pointer items-center px-3 hover:text-white">
|
2022-10-12 15:41:03 +00:00
|
|
|
Manage
|
2022-09-29 19:21:31 +00:00
|
|
|
<ChevronDownIcon className="ml-1 h-4 w-4" />
|
|
|
|
</div>
|
|
|
|
</Popover.Button>
|
2022-09-30 12:50:16 +00:00
|
|
|
<Popover.Panel className="absolute z-10 w-[200px] pt-2">
|
2022-10-12 15:41:03 +00:00
|
|
|
{({ close }) => (
|
|
|
|
<div className="rounded-2xl bg-white p-4 text-gray-900">
|
2022-09-29 19:21:31 +00:00
|
|
|
<div
|
2022-10-12 15:41:03 +00:00
|
|
|
className="mb-2 cursor-pointer hover:text-orange-500"
|
|
|
|
onClick={() => {
|
|
|
|
close()
|
|
|
|
createCreditAccount()
|
|
|
|
}}
|
2022-09-29 19:21:31 +00:00
|
|
|
>
|
2022-10-12 15:41:03 +00:00
|
|
|
Create Account
|
2022-09-29 19:21:31 +00:00
|
|
|
</div>
|
2022-10-12 15:41:03 +00:00
|
|
|
<div
|
|
|
|
className="mb-2 cursor-pointer hover:text-orange-500"
|
|
|
|
onClick={() => {
|
|
|
|
close()
|
|
|
|
deleteCreditAccount()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Close Account
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="mb-2 cursor-pointer hover:text-orange-500"
|
|
|
|
onClick={() => alert('TODO')}
|
|
|
|
>
|
|
|
|
Transfer Balance
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
className="cursor-pointer hover:text-orange-500"
|
|
|
|
onClick={() => alert('TODO')}
|
|
|
|
>
|
|
|
|
Rearrange
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-09-29 19:21:31 +00:00
|
|
|
</Popover.Panel>
|
|
|
|
</Popover>
|
2022-10-12 15:41:03 +00:00
|
|
|
</div>
|
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
{accountStats && (
|
|
|
|
<>
|
|
|
|
<p>{formatCurrency(accountStats.netWorth)}</p>
|
|
|
|
{/* TOOLTIP */}
|
|
|
|
<div title={`${String(accountStats.currentLeverage.toFixed(1))}x`}>
|
|
|
|
<SemiCircleProgress
|
|
|
|
value={accountStats.currentLeverage / accountStats.maxLeverage}
|
|
|
|
label="Lvg"
|
|
|
|
/>
|
2022-09-14 13:35:17 +00:00
|
|
|
</div>
|
2022-10-12 15:41:03 +00:00
|
|
|
<SemiCircleProgress value={accountStats.risk} label="Risk" />
|
|
|
|
<ProgressBar value={accountStats.health} />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
<div
|
|
|
|
className="flex w-16 cursor-pointer justify-center hover:text-white"
|
|
|
|
onClick={toggleCreditManager}
|
|
|
|
>
|
|
|
|
<ArrowRightLine />
|
|
|
|
</div>
|
2022-09-14 16:47:52 +00:00
|
|
|
</div>
|
2022-09-08 06:23:02 +00:00
|
|
|
</div>
|
2022-10-12 15:41:03 +00:00
|
|
|
)}
|
2022-09-29 19:21:31 +00:00
|
|
|
{(isLoadingCreate || isLoadingDelete) && (
|
|
|
|
<div className="fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-09-02 11:51:00 +00:00
|
|
|
</div>
|
2022-09-29 19:21:31 +00:00
|
|
|
)
|
|
|
|
}
|
2022-09-02 11:51:00 +00:00
|
|
|
|
2022-09-29 19:21:31 +00:00
|
|
|
export default Navigation
|