mango-ui-v3/components/RecentMarketTrades.tsx

75 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-04-12 20:39:08 -07:00
import { useCallback, useEffect, useState } from 'react'
2021-04-05 13:48:24 -07:00
import { getDecimalCount } from '../utils'
import { ChartTradeType } from '../@types/types'
import FloatingElement from './FloatingElement'
2021-04-06 15:11:42 -07:00
import useMarket from '../hooks/useMarket'
2021-04-05 13:48:24 -07:00
import useInterval from '../hooks/useInterval'
import ChartApi from '../utils/chartDataConnector'
import { ElementTitle } from './styles'
2021-04-07 08:44:22 -07:00
import { isEqual } from '../utils/index'
2021-04-05 13:48:24 -07:00
export default function PublicTrades() {
2021-04-06 15:11:42 -07:00
const { baseCurrency, quoteCurrency, market, marketAddress } = useMarket()
2021-04-05 13:48:24 -07:00
const [trades, setTrades] = useState([])
2021-04-12 20:39:08 -07:00
const fetchTradesForChart = useCallback(async () => {
2021-04-07 08:44:22 -07:00
const newTrades = await ChartApi.getRecentTrades(marketAddress)
if (trades.length === 0) {
setTrades(newTrades)
2021-04-12 20:39:08 -07:00
} else if (
newTrades &&
!isEqual(newTrades[0], trades[0], Object.keys(newTrades[0]))
) {
2021-04-07 08:44:22 -07:00
setTrades(newTrades)
}
2021-04-12 20:39:08 -07:00
}, [marketAddress, trades])
useEffect(() => {
fetchTradesForChart()
}, [fetchTradesForChart])
useInterval(async () => {
fetchTradesForChart()
2021-04-05 13:48:24 -07:00
}, 5000)
return (
<FloatingElement>
2021-04-13 07:10:57 -07:00
<ElementTitle>Recent Trades</ElementTitle>
<div className={`grid grid-cols-3 text-th-fgd-4 mb-2`}>
2021-04-05 13:48:24 -07:00
<div>Price ({quoteCurrency}) </div>
<div className={`text-right`}>Size ({baseCurrency})</div>
<div className={`text-right`}>Time</div>
2021-04-05 13:48:24 -07:00
</div>
{!!trades.length && (
<div>
{trades.map((trade: ChartTradeType, i: number) => (
2021-04-16 04:50:56 -07:00
<div key={i} className={`leading-7 grid grid-cols-3`}>
2021-04-05 13:48:24 -07:00
<div
2021-04-13 07:10:57 -07:00
className={`${
trade.side === 'buy' ? `text-th-green` : `text-th-red`
}`}
2021-04-05 13:48:24 -07:00
>
{market?.tickSize && !isNaN(trade.price)
? Number(trade.price).toFixed(
getDecimalCount(market.tickSize)
)
: trade.price}
</div>
<div className={`text-right`}>
2021-04-05 13:48:24 -07:00
{market?.minOrderSize && !isNaN(trade.size)
? Number(trade.size).toFixed(
getDecimalCount(market.minOrderSize)
)
: trade.size}
</div>
2021-04-16 04:50:56 -07:00
<div className={`text-right text-th-fgd-3`}>
2021-04-05 13:48:24 -07:00
{trade.time && new Date(trade.time).toLocaleTimeString()}
</div>
</div>
))}
</div>
)}
</FloatingElement>
)
}