mango-ui-v3/components/RecentMarketTrades.tsx

81 lines
2.7 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 { ChartTradeType } from '../@types/types'
import FloatingElement from './FloatingElement'
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-07-05 08:03:57 -07:00
import useMangoStore from '../stores/useMangoStore'
2021-04-05 13:48:24 -07:00
2021-07-05 08:03:57 -07:00
export default function RecentMarketTrades() {
const mangoConfig = useMangoStore((s) => s.selectedMangoGroup.config)
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
2021-08-17 13:47:57 -07:00
// const market = useMangoStore((s) => s.selectedMarket.current)
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-07-05 08:03:57 -07:00
if (!marketConfig) return
2021-07-05 08:03:57 -07:00
const newTrades = await ChartApi.getRecentTrades(
marketConfig.publicKey.toString()
)
2021-05-31 13:30:59 -07:00
if (!newTrades) return null
2021-05-01 21:31:55 -07:00
if (newTrades.length && trades.length === 0) {
2021-04-07 08:44:22 -07:00
setTrades(newTrades)
2021-04-12 20:39:08 -07:00
} else if (
2021-05-01 21:31:55 -07:00
newTrades?.length &&
2021-04-12 20:39:08 -07:00
!isEqual(newTrades[0], trades[0], Object.keys(newTrades[0]))
) {
2021-04-07 08:44:22 -07:00
setTrades(newTrades)
}
2021-07-05 08:03:57 -07:00
}, [marketConfig, trades])
2021-04-12 20:39:08 -07:00
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>
2021-04-26 05:53:27 -07:00
<div className={`grid grid-cols-3 text-th-fgd-4 mb-2 text-xs`}>
2021-07-05 08:03:57 -07:00
<div>Price ({mangoConfig.quoteSymbol}) </div>
<div className={`text-right`}>Size ({marketConfig.baseSymbol})</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-07-19 10:13:03 -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}
2021-07-19 10:13:03 -07:00
</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>
)
}