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

252 lines
7.7 KiB
TypeScript
Raw Normal View History

2022-11-18 11:11:06 -08:00
import { memo, useMemo, useEffect } from 'react'
2022-07-16 04:22:59 -07:00
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 { IconButton } from '../shared/Button'
2022-11-18 11:11:06 -08:00
import { 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 Decimal from 'decimal.js'
2022-09-14 17:02:16 -07:00
import { getTokenInMax } from './useTokenMax'
2022-11-18 09:09:39 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
2022-11-18 11:11:06 -08:00
import useJupiterMints from 'hooks/useJupiterMints'
2022-11-20 12:32:38 -08:00
import useMangoGroup from 'hooks/useMangoGroup'
2022-07-16 04:22:59 -07:00
2022-11-19 11:20:36 -08:00
// const generateSearchTerm = (item: Token, searchValue: string) => {
// const normalizedSearchValue = searchValue.toLowerCase()
// const values = `${item.symbol} ${item.name}`.toLowerCase()
2022-07-16 04:22:59 -07:00
2022-11-19 11:20:36 -08:00
// const isMatchingWithSymbol =
// item.symbol.toLowerCase().indexOf(normalizedSearchValue) >= 0
// const matchingSymbolPercent = isMatchingWithSymbol
// ? normalizedSearchValue.length / item.symbol.length
// : 0
2022-07-16 04:22:59 -07:00
2022-11-19 11:20:36 -08:00
// return {
// token: item,
// matchingIdx: values.indexOf(normalizedSearchValue),
// matchingSymbolPercent,
// }
// }
2022-07-16 04:22:59 -07:00
2022-11-19 11:20:36 -08:00
// 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)
// }
2022-07-16 04:22:59 -07:00
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
2022-07-16 04:22:59 -07:00
return (
<div>
<button
key={address}
2022-11-15 09:10:58 -08:00
className={`default-transition 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-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-11-20 12:32:38 -08: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>
)
}
2022-11-19 11:20:36 -08:00
// 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-11-18 11:11:06 -08:00
// const [search, setSearch] = useState('')
const { mangoTokens } = useJupiterMints()
const inputBank = mangoStore((s) => s.swap.inputBank)
const outputBank = mangoStore((s) => s.swap.outputBank)
2022-11-20 12:32:38 -08:00
const { group } = useMangoGroup()
2022-11-18 09:09:39 -08:00
const { mangoAccount } = useMangoAccount()
2022-07-16 04:22:59 -07:00
2022-09-29 21:21:23 -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 (
2022-11-18 11:11:06 -08:00
mangoTokens?.length &&
2022-09-14 03:37:45 -07:00
group &&
mangoAccount &&
outputBank &&
type === 'input'
) {
2022-11-18 11:11:06 -08:00
const filteredSortedTokens = mangoTokens
2022-09-14 03:37:45 -07:00
.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
2022-11-18 11:11:06 -08:00
} else if (mangoTokens?.length) {
const filteredTokens = mangoTokens
2022-09-14 03:37:45 -07:00
.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-11-18 11:11:06 -08:00
}, [mangoTokens, inputBank, outputBank, mangoAccount, group, useMargin, type])
2022-07-16 04:22:59 -07:00
2022-09-29 21:21:23 -07:00
// const handleUpdateSearch = (e: ChangeEvent<HTMLInputElement>) => {
// setSearch(e.target.value)
// }
2022-07-16 04:22:59 -07:00
2022-09-29 21:21:23 -07:00
// const sortedTokens = search ? startSearch(tokenInfos, search) : tokenInfos
const sortedTokens = tokenInfos
2022-07-16 04:22:59 -07:00
return (
<>
2022-09-16 04:37:24 -07:00
<p className="mb-3">
{type === 'input'
2022-11-22 02:36:45 -08:00
? t('swap:pay')
: type === 'output'
? t('swap:receive')
: ''}
2022-09-16 04:37:24 -07:00
</p>
2022-10-08 03:15:03 -07:00
<IconButton
className="absolute top-2 right-2 text-th-fgd-3 hover:text-th-fgd-2"
onClick={onClose}
hideBg
>
2022-09-29 21:21:23 -07:00
<XMarkIcon className="h-6 w-6" />
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
2022-11-18 11:11:06 -08:00
if (mangoTokens.length) {
logoURI = mangoTokens.find(
2022-08-15 19:41:06 -07:00
(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> */}
2022-10-08 03:15:03 -07:00
<div className="mb-2 flex justify-between rounded bg-th-bkg-2 p-2">
2022-09-14 03:37:45 -07:00
<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-11-27 15:40:40 -08:00
<div className="overflow-auto pb-2">
2022-07-16 04:22:59 -07:00
{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)