import { useCallback, useEffect, useState } from 'react' import { getDecimalCount } from '../utils' import { ChartTradeType } from '../@types/types' import FloatingElement from './FloatingElement' import useMarket from '../hooks/useMarket' import useInterval from '../hooks/useInterval' import ChartApi from '../utils/chartDataConnector' import { ElementTitle } from './styles' import { isEqual } from '../utils/index' export default function PublicTrades() { const { baseCurrency, quoteCurrency, market, marketAddress } = useMarket() const [trades, setTrades] = useState([]) const fetchTradesForChart = useCallback(async () => { const newTrades = await ChartApi.getRecentTrades(marketAddress) if (trades.length === 0) { setTrades(newTrades) } else if ( newTrades && !isEqual(newTrades[0], trades[0], Object.keys(newTrades[0])) ) { setTrades(newTrades) } }, [marketAddress, trades]) useEffect(() => { fetchTradesForChart() }, [fetchTradesForChart]) useInterval(async () => { fetchTradesForChart }, 5000) return ( Recent Trades
Price ({quoteCurrency})
Size ({baseCurrency})
Time
{!!trades.length && (
{trades.map((trade: ChartTradeType, i: number) => (
{market?.tickSize && !isNaN(trade.price) ? Number(trade.price).toFixed( getDecimalCount(market.tickSize) ) : trade.price}
{market?.minOrderSize && !isNaN(trade.size) ? Number(trade.size).toFixed( getDecimalCount(market.minOrderSize) ) : trade.size}
{trade.time && new Date(trade.time).toLocaleTimeString()}
))}
)}
) }