import { useCallback, useEffect, useState } from 'react' import { ChartTradeType } from '../@types/types' import useInterval from '../hooks/useInterval' import ChartApi from '../utils/chartDataConnector' import { ElementTitle } from './styles' import { getDecimalCount, isEqual, usdFormatter } from '../utils/index' import useMangoStore, { CLUSTER } from '../stores/useMangoStore' import { useViewport } from '../hooks/useViewport' import { breakpoints } from './TradePageGrid' import { ExpandableRow } from './TableElements' import { useTranslation } from 'next-i18next' export default function RecentMarketTrades() { const { t } = useTranslation('common') const mangoConfig = useMangoStore((s) => s.selectedMangoGroup.config) const marketConfig = useMangoStore((s) => s.selectedMarket.config) const market = useMangoStore((s) => s.selectedMarket.current) const { width } = useViewport() const isMobile = width ? width < breakpoints.sm : false const [trades, setTrades] = useState([]) const fetchTradesForChart = useCallback(async () => { if (!marketConfig) return const newTrades = await ChartApi.getRecentTrades( marketConfig.publicKey.toString() ) if (!newTrades) return null if (newTrades.length && trades.length === 0) { setTrades(newTrades) } else if ( newTrades?.length && !isEqual(newTrades[0], trades[0], Object.keys(newTrades[0])) ) { setTrades(newTrades) } }, [marketConfig, trades]) useEffect(() => { if (CLUSTER === 'mainnet') { fetchTradesForChart() } }, [fetchTradesForChart]) useInterval(async () => { if (CLUSTER === 'mainnet') { fetchTradesForChart() } }, 2000) return !isMobile ? ( <> {t('recent-trades')}
{`${t('price')} (${mangoConfig.quoteSymbol})`}
{t('size')} ({marketConfig.baseSymbol})
{t('time')}
{!!trades.length && (
{trades.map((trade: ChartTradeType, i: number) => (
{market?.tickSize && !isNaN(trade.price) ? usdFormatter( trade.price, getDecimalCount(market.tickSize), false ) : ''}
{market?.minOrderSize && !isNaN(trade.size) ? Number(trade.size).toLocaleString(undefined, { maximumFractionDigits: getDecimalCount( market.minOrderSize ), }) : ''}
{trade.time && new Date(trade.time).toLocaleTimeString()}
))}
)} ) : (
{t('recent-trades')}
} panelTemplate={ !!trades.length && (
{trades.map((trade: ChartTradeType, i: number) => (
{market?.tickSize && !isNaN(trade.price) ? Number(trade.price).toFixed( getDecimalCount(market.tickSize) ) : ''}
{market?.minOrderSize && !isNaN(trade.size) ? Number(trade.size).toFixed( getDecimalCount(market.minOrderSize) ) : ''}
{trade.time && new Date(trade.time).toLocaleTimeString()}
))}
) } /> ) }