import { Fragment, useCallback, useEffect, useState } from 'react' import { ArrowRightIcon, ChevronDownIcon, NoSymbolIcon, } from '@heroicons/react/20/solid' import dayjs from 'dayjs' import { useTranslation } from 'next-i18next' import Image from 'next/legacy/image' import { breakpoints } from '../../utils/theme' import { useViewport } from '../../hooks/useViewport' import { IconButton, LinkButton } from '../shared/Button' import { Transition } from '@headlessui/react' import SheenLoader from '../shared/SheenLoader' import mangoStore from '@store/mangoStore' import { countLeadingZeros, formatDecimal, formatFixedDecimals, trimDecimals, } from '../../utils/numbers' import useLocalStorageState from 'hooks/useLocalStorageState' import { PAGINATION_PAGE_LENGTH, PREFERRED_EXPLORER_KEY } from 'utils/constants' import Tooltip from '@components/shared/Tooltip' import { formatTokenSymbol } from 'utils/tokens' import useJupiterMints from 'hooks/useJupiterMints' import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements' import { EXPLORERS } from '@components/settings/PreferredExplorerSettings' import useMangoAccount from 'hooks/useMangoAccount' import ConnectEmptyState from '@components/shared/ConnectEmptyState' import { useWallet } from '@solana/wallet-adapter-react' const SwapHistoryTable = () => { const { t } = useTranslation(['common', 'settings', 'swap']) const swapHistory = mangoStore((s) => s.mangoAccount.swapHistory.data) const loadSwapHistory = mangoStore((s) => s.mangoAccount.swapHistory.loading) const { mangoTokens } = useJupiterMints() const [showSwapDetails, setSwapDetails] = useState('') const [offset, setOffset] = useState(0) const actions = mangoStore.getState().actions const { mangoAccountAddress } = useMangoAccount() const { connected } = useWallet() const { width } = useViewport() const showTableView = width ? width > breakpoints.md : false const [preferredExplorer] = useLocalStorageState( PREFERRED_EXPLORER_KEY, EXPLORERS[0] ) useEffect(() => { if (mangoAccountAddress) { actions.fetchSwapHistory(mangoAccountAddress) setOffset(0) } }, [actions, mangoAccountAddress]) const handleShowMore = useCallback(() => { const set = mangoStore.getState().set set((s) => { s.mangoAccount.swapHistory.loading = true }) if (!mangoAccountAddress) return setOffset(offset + PAGINATION_PAGE_LENGTH) actions.fetchSwapHistory( mangoAccountAddress, 0, offset + PAGINATION_PAGE_LENGTH ) }, [actions, offset, mangoAccountAddress]) const handleShowSwapDetails = (signature: string) => { showSwapDetails ? setSwapDetails('') : setSwapDetails(signature) } return mangoAccountAddress && (swapHistory.length || loadSwapHistory) ? ( <> {showTableView ? ( {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( (t) => t.symbol.toUpperCase() === inSymbol.toUpperCase() )?.logoURI quoteLogoURI = mangoTokens.find( (t) => t.symbol.toUpperCase() === outSymbol.toUpperCase() )?.logoURI } const inDecimals = countLeadingZeros(swap_in_amount) + 2 const outDecimals = countLeadingZeros(swap_out_amount) + 2 return ( ) })}
{t('date')} {t('swap:paid')} {t('swap:received')} {t('value')} {t('borrow')} {t('borrow-fee')}

{dayjs(block_datetime).format('ddd D MMM')}

{dayjs(block_datetime).format('h:mma')}

{`${formatDecimal(swap_in_amount, inDecimals)}`} {inSymbol}

{t('price')}: {' '} {formatFixedDecimals(swap_in_price_usd, true)}

{`${formatDecimal(swap_out_amount, outDecimals)}`} {outSymbol}

{t('price')}:{' '} {formatFixedDecimals(swap_out_price_usd, true)}

{formatFixedDecimals( swap_out_price_usd * swap_out_amount, false, true )}

{borrowAmount} {inSymbol}

{borrowFee} {inSymbol}

) : (
{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( (t) => t.symbol.toUpperCase() === inSymbol.toUpperCase() )?.logoURI quoteLogoURI = mangoTokens.find( (t) => t.symbol.toUpperCase() === outSymbol.toUpperCase() )?.logoURI } const inDecimals = countLeadingZeros(swap_in_amount) + 2 const outDecimals = countLeadingZeros(swap_out_amount) + 2 return (

{formatDecimal(swap_in_amount, inDecimals)}{' '} {inSymbol}

{t('price')}: {' '} {formatFixedDecimals(swap_in_price_usd, true)}

{formatDecimal(swap_out_amount, outDecimals)}{' '} {outSymbol}

{t('price')}: {' '} {formatFixedDecimals(swap_out_price_usd, true)}

handleShowSwapDetails(signature)} size="medium" >

{t('date')}

{dayjs(block_datetime).format('ddd D MMM')}

{dayjs(block_datetime).format('h:mma')}

{t('borrow')}

{borrowAmount}{' '} {inSymbol}

{t('borrow-fee')}

${borrowFee}

{t('transaction')}

{`View on ${t( `settings:${preferredExplorer.name}` )}`}
) })}
)} {loadSwapHistory ? (
{[...Array(4)].map((x, i) => (
))}
) : null} {swapHistory.length && swapHistory.length % PAGINATION_PAGE_LENGTH === 0 ? (
Show More
) : null} ) : mangoAccountAddress || connected ? (

{t('swap:no-history')}

) : (
) } export default SwapHistoryTable