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 useInterval from '../hooks/useInterval'
|
|
|
|
import ChartApi from '../utils/chartDataConnector'
|
|
|
|
import { ElementTitle } from './styles'
|
2022-01-31 07:52:28 -08:00
|
|
|
import { getDecimalCount, isEqual, usdFormatter } from '../utils/index'
|
2022-02-14 09:17:11 -08:00
|
|
|
import useMangoStore, { CLUSTER } from '../stores/useMangoStore'
|
2021-09-19 17:36:02 -07:00
|
|
|
import { useViewport } from '../hooks/useViewport'
|
|
|
|
import { breakpoints } from './TradePageGrid'
|
|
|
|
import { ExpandableRow } from './TableElements'
|
2021-10-20 05:42:40 -07:00
|
|
|
import { useTranslation } from 'next-i18next'
|
2021-04-05 13:48:24 -07:00
|
|
|
|
2021-07-05 08:03:57 -07:00
|
|
|
export default function RecentMarketTrades() {
|
2021-10-20 05:42:40 -07:00
|
|
|
const { t } = useTranslation('common')
|
2021-07-05 08:03:57 -07:00
|
|
|
const mangoConfig = useMangoStore((s) => s.selectedMangoGroup.config)
|
|
|
|
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
|
2021-08-19 19:47:25 -07:00
|
|
|
const market = useMangoStore((s) => s.selectedMarket.current)
|
2021-09-19 17:36:02 -07:00
|
|
|
const { width } = useViewport()
|
|
|
|
const isMobile = width ? width < breakpoints.sm : false
|
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-06-12 19:03:05 -07:00
|
|
|
|
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(() => {
|
2022-02-14 09:17:11 -08:00
|
|
|
if (CLUSTER === 'mainnet') {
|
|
|
|
fetchTradesForChart()
|
|
|
|
}
|
2021-04-12 20:39:08 -07:00
|
|
|
}, [fetchTradesForChart])
|
|
|
|
|
|
|
|
useInterval(async () => {
|
2022-02-14 09:17:11 -08:00
|
|
|
if (CLUSTER === 'mainnet') {
|
|
|
|
fetchTradesForChart()
|
|
|
|
}
|
2022-01-16 17:59:54 -08:00
|
|
|
}, 2000)
|
2021-04-05 13:48:24 -07:00
|
|
|
|
2021-09-19 17:36:02 -07:00
|
|
|
return !isMobile ? (
|
|
|
|
<>
|
2021-10-20 05:42:40 -07:00
|
|
|
<ElementTitle>{t('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-10-20 05:42:40 -07:00
|
|
|
<div>{`${t('price')} (${mangoConfig.quoteSymbol})`} </div>
|
|
|
|
<div className={`text-right`}>
|
|
|
|
{t('size')} ({marketConfig.baseSymbol})
|
|
|
|
</div>
|
|
|
|
<div className={`text-right`}>{t('time')}</div>
|
2021-04-05 13:48:24 -07:00
|
|
|
</div>
|
|
|
|
{!!trades.length && (
|
2022-01-31 07:52:28 -08:00
|
|
|
<div className="text-xs">
|
2021-04-05 13:48:24 -07:00
|
|
|
{trades.map((trade: ChartTradeType, i: number) => (
|
2022-01-31 07:52:28 -08:00
|
|
|
<div key={i} className={`leading-6 grid grid-cols-3`}>
|
2021-08-19 19:47:25 -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)
|
2022-01-31 07:52:28 -08:00
|
|
|
? usdFormatter(
|
|
|
|
trade.price,
|
|
|
|
getDecimalCount(market.tickSize),
|
|
|
|
false
|
2021-04-05 13:48:24 -07:00
|
|
|
)
|
2021-08-23 10:21:20 -07:00
|
|
|
: ''}
|
2021-04-05 13:48:24 -07:00
|
|
|
</div>
|
2021-12-01 12:45:18 -08:00
|
|
|
<div className={`text-right text-th-fgd-3`}>
|
2021-04-05 13:48:24 -07:00
|
|
|
{market?.minOrderSize && !isNaN(trade.size)
|
|
|
|
? Number(trade.size).toFixed(
|
|
|
|
getDecimalCount(market.minOrderSize)
|
|
|
|
)
|
2021-08-23 10:21:20 -07:00
|
|
|
: ''}
|
2021-08-19 19:47:25 -07:00
|
|
|
</div>
|
2022-02-09 19:33:43 -08: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>
|
|
|
|
)}
|
2021-09-19 17:36:02 -07:00
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<ExpandableRow
|
|
|
|
buttonTemplate={
|
2021-11-03 04:28:58 -07:00
|
|
|
<div className="flex justify-between text-left w-full">
|
2021-10-20 05:42:40 -07:00
|
|
|
<div className="mb-0.5 text-fgd-1">{t('recent-trades')}</div>
|
2021-09-19 17:36:02 -07:00
|
|
|
</div>
|
|
|
|
}
|
|
|
|
panelTemplate={
|
|
|
|
!!trades.length && (
|
|
|
|
<div className="col-span-2">
|
|
|
|
{trades.map((trade: ChartTradeType, i: number) => (
|
|
|
|
<div key={i} className={`leading-5 grid grid-cols-3 text-xs`}>
|
|
|
|
<div
|
|
|
|
className={`${
|
|
|
|
trade.side === 'buy' ? `text-th-green` : `text-th-red`
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{market?.tickSize && !isNaN(trade.price)
|
|
|
|
? Number(trade.price).toFixed(
|
|
|
|
getDecimalCount(market.tickSize)
|
|
|
|
)
|
|
|
|
: ''}
|
|
|
|
</div>
|
|
|
|
<div className={`text-right`}>
|
|
|
|
{market?.minOrderSize && !isNaN(trade.size)
|
|
|
|
? Number(trade.size).toFixed(
|
|
|
|
getDecimalCount(market.minOrderSize)
|
|
|
|
)
|
|
|
|
: ''}
|
|
|
|
</div>
|
|
|
|
<div className={`text-right text-th-fgd-4`}>
|
|
|
|
{trade.time && new Date(trade.time).toLocaleTimeString()}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
rounded
|
|
|
|
/>
|
2021-04-05 13:48:24 -07:00
|
|
|
)
|
|
|
|
}
|