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

425 lines
17 KiB
TypeScript
Raw Normal View History

2022-08-10 04:17:10 -07:00
import { Fragment, useState } from 'react'
import {
ArrowRightIcon,
ChevronDownIcon,
ClockIcon,
2022-09-06 21:36:35 -07:00
ArrowTopRightOnSquareIcon,
2022-08-10 04:17:10 -07:00
LinkIcon,
2022-09-06 21:36:35 -07:00
} from '@heroicons/react/20/solid'
2022-08-10 04:17:10 -07:00
import dayjs from 'dayjs'
import { useTranslation } from 'next-i18next'
import Image from 'next/image'
2022-09-19 03:43:38 -07:00
import { breakpoints } from '../../utils/theme'
import { useViewport } from '../../hooks/useViewport'
import { IconButton } from '../shared/Button'
2022-08-10 04:17:10 -07:00
import { Transition } from '@headlessui/react'
2022-09-19 03:43:38 -07:00
import SheenLoader from '../shared/SheenLoader'
2022-08-10 04:17:10 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
2022-10-05 18:16:29 -07:00
import mangoStore, { TradeHistoryItem } from '@store/mangoStore'
2022-08-24 17:14:27 -07:00
import {
countLeadingZeros,
formatFixedDecimals,
trimDecimals,
2022-09-19 03:43:38 -07:00
} from '../../utils/numbers'
2022-09-27 20:07:26 -07:00
import useLocalStorageState from 'hooks/useLocalStorageState'
import { PREFERRED_EXPLORER_KEY } from 'utils/constants'
import { EXPLORERS } from 'pages/settings'
import Tooltip from '@components/shared/Tooltip'
2022-08-10 04:17:10 -07:00
const SwapHistoryTable = ({
2022-08-10 04:17:10 -07:00
tradeHistory,
loading,
}: {
tradeHistory: Array<TradeHistoryItem>
loading: boolean
}) => {
2022-09-27 20:07:26 -07:00
const { t } = useTranslation(['common', 'settings'])
2022-10-05 18:16:29 -07:00
const jupiterTokens = mangoStore((s) => s.jupiterTokens)
2022-08-10 04:17:10 -07:00
const { connected } = useWallet()
const [showSwapDetails, setSwapDetails] = useState('')
2022-08-10 04:17:10 -07:00
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
2022-09-27 20:07:26 -07:00
const [preferredExplorer] = useLocalStorageState(
PREFERRED_EXPLORER_KEY,
EXPLORERS[0]
)
2022-08-10 04:17:10 -07:00
const handleShowSwapDetails = (signature: string) => {
showSwapDetails ? setSwapDetails('') : setSwapDetails(signature)
2022-08-10 04:17:10 -07:00
}
return connected ? (
!loading ? (
tradeHistory.length ? (
showTableView ? (
2022-09-19 03:43:38 -07:00
<table className="min-w-full">
2022-08-10 04:17:10 -07:00
<thead>
<tr>
<th className="text-left">{t('date')}</th>
<th className="w-1/3 text-left">{t('swap')}</th>
2022-08-10 04:17:10 -07:00
<th className="text-right">{t('borrow')}</th>
<th className="text-right">{t('borrow-fee')}</th>
<th />
</tr>
</thead>
<tbody>
{tradeHistory.map((h, index) => {
const {
block_datetime,
signature,
swap_in_amount,
swap_in_loan_origination_fee,
swap_in_price_usd,
swap_in_symbol,
swap_out_amount,
2022-08-10 14:52:28 -07:00
loan,
loan_origination_fee,
2022-08-10 04:17:10 -07:00
swap_out_price_usd,
swap_out_symbol,
} = h
const borrowAmount =
loan > 0
2022-09-14 23:06:23 -07:00
? `${trimDecimals(loan, countLeadingZeros(loan) + 2)}`
2022-08-10 04:17:10 -07:00
: 0
const borrowFee =
swap_in_loan_origination_fee > 0
2022-08-24 17:14:27 -07:00
? swap_in_loan_origination_fee.toFixed(4)
2022-08-10 14:52:28 -07:00
: loan_origination_fee > 0
2022-08-24 17:14:27 -07:00
? loan_origination_fee.toFixed(4)
2022-08-10 04:17:10 -07:00
: 0
2022-08-24 17:14:27 -07:00
2022-10-05 18:16:29 -07:00
let baseLogoURI
let quoteLogoURI
if (jupiterTokens.length) {
baseLogoURI = jupiterTokens.find(
(t) => t.symbol === swap_in_symbol
)?.logoURI
quoteLogoURI = jupiterTokens.find(
(t) => t.symbol === swap_out_symbol
)?.logoURI
}
2022-08-24 17:14:27 -07:00
const inDecimals = countLeadingZeros(swap_in_amount) + 2
const outDecimals = countLeadingZeros(swap_out_amount) + 2
2022-08-10 04:17:10 -07:00
return (
2022-08-16 15:29:24 -07:00
<tr key={signature}>
2022-08-10 04:17:10 -07:00
<td>
2022-09-14 23:06:23 -07:00
<p className="font-body tracking-wide">
{dayjs(block_datetime).format('ddd D MMM')}
</p>
2022-10-05 18:16:29 -07:00
<p className="font-body text-xs tracking-wide text-th-fgd-3">
2022-08-10 04:17:10 -07:00
{dayjs(block_datetime).format('h:mma')}
</p>
</td>
<td className="w-1/3">
<div className="flex items-center space-x-4">
<div className="flex w-1/2 items-center">
<div className="mr-2 flex flex-shrink-0 items-center">
<Image
alt=""
width="24"
height="24"
2022-10-05 18:16:29 -07:00
src={baseLogoURI || ''}
2022-08-10 04:17:10 -07:00
/>
</div>
<div>
2022-09-14 23:06:23 -07:00
<p className="mb-1.5 whitespace-nowrap leading-none">
{`${trimDecimals(swap_in_amount, inDecimals)}`}
2022-10-05 18:16:29 -07:00
<span className="ml-1 font-body tracking-wide text-th-fgd-4">
2022-09-14 23:06:23 -07:00
{swap_in_symbol}
</span>
</p>
2022-10-05 18:16:29 -07:00
<p className="text-xs leading-none text-th-fgd-4">
2022-08-24 17:14:27 -07:00
{formatFixedDecimals(swap_in_price_usd, true)}
<span className="mx-1 text-th-fgd-4">|</span>
{formatFixedDecimals(
swap_in_amount * swap_in_price_usd,
true
)}
2022-08-10 04:17:10 -07:00
</p>
</div>
</div>
<div className="">
<ArrowRightIcon className="h-4 w-4 flex-shrink-0 text-th-fgd-4" />
</div>
<div className="flex w-1/2 items-center">
<div className="mr-2 flex flex-shrink-0 items-center">
<Image
alt=""
width="24"
height="24"
2022-10-05 18:16:29 -07:00
src={quoteLogoURI || ''}
2022-08-10 04:17:10 -07:00
/>
</div>
<div>
2022-09-14 23:06:23 -07:00
<p className="mb-1.5 whitespace-nowrap leading-none">
{`${trimDecimals(swap_out_amount, outDecimals)}`}
2022-10-05 18:16:29 -07:00
<span className="ml-1 font-body tracking-wide text-th-fgd-4">
2022-09-14 23:06:23 -07:00
{swap_out_symbol}
</span>
</p>
2022-10-05 18:16:29 -07:00
<p className="text-xs leading-none text-th-fgd-4">
2022-08-24 17:14:27 -07:00
{formatFixedDecimals(swap_out_price_usd, true)}
<span className="mx-1 text-th-fgd-4">|</span>
{formatFixedDecimals(
swap_out_amount * swap_out_price_usd,
true
2022-08-10 04:17:10 -07:00
)}
</p>
</div>
</div>
</div>
</td>
<td>
<div className="flex flex-col text-right">
2022-09-14 23:06:23 -07:00
<p>
{borrowAmount}
2022-10-05 18:16:29 -07:00
<span className="ml-1 font-body tracking-wide text-th-fgd-4">
2022-09-14 23:06:23 -07:00
{swap_in_symbol}
</span>
</p>
2022-08-10 04:17:10 -07:00
</div>
</td>
<td>
<div className="flex flex-col text-right">
<p>${borrowFee}</p>
</div>
</td>
<td>
2022-09-27 20:13:49 -07:00
<div className="flex items-center justify-end">
2022-09-27 20:07:26 -07:00
<Tooltip
content={`View on ${t(
`settings:${preferredExplorer.name}`
)}`}
placement="top-end"
2022-08-10 04:17:10 -07:00
>
2022-09-27 20:07:26 -07:00
<a
href={`${preferredExplorer.url}${signature}`}
target="_blank"
rel="noopener noreferrer"
>
2022-09-27 20:13:49 -07:00
<div className="h-6 w-6">
<Image
alt=""
width="24"
height="24"
src={`/explorer-logos/${preferredExplorer.name}.png`}
/>
</div>
2022-09-27 20:07:26 -07:00
</a>
</Tooltip>
2022-08-10 04:17:10 -07:00
</div>
</td>
2022-08-16 15:29:24 -07:00
</tr>
2022-08-10 04:17:10 -07:00
)
})}
</tbody>
</table>
) : (
2022-09-19 18:09:34 -07:00
<div>
2022-08-10 04:17:10 -07:00
{tradeHistory.map((h) => {
const {
block_datetime,
signature,
swap_in_amount,
swap_in_loan,
swap_in_loan_origination_fee,
swap_in_price_usd,
swap_in_symbol,
swap_out_amount,
2022-08-10 14:52:28 -07:00
loan,
loan_origination_fee,
2022-08-10 04:17:10 -07:00
swap_out_price_usd,
swap_out_symbol,
} = h
2022-08-24 17:14:27 -07:00
2022-08-10 04:17:10 -07:00
const borrowAmount =
2022-09-19 18:09:34 -07:00
loan > 0
? `${trimDecimals(loan, countLeadingZeros(loan) + 2)}`
2022-08-10 04:17:10 -07:00
: 0
const borrowFee =
swap_in_loan_origination_fee > 0
2022-08-24 17:14:27 -07:00
? swap_in_loan_origination_fee.toFixed(4)
2022-08-10 14:52:28 -07:00
: loan_origination_fee > 0
2022-08-24 17:14:27 -07:00
? loan_origination_fee.toFixed(4)
2022-08-10 04:17:10 -07:00
: 0
2022-10-05 18:16:29 -07:00
let baseLogoURI
let quoteLogoURI
if (jupiterTokens.length) {
baseLogoURI = jupiterTokens.find(
(t) => t.symbol === swap_in_symbol
)?.logoURI
quoteLogoURI = jupiterTokens.find(
(t) => t.symbol === swap_out_symbol
)?.logoURI
}
2022-08-10 04:17:10 -07:00
return (
2022-09-19 18:09:34 -07:00
<div key={signature} className="border-b border-th-bkg-3 p-4">
2022-08-10 04:17:10 -07:00
<div className="flex items-center justify-between">
<div className="flex items-center space-x-4">
<div className="flex w-1/2 items-center">
<div className="mr-2 flex flex-shrink-0 items-center">
<Image
alt=""
width="24"
height="24"
src={baseLogoURI || ''}
2022-08-10 04:17:10 -07:00
/>
</div>
<div>
2022-09-19 18:09:34 -07:00
<p className="mb-1.5 whitespace-nowrap font-mono leading-none text-th-fgd-1">
{swap_in_amount.toFixed(2)}{' '}
<span className="font-body">{swap_in_symbol}</span>
</p>
<p className="font-mono text-xs leading-none text-th-fgd-3">
2022-08-24 17:14:27 -07:00
{formatFixedDecimals(swap_in_price_usd, true)}
2022-09-19 18:09:34 -07:00
<span className="mx-1 font-body text-th-fgd-4">
|
</span>
2022-08-24 17:14:27 -07:00
{formatFixedDecimals(
swap_in_amount * swap_in_price_usd,
true
)}
2022-08-10 04:17:10 -07:00
</p>
</div>
</div>
<div className="">
<ArrowRightIcon className="h-4 w-4 flex-shrink-0 text-th-fgd-4" />
</div>
<div className="flex w-1/2 items-center">
<div className="mr-2 flex flex-shrink-0 items-center">
<Image
alt=""
width="24"
height="24"
src={quoteLogoURI || ''}
2022-08-10 04:17:10 -07:00
/>
</div>
<div>
2022-09-19 18:09:34 -07:00
<p className="mb-1.5 whitespace-nowrap leading-none text-th-fgd-1">
{swap_out_amount.toFixed(2)}{' '}
<span className="font-body">{swap_out_symbol}</span>
</p>
<p className="font-mono text-xs leading-none text-th-fgd-3">
2022-08-24 17:14:27 -07:00
{formatFixedDecimals(swap_out_price_usd, true)}
2022-09-19 18:09:34 -07:00
<span className="mx-1 font-body text-th-fgd-4">
|
</span>
2022-08-24 17:14:27 -07:00
{formatFixedDecimals(
swap_out_amount * swap_out_price_usd,
true
)}
2022-08-10 04:17:10 -07:00
</p>
</div>
</div>
</div>
<IconButton
onClick={() => handleShowSwapDetails(signature)}
2022-08-10 04:17:10 -07:00
>
<ChevronDownIcon
className={`${
showSwapDetails === signature
? 'rotate-180'
: 'rotate-360'
} h-6 w-6 flex-shrink-0 text-th-fgd-1`}
2022-08-10 04:17:10 -07:00
/>
</IconButton>
</div>
<Transition
appear={true}
show={showSwapDetails === signature}
2022-08-10 04:17:10 -07:00
as={Fragment}
enter="transition ease-in duration-200"
2022-08-10 04:17:10 -07:00
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition ease-out"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<div className="mt-4 grid grid-cols-2 gap-4 border-t border-th-bkg-3 pt-4">
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">{t('date')}</p>
<p className="text-th-fgd-1">
{dayjs(block_datetime).format('ddd D MMM')}
</p>
<p className="text-xs text-th-fgd-3">
{dayjs(block_datetime).format('h:mma')}
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">{t('borrow')}</p>
2022-09-19 18:09:34 -07:00
<p className="font-mono text-th-fgd-1">
{borrowAmount}{' '}
<span className="font-body">{swap_in_symbol}</span>
</p>
2022-08-10 04:17:10 -07:00
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('borrow-fee')}
</p>
2022-09-19 18:09:34 -07:00
<p className="font-mono text-th-fgd-1">${borrowFee}</p>
2022-08-10 04:17:10 -07:00
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">
{t('transaction')}
</p>
2022-09-27 20:07:26 -07:00
<a
className="default-transition flex items-center text-th-fgd-1 hover:text-th-fgd-3"
href={`${preferredExplorer.url}${signature}`}
target="_blank"
rel="noopener noreferrer"
>
<Image
alt=""
width="20"
height="20"
src={`/explorer-logos/${preferredExplorer.name}.png`}
/>
<span className="ml-1.5 text-base">{`View on ${t(
`settings:${preferredExplorer.name}`
)}`}</span>
</a>
2022-08-10 04:17:10 -07:00
</div>
</div>
</Transition>
</div>
)
})}
</div>
)
) : (
2022-09-19 04:32:59 -07:00
<div className="flex flex-col items-center p-8">
2022-08-10 04:17:10 -07:00
<ClockIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
2022-09-19 03:43:38 -07:00
<p>No swap history found...</p>
2022-08-10 04:17:10 -07:00
</div>
)
) : (
<div className="mt-8 space-y-2">
2022-09-08 18:00:47 -07:00
<SheenLoader className="flex flex-1">
2022-08-10 04:17:10 -07:00
<div className="h-8 w-full rounded bg-th-bkg-2" />
</SheenLoader>
2022-09-08 18:00:47 -07:00
<SheenLoader className="flex flex-1">
2022-08-10 04:17:10 -07:00
<div className="h-16 w-full rounded bg-th-bkg-2" />
</SheenLoader>
2022-09-08 18:00:47 -07:00
<SheenLoader className="flex flex-1">
2022-08-10 04:17:10 -07:00
<div className="h-16 w-full rounded bg-th-bkg-2" />
</SheenLoader>
2022-09-08 18:00:47 -07:00
<SheenLoader className="flex flex-1">
2022-08-10 04:17:10 -07:00
<div className="h-16 w-full rounded bg-th-bkg-2" />
</SheenLoader>
</div>
)
) : (
2022-09-19 04:32:59 -07:00
<div className="flex flex-col items-center p-8">
2022-08-10 04:17:10 -07:00
<LinkIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>Connect to view your swap history</p>
2022-08-10 04:17:10 -07:00
</div>
)
}
export default SwapHistoryTable