import { ArrowSmDownIcon } from '@heroicons/react/solid' import BN from 'bn.js' import useTradeHistory from '../hooks/useTradeHistory' import Link from 'next/link' import { useRouter } from 'next/router' import SideBadge from './SideBadge' import { LinkButton } from './Button' import { useSortableData } from '../hooks/useSortableData' import { useViewport } from '../hooks/useViewport' import { breakpoints } from './TradePageGrid' import { Table, Td, Th, TrBody, TrHead } from './TableElements' import { ExpandableRow } from './TableElements' import { formatUsdValue } from '../utils' import { useTranslation } from 'next-i18next' const TradeHistoryTable = ({ numTrades }: { numTrades?: number }) => { const { t } = useTranslation('common') const { asPath } = useRouter() const tradeHistory = useTradeHistory({ excludePerpLiquidations: true }) const { items, requestSort, sortConfig } = useSortableData(tradeHistory) const { width } = useViewport() const isMobile = width ? width < breakpoints.md : false const renderTradeDateTime = (timestamp: BN | string) => { let date // don't compare to BN because of npm maddness // prototypes can be different due to multiple versions being imported if (typeof timestamp === 'string') { date = new Date(timestamp) } else { date = new Date(timestamp.toNumber() * 1000) } return ( <>
{date.toLocaleDateString()}
{date.toLocaleTimeString()}
) } const renderMarketName = (trade: any) => { let marketType, baseSymbol if (trade.marketName.includes('PERP')) { marketType = 'perp' baseSymbol = trade.marketName.slice(0, trade.marketName.indexOf('-')) } else if (trade.marketName.includes('USDC')) { marketType = 'spot' baseSymbol = trade.marketName.slice(0, trade.marketName.indexOf('/')) } else { return {trade.marketName} } const location = `/${marketType}/${baseSymbol}` if (asPath.includes(location)) { return {trade.marketName} } else { return ( {trade.marketName} ) } } const filteredTrades = numTrades ? items.slice(0, numTrades) : items return (
{tradeHistory && tradeHistory.length ? ( !isMobile ? ( {filteredTrades.map((trade: any, index) => { return ( ) })}
requestSort('market')} > {t('market')} requestSort('side')} > {t('side')} requestSort('size')} > {t('size')} requestSort('price')} > {t('price')} requestSort('value')} > {t('value')} requestSort('liquidity')} > {t('liquidity')} requestSort('feeCost')} > {t('fee')} requestSort('loadTimestamp')} > {t('approximate-time')}
{renderMarketName(trade)}
{trade.size} ${new Intl.NumberFormat('en-US').format(trade.price)} {formatUsdValue(trade.value)} {trade.liquidity} {formatUsdValue(trade.feeCost)} {trade.loadTimestamp || trade.timestamp ? renderTradeDateTime( trade.loadTimestamp || trade.timestamp ) : t('recent')}
) : ( items.map((trade: any, index) => (
{trade.loadTimestamp || trade.timestamp ? renderTradeDateTime( trade.loadTimestamp || trade.timestamp ) : t('recent')}
{trade.marketName}
{trade.side.toUpperCase()} {trade.size}
} key={`${index}`} index={index} panelTemplate={
{t('price')}
{formatUsdValue(trade.price)}
{t('value')}
{formatUsdValue(trade.value)}
{t('liquidity')}
{trade.liquidity}
{t('fee')}
{formatUsdValue(trade.feeCost)}
} /> )) ) ) : (
{t('no-history')} {asPath === '/account' ? ( {t('make-trade')} ) : null}
)}
{numTrades && items.length > numTrades ? (
{t('view-all-trades')}
) : null}
) } export default TradeHistoryTable