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 ? (
<>