import { useState } from 'react'
import Link from 'next/link'
import { useRouter } from 'next/router'
import { useOpenOrders } from '../hooks/useOpenOrders'
import Button from './Button'
import Loading from './Loading'
import useMangoStore from '../stores/useMangoStore'
import { notify } from '../utils/notifications'
import SideBadge from './SideBadge'
import { Order, Market } from '@project-serum/serum/lib/market'
import {
PerpOrder,
PerpMarket,
MarketConfig,
} from '@blockworks-foundation/mango-client'
import { formatUsdValue, getDecimalCount, usdFormatter } from '../utils'
import { Table, Td, Th, TrBody, TrHead } from './TableElements'
import { useViewport } from '../hooks/useViewport'
import { breakpoints } from './TradePageGrid'
import { Row } from './TableElements'
import { PerpTriggerOrder } from '../@types/types'
import { useTranslation } from 'next-i18next'
const DesktopTable = ({ openOrders, cancelledOrderId, handleCancelOrder }) => {
const { t } = useTranslation('common')
const { asPath } = useRouter()
const renderMarketName = (market: MarketConfig) => {
const location =
market.kind === 'spot'
? `/market?name=${market.baseSymbol}%2FUSDC`
: `/market?name=${market.name}`
if (!asPath.includes(location)) {
return (
{market.name}
)
} else {
return {market.name}
}
}
return (
{t('market')} |
{t('size')} |
{t('size')} |
{t('price')} |
{t('value')} |
{t('condition')} |
{t('edit')}
|
{openOrders.map(({ order, market }, index) => {
const decimals = getDecimalCount(market.account.tickSize)
return (
{renderMarketName(market.config)}
|
|
{order.size} |
{usdFormatter(order.price, decimals)} |
{formatUsdValue(order.price * order.size)} |
{order.perpTrigger &&
`${t(order.orderType)} ${t(
order.triggerCondition
)} ${order.triggerPrice.toFixed(2)}`}
|
|
)
})}
)
}
const MobileTable = ({ openOrders, cancelledOrderId, handleCancelOrder }) => {
const { t } = useTranslation('common')
return (
<>
{openOrders.map(({ market, order }, index) => (
{market.config.name}
{order.side.toUpperCase()}
{order.perpTrigger
? `${order.size} ${order.triggerCondition} ${order.triggerPrice}`
: `${order.size} at ${formatUsdValue(order.price)}`}
))}
>
)
}
const OpenOrdersTable = () => {
const { t } = useTranslation('common')
const { asPath } = useRouter()
const openOrders = useOpenOrders()
const [cancelId, setCancelId] = useState(null)
const actions = useMangoStore((s) => s.actions)
const { width } = useViewport()
const isMobile = width ? width < breakpoints.md : false
const handleCancelOrder = async (
order: Order | PerpOrder | PerpTriggerOrder,
market: Market | PerpMarket
) => {
const wallet = useMangoStore.getState().wallet.current
const selectedMangoGroup =
useMangoStore.getState().selectedMangoGroup.current
const selectedMangoAccount =
useMangoStore.getState().selectedMangoAccount.current
const mangoClient = useMangoStore.getState().connection.client
setCancelId(order.orderId)
let txid
try {
if (!selectedMangoGroup || !selectedMangoAccount) return
if (market instanceof Market) {
txid = await mangoClient.cancelSpotOrder(
selectedMangoGroup,
selectedMangoAccount,
wallet,
market,
order as Order
)
actions.reloadOrders()
} else if (market instanceof PerpMarket) {
// TODO: this is not ideal
if (order['triggerCondition']) {
txid = await mangoClient.removeAdvancedOrder(
selectedMangoGroup,
selectedMangoAccount,
wallet,
(order as PerpTriggerOrder).orderId
)
actions.reloadOrders()
} else {
txid = await mangoClient.cancelPerpOrder(
selectedMangoGroup,
selectedMangoAccount,
wallet,
market,
order as PerpOrder,
false
)
}
}
notify({ title: t('cancel-success'), txid })
} catch (e) {
notify({
title: t('cancel-error'),
description: e.message,
txid: e.txid,
type: 'error',
})
console.log('error', `${e}`)
} finally {
actions.reloadMangoAccount()
actions.updateOpenOrders()
setCancelId(null)
}
}
const tableProps = {
openOrders,
cancelledOrderId: cancelId,
handleCancelOrder,
}
return (
{openOrders && openOrders.length > 0 ? (
!isMobile ? (
) : (
)
) : (
)}
)
}
export default OpenOrdersTable