filter and sort token list

This commit is contained in:
saml33 2022-07-26 13:59:46 +10:00
parent 41accab8e7
commit ff155deb48
7 changed files with 72 additions and 25 deletions

View File

@ -1,33 +1,66 @@
import { Bank, MangoAccount } from '@blockworks-foundation/mango-v4' import { Bank, MangoAccount } from '@blockworks-foundation/mango-v4'
import { Transition } from '@headlessui/react' import { Transition } from '@headlessui/react'
import { ChevronDownIcon, DotsHorizontalIcon } from '@heroicons/react/solid' import { ChevronDownIcon, DotsHorizontalIcon } from '@heroicons/react/solid'
import { useWallet } from '@solana/wallet-adapter-react'
import { useTranslation } from 'next-i18next' import { useTranslation } from 'next-i18next'
import Image from 'next/image' import Image from 'next/image'
import { Fragment, useState } from 'react' import { Fragment, useEffect, useMemo, useState } from 'react'
import { useViewport } from '../hooks/useViewport' import { useViewport } from '../hooks/useViewport'
import mangoStore from '../store/state' import mangoStore from '../store/state'
import { formatDecimal, numberFormat } from '../utils/numbers' import { formatDecimal, numberFormat } from '../utils/numbers'
import { breakpoints } from '../utils/theme' import { breakpoints } from '../utils/theme'
import Switch from './forms/Switch'
import BorrowModal from './modals/BorrowModal' import BorrowModal from './modals/BorrowModal'
import DepositModal from './modals/DepositModal' import DepositModal from './modals/DepositModal'
import WithdrawModal from './modals/WithdrawModal' import WithdrawModal from './modals/WithdrawModal'
import Button, { IconButton, LinkButton } from './shared/Button' import { IconButton, LinkButton } from './shared/Button'
import ContentBox from './shared/ContentBox' import ContentBox from './shared/ContentBox'
import { UpTriangle } from './shared/DirectionTriangles' import { UpTriangle } from './shared/DirectionTriangles'
import IconDropMenu from './shared/IconDropMenu' import IconDropMenu from './shared/IconDropMenu'
const TokenList = () => { const TokenList = () => {
const { t } = useTranslation('common') const { t } = useTranslation('common')
const { connected } = useWallet()
const [showTokenDetails, setShowTokenDetails] = useState('') const [showTokenDetails, setShowTokenDetails] = useState('')
const [showZeroBalances, setShowZeroBalances] = useState(true)
const mangoAccount = mangoStore((s) => s.mangoAccount) const mangoAccount = mangoStore((s) => s.mangoAccount)
const group = mangoStore((s) => s.group) const group = mangoStore((s) => s.group)
const { width } = useViewport() const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false const showTableView = width ? width > breakpoints.md : false
const banks = group?.banksMap const banks = useMemo(() => {
? Array.from(group?.banksMap, ([key, value]) => ({ key, value })) if (group?.banksMap) {
: [] const rawBanks = Array.from(group?.banksMap, ([key, value]) => ({
key,
value,
}))
return mangoAccount
? showZeroBalances
? rawBanks.sort(
(a, b) =>
Math.abs(mangoAccount?.getUi(b.value) * Number(b.value.price)) -
Math.abs(mangoAccount?.getUi(a.value) * Number(a.value.price))
)
: rawBanks
.filter((b) => mangoAccount?.getUi(b.value) !== 0)
.sort(
(a, b) =>
Math.abs(
mangoAccount?.getUi(b.value) * Number(b.value.price)
) -
Math.abs(mangoAccount?.getUi(a.value) * Number(a.value.price))
)
: rawBanks
}
return []
}, [showZeroBalances, group, mangoAccount])
useEffect(() => {
if (!connected) {
setShowZeroBalances(true)
}
}, [connected])
const handleShowTokenDetails = (name: string) => { const handleShowTokenDetails = (name: string) => {
showTokenDetails ? setShowTokenDetails('') : setShowTokenDetails(name) showTokenDetails ? setShowTokenDetails('') : setShowTokenDetails(name)
@ -35,7 +68,17 @@ const TokenList = () => {
return ( return (
<ContentBox hideBorder hidePadding> <ContentBox hideBorder hidePadding>
<h2>{t('tokens')}</h2> <div className="flex items-center justify-between">
<h2>{t('tokens')}</h2>
<Switch
className="text-th-fgd-3"
checked={showZeroBalances}
disabled={!mangoAccount}
onChange={() => setShowZeroBalances(!showZeroBalances)}
>
{t('show-zero-balances')}
</Switch>
</div>
{showTableView ? ( {showTableView ? (
<table className="min-w-full"> <table className="min-w-full">
<thead> <thead>
@ -319,23 +362,20 @@ const ActionsMenu = ({
> >
{t('deposit')} {t('deposit')}
</LinkButton> </LinkButton>
{mangoAccount && mangoAccount.getUi(bank) > 0 ? ( <LinkButton
<LinkButton className="w-full text-left"
className="w-full text-left" disabled={!mangoAccount}
disabled={!mangoAccount} onClick={() => handleShowActionModals(bank.name, 'withdraw')}
onClick={() => handleShowActionModals(bank.name, 'withdraw')} >
> {t('withdraw')}
{t('withdraw')} </LinkButton>
</LinkButton> <LinkButton
) : ( className="w-full text-left"
<LinkButton disabled={!mangoAccount}
className="w-full text-left" onClick={() => handleShowActionModals(bank.name, 'borrow')}
disabled={!mangoAccount} >
onClick={() => handleShowActionModals(bank.name, 'borrow')} {t('borrow')}
> </LinkButton>
{t('borrow')}
</LinkButton>
)}
<LinkButton <LinkButton
className="w-full text-left" className="w-full text-left"
disabled={!mangoAccount} disabled={!mangoAccount}

View File

@ -9,7 +9,7 @@ const TradeSimplePage = () => {
const outputTokenInfo = mangoStore((s) => s.outputTokenInfo) const outputTokenInfo = mangoStore((s) => s.outputTokenInfo)
return ( return (
<div className="grid grid-cols-12 gap-6"> <div className="grid grid-cols-12 gap-x-6 gap-y-8">
<div className="col-span-12 space-y-12 md:col-span-6 lg:col-span-8"> <div className="col-span-12 space-y-12 md:col-span-6 lg:col-span-8">
<SwapTokenChart <SwapTokenChart
inputTokenId={inputTokenInfo?.extensions?.coingeckoId} inputTokenId={inputTokenInfo?.extensions?.coingeckoId}

View File

@ -5,6 +5,7 @@ interface SwitchProps {
className?: string className?: string
onChange: (x: boolean) => void onChange: (x: boolean) => void
children: ReactNode children: ReactNode
disabled?: boolean
} }
const Switch: FunctionComponent<SwitchProps> = ({ const Switch: FunctionComponent<SwitchProps> = ({
@ -12,6 +13,7 @@ const Switch: FunctionComponent<SwitchProps> = ({
className = '', className = '',
children, children,
onChange, onChange,
disabled,
}) => { }) => {
const handleClick = () => { const handleClick = () => {
onChange(!checked) onChange(!checked)
@ -26,10 +28,11 @@ const Switch: FunctionComponent<SwitchProps> = ({
checked ? 'bg-th-primary' : 'bg-th-bkg-button' checked ? 'bg-th-primary' : 'bg-th-bkg-button'
} relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full } relative inline-flex h-6 w-11 flex-shrink-0 cursor-pointer rounded-full
border-2 border-transparent transition-colors duration-200 ease-in-out border-2 border-transparent transition-colors duration-200 ease-in-out
focus:outline-none`} focus:outline-none ${disabled ? 'opacity-60' : ''}`}
role="switch" role="switch"
aria-checked={checked} aria-checked={checked}
onClick={handleClick} onClick={handleClick}
disabled={disabled}
> >
<span className="sr-only">{children}</span> <span className="sr-only">{children}</span>
<span <span

View File

@ -36,6 +36,7 @@
"select-token": "Select Token", "select-token": "Select Token",
"sell": "Sell", "sell": "Sell",
"settings": "Settings", "settings": "Settings",
"show-zero-balances": "Show Zero Balances",
"spanish": "Español", "spanish": "Español",
"stats": "Stats", "stats": "Stats",
"theme": "Theme", "theme": "Theme",

View File

@ -36,6 +36,7 @@
"select-token": "Select Token", "select-token": "Select Token",
"sell": "Sell", "sell": "Sell",
"settings": "Settings", "settings": "Settings",
"show-zero-balances": "Show Zero Balances",
"spanish": "Español", "spanish": "Español",
"stats": "Stats", "stats": "Stats",
"theme": "Theme", "theme": "Theme",

View File

@ -36,6 +36,7 @@
"select-token": "Select Token", "select-token": "Select Token",
"sell": "Sell", "sell": "Sell",
"settings": "Settings", "settings": "Settings",
"show-zero-balances": "Show Zero Balances",
"spanish": "Español", "spanish": "Español",
"stats": "Stats", "stats": "Stats",
"theme": "Theme", "theme": "Theme",

View File

@ -36,6 +36,7 @@
"select-token": "Select Token", "select-token": "Select Token",
"sell": "Sell", "sell": "Sell",
"settings": "Settings", "settings": "Settings",
"show-zero-balances": "Show Zero Balances",
"spanish": "Español", "spanish": "Español",
"stats": "Stats", "stats": "Stats",
"theme": "Theme", "theme": "Theme",