import { Fragment, 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 } 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 { 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' const SwapHistoryTable = () => { const { t } = useTranslation(['common', 'settings', 'swap']) const swapHistory = mangoStore((s) => s.mangoAccount.stats.swapHistory.data) const initialLoad = mangoStore( (s) => s.mangoAccount.stats.swapHistory.initialLoad ) const { mangoTokens } = useJupiterMints() const [showSwapDetails, setSwapDetails] = useState('') const actions = mangoStore.getState().actions const { mangoAccountAddress } = useMangoAccount() const { width } = useViewport() const showTableView = width ? width > breakpoints.md : false const [preferredExplorer] = useLocalStorageState( PREFERRED_EXPLORER_KEY, EXPLORERS[0] ) useEffect(() => { if (mangoAccountAddress) { actions.fetchSwapHistory(mangoAccountAddress) } }, [actions, mangoAccountAddress]) const handleShowSwapDetails = (signature: string) => { showSwapDetails ? setSwapDetails('') : setSwapDetails(signature) } return initialLoad ? ( mangoAccountAddress && swapHistory.length ? ( 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) console.log('mangoTokens', mangoTokens) if (mangoTokens.length) { baseLogoURI = mangoTokens.find( (t) => t.symbol === inSymbol )?.logoURI quoteLogoURI = mangoTokens.find( (t) => t.symbol === outSymbol )?.logoURI } const inDecimals = countLeadingZeros(swap_in_amount) const outDecimals = countLeadingZeros(swap_out_amount) + 2 return ( ) })}
{t('date')} {t('swap')} {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 === inSymbol )?.logoURI quoteLogoURI = mangoTokens.find( (t) => t.symbol === outSymbol )?.logoURI } return (

{swap_in_amount.toFixed(2)}{' '} {inSymbol}

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

{swap_out_amount.toFixed(2)}{' '} {outSymbol}

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

handleShowSwapDetails(signature)}>

{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}` )}`}
) })}
) ) : (

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

) ) : (
{[...Array(4)].map((x, i) => (
))}
) } export default SwapHistoryTable