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

451 lines
17 KiB
TypeScript
Raw Normal View History

2023-01-11 19:21:23 -08:00
import { Fragment, useCallback, useEffect, useState } from 'react'
2022-08-10 04:17:10 -07:00
import {
ArrowRightIcon,
ChevronDownIcon,
2022-11-24 15:56:31 -08:00
NoSymbolIcon,
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'
2022-10-28 14:46:38 -07:00
import Image from 'next/legacy/image'
2022-09-19 03:43:38 -07:00
import { breakpoints } from '../../utils/theme'
import { useViewport } from '../../hooks/useViewport'
2023-01-11 19:21:23 -08:00
import { IconButton, LinkButton } 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'
import mangoStore from '@store/mangoStore'
2022-08-24 17:14:27 -07:00
import {
countLeadingZeros,
2023-01-07 10:59:32 -08:00
formatDecimal,
2022-08-24 17:14:27 -07:00
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'
2023-01-18 02:36:45 -08:00
import { PAGINATION_PAGE_LENGTH, PREFERRED_EXPLORER_KEY } from 'utils/constants'
2022-09-27 20:07:26 -07:00
import Tooltip from '@components/shared/Tooltip'
import { formatTokenSymbol } from 'utils/tokens'
2022-11-18 11:11:06 -08:00
import useJupiterMints from 'hooks/useJupiterMints'
2022-11-30 19:32:32 -08:00
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
2022-11-24 18:39:14 -08:00
import { EXPLORERS } from '@components/settings/PreferredExplorerSettings'
2022-12-19 10:48:59 -08:00
import useMangoAccount from 'hooks/useMangoAccount'
2022-08-10 04:17:10 -07:00
const SwapHistoryTable = () => {
2022-11-24 15:56:31 -08:00
const { t } = useTranslation(['common', 'settings', 'swap'])
const swapHistory = mangoStore((s) => s.mangoAccount.stats.swapHistory.data)
2023-01-11 19:21:23 -08:00
const loadSwapHistory = mangoStore(
(s) => s.mangoAccount.stats.swapHistory.loading
)
2022-11-18 11:11:06 -08:00
const { mangoTokens } = useJupiterMints()
const [showSwapDetails, setSwapDetails] = useState('')
2023-01-11 19:21:23 -08:00
const [offset, setOffset] = useState(0)
2023-01-03 14:20:00 -08:00
const actions = mangoStore.getState().actions
const { mangoAccountAddress } = useMangoAccount()
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
2022-12-19 10:48:59 -08:00
useEffect(() => {
if (mangoAccountAddress) {
actions.fetchSwapHistory(mangoAccountAddress)
2023-01-18 02:36:45 -08:00
setOffset(0)
2022-12-19 10:48:59 -08:00
}
}, [actions, mangoAccountAddress])
2022-12-19 10:48:59 -08:00
2023-01-11 19:21:23 -08:00
const handleShowMore = useCallback(() => {
const set = mangoStore.getState().set
set((s) => {
s.mangoAccount.stats.swapHistory.loading = true
})
if (!mangoAccountAddress) return
2023-01-18 02:36:45 -08:00
setOffset(offset + PAGINATION_PAGE_LENGTH)
actions.fetchSwapHistory(
mangoAccountAddress,
0,
offset + PAGINATION_PAGE_LENGTH
)
2023-01-11 19:21:23 -08:00
}, [actions, offset, mangoAccountAddress])
const handleShowSwapDetails = (signature: string) => {
showSwapDetails ? setSwapDetails('') : setSwapDetails(signature)
2022-08-10 04:17:10 -07:00
}
2023-01-11 19:21:23 -08:00
return mangoAccountAddress && (swapHistory.length || loadSwapHistory) ? (
<>
{showTableView ? (
<Table>
<thead>
<TrHead>
<Th className="text-left">{t('date')}</Th>
2023-01-18 03:41:44 -08:00
<Th className="text-left">{t('swap:paid')}</Th>
<Th className="text-left">{t('swap:received')}</Th>
<Th className="text-right">{t('value')}</Th>
<Th className="text-right">{t('borrow')}</Th>
<Th className="text-right">{t('borrow-fee')}</Th>
<Th />
</TrHead>
</thead>
<tbody>
2022-09-26 23:22:51 -07:00
{swapHistory.map((h) => {
2022-08-10 04:17:10 -07:00
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 =
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
const inSymbol = formatTokenSymbol(swap_in_symbol)
const outSymbol = formatTokenSymbol(swap_out_symbol)
2022-11-18 11:11:06 -08:00
if (mangoTokens.length) {
baseLogoURI = mangoTokens.find(
2023-01-18 03:41:44 -08:00
(t) => t.symbol.toUpperCase() === inSymbol.toUpperCase()
)?.logoURI
2022-11-18 11:11:06 -08:00
quoteLogoURI = mangoTokens.find(
2023-01-18 03:41:44 -08:00
(t) => t.symbol.toUpperCase() === outSymbol.toUpperCase()
)?.logoURI
}
2023-01-18 03:41:44 -08:00
const inDecimals = countLeadingZeros(swap_in_amount) + 2
const outDecimals = countLeadingZeros(swap_out_amount) + 2
2022-08-10 04:17:10 -07:00
return (
<TrBody key={signature}>
<Td>
<p className="font-body tracking-wider">
{dayjs(block_datetime).format('ddd D MMM')}
</p>
<p className="font-body text-xs text-th-fgd-3">
{dayjs(block_datetime).format('h:mma')}
</p>
</Td>
2023-01-18 03:41:44 -08:00
<Td>
<div className="flex items-center">
<div className="mr-2.5 flex flex-shrink-0 items-center">
<Image
alt=""
width="24"
height="24"
src={baseLogoURI || ''}
/>
</div>
<div>
<p className="whitespace-nowrap">
{`${formatDecimal(swap_in_amount, inDecimals)}`}
<span className="ml-1 font-body text-th-fgd-3">
{inSymbol}
</span>
</p>
<p className="text-xs text-th-fgd-3">
<span className="font-body text-th-fgd-4">
{t('price')}:
</span>{' '}
{formatFixedDecimals(swap_in_price_usd, true)}
</p>
2022-08-10 04:17:10 -07:00
</div>
2023-01-18 03:41:44 -08:00
</div>
</Td>
<Td>
<div className="flex items-center">
<div className="mr-2.5 flex flex-shrink-0 items-center">
<Image
alt=""
width="24"
height="24"
src={quoteLogoURI || ''}
/>
</div>
<div>
<p className="whitespace-nowrap">
{`${formatDecimal(swap_out_amount, outDecimals)}`}
<span className="ml-1 font-body text-th-fgd-3">
{outSymbol}
</span>
</p>
<p className="text-xs text-th-fgd-4">
<span className="font-body">{t('price')}:</span>{' '}
{formatFixedDecimals(swap_out_price_usd, true)}
</p>
2022-08-10 04:17:10 -07:00
</div>
</div>
</Td>
<Td>
<p className="text-right">
{formatFixedDecimals(
swap_out_price_usd * swap_out_amount,
2023-01-06 02:32:54 -08:00
false,
true
)}
</p>
</Td>
<Td>
<div className="flex flex-col text-right">
<p>
{borrowAmount}
<span className="ml-1 font-body text-th-fgd-3">
{inSymbol}
</span>
</p>
</div>
</Td>
<Td>
<div className="flex flex-col text-right">
<p>
{borrowFee}
<span className="ml-1 font-body text-th-fgd-3">
{inSymbol}
</span>
</p>
</div>
</Td>
<Td>
<div className="flex items-center justify-end">
<Tooltip
content={`View on ${t(
`settings:${preferredExplorer.name}`
)}`}
placement="top-end"
>
<a
href={`${preferredExplorer.url}${signature}`}
target="_blank"
rel="noopener noreferrer"
>
<div className="h-6 w-6">
<Image
alt=""
width="24"
height="24"
src={`/explorer-logos/${preferredExplorer.name}.png`}
/>
</div>
</a>
</Tooltip>
</div>
</Td>
</TrBody>
)
})}
</tbody>
</Table>
) : (
<div>
{swapHistory.map((h) => {
const {
block_datetime,
signature,
swap_in_amount,
swap_in_loan_origination_fee,
swap_in_price_usd,
swap_in_symbol,
swap_out_amount,
loan,
loan_origination_fee,
swap_out_price_usd,
swap_out_symbol,
} = h
const borrowAmount =
loan > 0
? `${trimDecimals(loan, countLeadingZeros(loan) + 2)}`
: 0
const borrowFee =
swap_in_loan_origination_fee > 0
? swap_in_loan_origination_fee.toFixed(4)
: loan_origination_fee > 0
? loan_origination_fee.toFixed(4)
: 0
let baseLogoURI
let quoteLogoURI
const inSymbol = formatTokenSymbol(swap_in_symbol)
const outSymbol = formatTokenSymbol(swap_out_symbol)
if (mangoTokens.length) {
baseLogoURI = mangoTokens.find(
2023-01-18 03:18:57 -08:00
(t) => t.symbol.toUpperCase() === inSymbol.toUpperCase()
)?.logoURI
quoteLogoURI = mangoTokens.find(
2023-01-18 03:18:57 -08:00
(t) => t.symbol.toUpperCase() === outSymbol.toUpperCase()
)?.logoURI
}
const inDecimals = countLeadingZeros(swap_in_amount) + 2
const outDecimals = countLeadingZeros(swap_out_amount) + 2
2023-01-18 03:41:44 -08:00
return (
<div key={signature} className="border-b border-th-bkg-3 p-4">
<div className="flex items-center justify-between">
<div className="flex items-center justify-between">
<div className="flex 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>
<p className="whitespace-nowrap font-mono text-th-fgd-1">
2023-01-18 03:41:44 -08:00
{formatDecimal(swap_in_amount, inDecimals)}{' '}
2022-12-04 17:06:50 -08:00
<span className="font-body text-th-fgd-3">
{inSymbol}
</span>
2022-09-19 18:09:34 -07:00
</p>
<p className="font-mono text-xs text-th-fgd-3">
<span className="font-body text-th-fgd-4">
{t('price')}:
</span>{' '}
{formatFixedDecimals(swap_in_price_usd, true)}
2022-08-10 04:17:10 -07:00
</p>
</div>
</div>
<ArrowRightIcon className="mx-4 h-4 w-4 flex-shrink-0 text-th-fgd-4" />
<div className="flex items-center">
<div className="mr-2 flex flex-shrink-0 items-center">
<Image
alt=""
width="24"
height="24"
src={quoteLogoURI || ''}
/>
</div>
<div>
<p className="whitespace-nowrap font-mono text-th-fgd-1">
2023-01-18 03:41:44 -08:00
{formatDecimal(swap_out_amount, outDecimals)}{' '}
<span className="font-body text-th-fgd-3">
{outSymbol}
</span>
</p>
<p className="font-mono text-xs text-th-fgd-3">
<span className="font-body text-th-fgd-4">
{t('price')}:
</span>{' '}
{formatFixedDecimals(swap_out_price_usd, true)}
2022-08-10 04:17:10 -07:00
</p>
</div>
</div>
</div>
2023-01-18 03:41:44 -08:00
<IconButton
onClick={() => handleShowSwapDetails(signature)}
size="medium"
>
<ChevronDownIcon
className={`${
showSwapDetails === signature
? 'rotate-180'
: 'rotate-360'
} h-6 w-6 flex-shrink-0 text-th-fgd-1`}
/>
</IconButton>
2022-08-10 04:17:10 -07:00
</div>
<Transition
appear={true}
show={showSwapDetails === signature}
as={Fragment}
enter="transition ease-in duration-200"
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>
<p className="font-mono text-th-fgd-1">
{borrowAmount}{' '}
<span className="font-body text-th-fgd-3">
{inSymbol}
</span>
</p>
</div>
<div className="col-span-1">
<p className="text-xs text-th-fgd-3">{t('borrow-fee')}</p>
<p className="font-mono text-th-fgd-1">${borrowFee}</p>
</div>
<div className="col-span-1">
<p className="mb-0.5 text-xs text-th-fgd-3">
{t('transaction')}
</p>
<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">{`View on ${t(
`settings:${preferredExplorer.name}`
)}`}</span>
</a>
</div>
</div>
</Transition>
</div>
)
})}
2022-08-10 04:17:10 -07:00
</div>
2023-01-11 19:21:23 -08:00
)}
{loadSwapHistory ? (
<div className="mt-4 space-y-1.5">
{[...Array(4)].map((x, i) => (
<SheenLoader className="mx-4 flex flex-1 md:mx-6" key={i}>
<div className="h-16 w-full bg-th-bkg-2" />
</SheenLoader>
))}
</div>
) : null}
2023-01-18 02:36:45 -08:00
{swapHistory.length &&
swapHistory.length % PAGINATION_PAGE_LENGTH === 0 ? (
<div className="flex justify-center py-6">
2023-01-11 19:21:23 -08:00
<LinkButton onClick={handleShowMore}>Show More</LinkButton>
</div>
) : null}
</>
2022-08-10 04:17:10 -07:00
) : (
2023-01-11 19:21:23 -08:00
<div className="flex flex-col items-center p-8">
<NoSymbolIcon className="mb-2 h-6 w-6 text-th-fgd-4" />
<p>{t('swap:no-history')}</p>
2022-08-10 04:17:10 -07:00
</div>
)
}
export default SwapHistoryTable