import { useCallback, useState } from 'react' import { useRouter } from 'next/router' import Link from 'next/link' import { useTranslation } from 'next-i18next' import { ExclamationIcon } from '@heroicons/react/outline' import useMangoStore from '../stores/useMangoStore' import Button, { LinkButton } from '../components/Button' import { useViewport } from '../hooks/useViewport' import { breakpoints } from './TradePageGrid' import { ExpandableRow, Table, Td, Th, TrBody, TrHead } from './TableElements' import { formatUsdValue, getPrecisionDigits, roundPerpSize } from '../utils' import Loading from './Loading' import MarketCloseModal from './MarketCloseModal' import PerpSideBadge from './PerpSideBadge' import PnlText from './PnlText' import { settlePnl } from './MarketPosition' import MobileTableHeader from './mobile/MobileTableHeader' const PositionsTable = () => { const { t } = useTranslation('common') const { reloadMangoAccount } = useMangoStore((s) => s.actions) const [settling, setSettling] = useState(false) const [settleSinglePos, setSettleSinglePos] = useState(null) const selectedMarket = useMangoStore((s) => s.selectedMarket.current) const selectedMarketConfig = useMangoStore((s) => s.selectedMarket.config) const price = useMangoStore((s) => s.tradeForm.price) const [showMarketCloseModal, setShowMarketCloseModal] = useState(false) const setMangoStore = useMangoStore((s) => s.set) const openPositions = useMangoStore( (s) => s.selectedMangoAccount.openPerpPositions ) const unsettledPositions = useMangoStore.getState().selectedMangoAccount.unsettledPerpPositions const { width } = useViewport() const isMobile = width ? width < breakpoints.md : false const { asPath } = useRouter() const handleCloseWarning = useCallback(() => { setShowMarketCloseModal(false) }, []) const handleSizeClick = (size, indexPrice) => { const sizePrecisionDigits = getPrecisionDigits(selectedMarket.minOrderSize) const priceOrDefault = price ? price : indexPrice const roundedSize = parseFloat(Math.abs(size).toFixed(sizePrecisionDigits)) const quoteSize = parseFloat((roundedSize * priceOrDefault).toFixed(2)) setMangoStore((state) => { state.tradeForm.baseSize = roundedSize state.tradeForm.quoteSize = quoteSize state.tradeForm.side = size > 0 ? 'sell' : 'buy' }) } const handleSettleAll = async () => { setSettling(true) for (const p of unsettledPositions) { await settlePnl(p.perpMarket, p.perpAccount, t, undefined) } reloadMangoAccount() setSettling(false) } const handleSettlePnl = async (perpMarket, perpAccount, index) => { setSettleSinglePos(index) await settlePnl(perpMarket, perpAccount, t, undefined) setSettleSinglePos(null) } return (
{unsettledPositions.length > 0 ? (

{t('unsettled-positions')}

{unsettledPositions.map((p, index) => { return (

{p.marketConfig.name}

{settleSinglePos === index ? ( ) : ( handleSettlePnl(p.perpMarket, p.perpAccount, index) } > {t('redeem-pnl')} )}
) })}
) : null}
{openPositions.length ? ( !isMobile ? ( {openPositions.map( ({ marketConfig, perpMarket, perpAccount, basePosition, notionalSize, indexPrice, avgEntryPrice, breakEvenPrice, unrealizedPnl, }) => { const basePositionUi = roundPerpSize( basePosition, marketConfig.baseSymbol ) return ( {showMarketCloseModal ? ( ) : null} ) } )}
{t('market')} {t('side')} {t('position-size')} {t('notional-size')} {t('average-entry')} {t('break-even')} {t('unrealized-pnl')}
{decodeURIComponent(asPath).includes( marketConfig.name ) ? ( {marketConfig.name} ) : ( {marketConfig.name} )}
{basePosition && selectedMarketConfig.kind === 'perp' && asPath.includes(marketConfig.baseSymbol) ? ( handleSizeClick(basePosition, indexPrice) } > {`${basePositionUi} ${marketConfig.baseSymbol}`} ) : ( {`${basePositionUi} ${marketConfig.baseSymbol}`} )} {formatUsdValue(Math.abs(notionalSize))} {avgEntryPrice ? formatUsdValue(avgEntryPrice) : '--'} {breakEvenPrice ? formatUsdValue(breakEvenPrice) : '--'} {breakEvenPrice ? ( ) : ( '--' )}
) : ( <> {openPositions.map( ( { marketConfig, basePosition, notionalSize, avgEntryPrice, breakEvenPrice, unrealizedPnl, }, index ) => { const basePositionUi = roundPerpSize( basePosition, marketConfig.baseSymbol ) return (
{marketConfig.name}
0 ? 'text-th-green' : 'text-th-red' }`} > {basePosition > 0 ? t('long').toUpperCase() : t('short').toUpperCase()} {basePositionUi}
} key={`${index}`} panelTemplate={
{t('average-entry')}
{avgEntryPrice ? formatUsdValue(avgEntryPrice) : '--'}
{t('notional-size')}
{formatUsdValue(notionalSize)}
{t('break-even')}
{breakEvenPrice ? formatUsdValue(breakEvenPrice) : '--'}
} /> ) } )} ) ) : (
{t('no-perp')}
)}
) } export default PositionsTable