mango-v4-ui/components/swap/SwapFormTokenList.tsx

256 lines
7.8 KiB
TypeScript
Raw Normal View History

2022-07-16 04:22:59 -07:00
import { memo, useMemo, useState, useEffect, ChangeEvent } from 'react'
import Image from 'next/image'
import { Token } from '../../types/jupiter'
2022-09-12 08:53:57 -07:00
import mangoStore from '@store/mangoStore'
2022-07-16 04:22:59 -07:00
import Input from '../forms/Input'
import { IconButton } from '../shared/Button'
2022-09-06 21:36:35 -07:00
import {
QuestionMarkCircleIcon,
MagnifyingGlassIcon,
XMarkIcon,
} from '@heroicons/react/20/solid'
2022-07-17 04:51:51 -07:00
import { useTranslation } from 'next-i18next'
2022-09-14 03:37:45 -07:00
import { floorToDecimal } from '../../utils/numbers'
import Decimal from 'decimal.js'
2022-09-14 17:02:16 -07:00
import { getTokenInMax } from './useTokenMax'
2022-07-16 04:22:59 -07:00
const generateSearchTerm = (item: Token, searchValue: string) => {
const normalizedSearchValue = searchValue.toLowerCase()
const values = `${item.symbol} ${item.name}`.toLowerCase()
const isMatchingWithSymbol =
item.symbol.toLowerCase().indexOf(normalizedSearchValue) >= 0
const matchingSymbolPercent = isMatchingWithSymbol
? normalizedSearchValue.length / item.symbol.length
: 0
return {
token: item,
matchingIdx: values.indexOf(normalizedSearchValue),
matchingSymbolPercent,
}
}
const startSearch = (items: Token[], searchValue: string) => {
return items
.map((item) => generateSearchTerm(item, searchValue))
.filter((item) => item.matchingIdx >= 0)
.sort((i1, i2) => i1.matchingIdx - i2.matchingIdx)
.sort((i1, i2) => i2.matchingSymbolPercent - i1.matchingSymbolPercent)
.map((item) => item.token)
}
const TokenItem = ({
token,
onSubmit,
2022-09-14 03:37:45 -07:00
useMargin,
type,
2022-07-16 04:22:59 -07:00
}: {
token: Token
onSubmit: (x: string) => void
2022-09-14 03:37:45 -07:00
useMargin: boolean
type: string
2022-07-16 04:22:59 -07:00
}) => {
const { address, symbol, logoURI, name } = token
2022-09-14 03:37:45 -07:00
const isDisabled =
2022-09-14 17:02:16 -07:00
(type === 'input' && useMargin && token.amountWithBorrow!.eq(0)) ||
(type === 'input' && !useMargin && token.amount!.eq(0))
2022-09-14 03:37:45 -07:00
2022-07-16 04:22:59 -07:00
return (
<div>
<button
key={address}
2022-09-16 04:37:24 -07:00
className="flex w-full cursor-pointer items-center justify-between rounded-md p-2 font-normal focus:bg-th-bkg-3 focus:outline-none md:hover:bg-th-bkg-2"
2022-08-10 13:23:19 -07:00
onClick={() => onSubmit(address)}
2022-09-14 03:37:45 -07:00
disabled={isDisabled}
2022-07-16 04:22:59 -07:00
>
<div className="flex items-center">
<picture>
<source srcSet={logoURI} type="image/webp" />
<img src={logoURI} width="24" height="24" alt={symbol} />
</picture>
<div className="ml-2.5">
<div className="text-left text-th-fgd-2">{symbol || 'unknown'}</div>
2022-09-14 03:37:45 -07:00
<div className="text-left text-xs text-th-fgd-4">
{name || 'unknown'}
</div>
2022-07-16 04:22:59 -07:00
</div>
</div>
2022-09-14 03:37:45 -07:00
{type === 'input' ? (
<p className="text-sm text-th-fgd-2">
{useMargin
2022-09-14 17:02:16 -07:00
? token.amountWithBorrow!.toString()
: token.amount!.toString()}
2022-09-14 03:37:45 -07:00
</p>
) : null}
2022-07-16 04:22:59 -07:00
</button>
</div>
)
}
const popularTokenSymbols = ['USDC', 'SOL', 'USDT', 'MNGO', 'BTC']
2022-07-16 04:22:59 -07:00
const SwapFormTokenList = ({
2022-07-16 04:22:59 -07:00
onClose,
onTokenSelect,
type,
2022-09-14 03:37:45 -07:00
useMargin,
2022-07-16 04:22:59 -07:00
}: {
onClose: () => void
onTokenSelect: (x: string) => void
type: string
2022-09-14 03:37:45 -07:00
useMargin: boolean
2022-07-16 04:22:59 -07:00
}) => {
2022-09-16 04:37:24 -07:00
const { t } = useTranslation(['common', 'swap'])
2022-07-16 04:22:59 -07:00
const [search, setSearch] = useState('')
const tokens = mangoStore.getState().jupiterTokens
const walletTokens = mangoStore((s) => s.wallet.tokens)
2022-08-15 19:41:06 -07:00
const jupiterTokens = mangoStore((s) => s.jupiterTokens)
const inputBank = mangoStore((s) => s.swap.inputBank)
const outputBank = mangoStore((s) => s.swap.outputBank)
2022-09-14 03:37:45 -07:00
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
const group = mangoStore((s) => s.group)
2022-07-16 04:22:59 -07:00
const popularTokens = useMemo(() => {
return tokens.filter((token) => {
return !token?.name || !token?.symbol
? false
: popularTokenSymbols.includes(token.symbol)
})
}, [tokens])
2022-07-16 04:22:59 -07:00
useEffect(() => {
function onEscape(e: any) {
if (e.keyCode === 27) {
2022-08-02 11:04:00 -07:00
onClose()
2022-07-16 04:22:59 -07:00
}
}
window.addEventListener('keydown', onEscape)
return () => window.removeEventListener('keydown', onEscape)
}, [onClose])
const tokenInfos = useMemo(() => {
2022-09-14 03:37:45 -07:00
if (
tokens?.length &&
group &&
mangoAccount &&
outputBank &&
type === 'input'
) {
const filteredSortedTokens = tokens
.map((token) => {
const max = getTokenInMax(
mangoAccount,
token.address,
group,
useMargin
)
2022-09-14 17:02:16 -07:00
return { ...token, ...max }
2022-09-14 03:37:45 -07:00
})
.filter((token) => (token.symbol === outputBank?.name ? false : true))
.sort((a, b) =>
useMargin
2022-09-14 17:02:16 -07:00
? Number(b.amountWithBorrow) - Number(a.amountWithBorrow)
: Number(b.amount) - Number(a.amount)
2022-07-16 04:22:59 -07:00
)
2022-09-14 03:37:45 -07:00
return filteredSortedTokens
} else if (tokens?.length) {
const filteredTokens = tokens
.map((token) => ({
...token,
2022-09-14 17:02:16 -07:00
amount: new Decimal(0),
amountWithBorrow: new Decimal(0),
2022-09-14 03:37:45 -07:00
}))
.filter((token) => (token.symbol === inputBank?.name ? false : true))
return filteredTokens
2022-07-16 04:22:59 -07:00
} else {
return []
}
2022-09-14 03:37:45 -07:00
}, [tokens, walletTokens, inputBank, outputBank, mangoAccount, group])
2022-07-16 04:22:59 -07:00
const handleUpdateSearch = (e: ChangeEvent<HTMLInputElement>) => {
setSearch(e.target.value)
}
const sortedTokens = search ? startSearch(tokenInfos, search) : tokenInfos
return (
<>
2022-09-16 04:37:24 -07:00
<p className="mb-3">
{type === 'input'
? `${t('swap')} ${t('swap:from')}`
: `${t('swap')} ${t('swap:to')}`}
</p>
2022-08-02 11:04:00 -07:00
<IconButton className="absolute top-2 right-2" onClick={onClose} hideBg>
2022-09-06 21:36:35 -07:00
<XMarkIcon className="h-5 w-5" />
2022-07-16 04:22:59 -07:00
</IconButton>
2022-09-14 03:37:45 -07:00
{/* No need for search/popular tokens until we have more tokens */}
{/* <div className="flex items-center text-th-fgd-4">
2022-07-16 04:22:59 -07:00
<Input
type="text"
placeholder="Search by token or paste address"
2022-09-06 21:36:35 -07:00
prefix={<MagnifyingGlassIcon className="h-5 w-5" />}
2022-07-16 04:22:59 -07:00
autoFocus
value={search}
onChange={handleUpdateSearch}
/>
</div>
2022-08-02 11:04:00 -07:00
{popularTokens.length ? (
2022-07-16 04:22:59 -07:00
<div className="mt-4 flex flex-wrap">
2022-08-15 19:41:06 -07:00
{popularTokens.map((token) => {
let logoURI
if (jupiterTokens.length) {
logoURI = jupiterTokens.find(
(t) => t.address === token.address
)!.logoURI
}
const disabled =
(type === 'input' && token.symbol === outputBank?.name) ||
(type === 'output' && token.symbol === inputBank?.name)
2022-08-15 19:41:06 -07:00
return (
<button
className={`${
disabled ? 'opacity-20' : 'hover:border-th-fgd-3'
} mx-1 mb-2 flex items-center rounded-md border border-th-bkg-4 py-1 px-3 focus:border-th-fgd-2`}
2022-08-15 19:41:06 -07:00
onClick={() => onTokenSelect(token.address)}
disabled={disabled}
2022-08-15 19:41:06 -07:00
key={token.address}
>
{logoURI ? (
<Image alt="" width="16" height="16" src={logoURI} />
) : (
<QuestionMarkCircleIcon className="h-5 w-5 text-th-fgd-3" />
)}
<span className="ml-1.5 text-th-fgd-1">{token.symbol}</span>
</button>
)
})}
2022-07-16 04:22:59 -07:00
</div>
2022-09-14 03:37:45 -07:00
) : null} */}
{/* <div className="my-2 border-t border-th-bkg-4"></div> */}
<div className="mb-2 flex justify-between rounded bg-th-bkg-3 p-2">
<p className="text-xs text-th-fgd-4">{t('token')}</p>
{type === 'input' ? (
<p className="text-xs text-th-fgd-4">{t('max')}</p>
) : null}
</div>
2022-07-16 04:22:59 -07:00
<div className="overflow-auto">
{sortedTokens.map((token) => (
2022-07-22 08:40:53 -07:00
<TokenItem
key={token.address}
token={token}
onSubmit={onTokenSelect}
2022-09-14 03:37:45 -07:00
useMargin={useMargin}
type={type}
2022-07-22 08:40:53 -07:00
/>
2022-07-16 04:22:59 -07:00
))}
</div>
</>
)
}
export default memo(SwapFormTokenList)