mango-v4-ui/components/stats/SpotMarketsTable.tsx

197 lines
6.6 KiB
TypeScript
Raw Normal View History

2022-10-08 04:37:08 -07:00
import { Serum3Market } from '@blockworks-foundation/mango-v4'
import { useTranslation } from 'next-i18next'
import { useTheme } from 'next-themes'
import { useMemo } from 'react'
2022-10-08 04:37:08 -07:00
import { useViewport } from '../../hooks/useViewport'
import mangoStore from '@store/mangoStore'
import { COLORS } from '../../styles/colors'
import { formatFixedDecimals } from '../../utils/numbers'
import { breakpoints } from '../../utils/theme'
import ContentBox from '../shared/ContentBox'
import Change from '../shared/Change'
import MarketLogos from '@components/trade/MarketLogos'
2022-10-10 14:15:35 -07:00
import dynamic from 'next/dynamic'
2022-11-18 11:11:06 -08:00
import { useCoingecko } from 'hooks/useCoingecko'
import useMangoGroup from 'hooks/useMangoGroup'
2022-11-20 02:44:14 -08:00
import { Table, Td, Th, TrBody, TrHead } from '@components/shared/TableElements'
2022-10-10 14:15:35 -07:00
const SimpleAreaChart = dynamic(
() => import('@components/shared/SimpleAreaChart'),
{ ssr: false }
)
2022-10-08 04:37:08 -07:00
const SpotMarketsTable = () => {
const { t } = useTranslation('common')
2022-11-18 11:11:06 -08:00
const { isLoading: loadingPrices, data: coingeckoPrices } = useCoingecko()
2022-11-20 12:32:38 -08:00
const { group } = useMangoGroup()
2022-10-08 04:37:08 -07:00
const serumMarkets = mangoStore((s) => s.serumMarkets)
const { theme } = useTheme()
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
return (
<ContentBox hideBorder hidePadding>
{showTableView ? (
2022-11-20 02:44:14 -08:00
<Table>
2022-10-08 04:37:08 -07:00
<thead>
2022-11-20 02:44:14 -08:00
<TrHead>
<Th className="text-left">{t('market')}</Th>
<Th className="text-right">{t('price')}</Th>
<Th className="hidden text-right lg:block"></Th>
<Th className="text-right">{t('rolling-change')}</Th>
</TrHead>
2022-10-08 04:37:08 -07:00
</thead>
<tbody>
{serumMarkets.map((market) => {
const bank = group?.getFirstBankByTokenIndex(
market.baseTokenIndex
)
const oraclePrice = bank?.uiPrice
const coingeckoData = coingeckoPrices.find((asset) =>
bank?.name === 'soETH'
? asset.symbol === 'ETH'
: asset.symbol === bank?.name
2022-10-08 04:37:08 -07:00
)
const change = coingeckoData
? ((coingeckoData.prices[coingeckoData.prices.length - 1][1] -
coingeckoData.prices[0][1]) /
coingeckoData.prices[0][1]) *
2022-10-08 04:37:08 -07:00
100
: 0
const chartData = coingeckoData ? coingeckoData.prices : undefined
2022-10-08 04:37:08 -07:00
return (
2022-11-20 02:44:14 -08:00
<TrBody key={market.publicKey.toString()}>
<Td>
2022-10-08 04:37:08 -07:00
<div className="flex items-center">
2022-10-10 19:16:13 -07:00
<MarketLogos market={market} />
2022-10-08 04:37:08 -07:00
<p className="font-body tracking-wide">{market.name}</p>
</div>
2022-11-20 02:44:14 -08:00
</Td>
<Td>
2022-10-08 04:37:08 -07:00
<div className="flex flex-col text-right">
<p>{formatFixedDecimals(oraclePrice!, true)}</p>
</div>
2022-11-20 02:44:14 -08:00
</Td>
<Td>
2022-11-18 11:11:06 -08:00
{!loadingPrices ? (
2022-10-08 04:37:08 -07:00
chartData !== undefined ? (
<SimpleAreaChart
color={
2022-11-30 19:32:32 -08:00
change >= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]
2022-10-08 04:37:08 -07:00
}
data={chartData}
height={40}
name={bank!.name}
width={104}
xKey="0"
yKey="1"
2022-10-08 04:37:08 -07:00
/>
) : bank?.name === 'USDC' ||
bank?.name === 'USDT' ? null : (
2022-10-08 04:37:08 -07:00
<p className="mb-0 text-th-fgd-4">{t('unavailable')}</p>
)
) : (
<div className="h-10 w-[104px] animate-pulse rounded bg-th-bkg-3" />
)}
2022-11-20 02:44:14 -08:00
</Td>
<Td>
2022-10-08 04:37:08 -07:00
<div className="flex flex-col items-end">
<Change change={change} />
2022-10-08 04:37:08 -07:00
</div>
2022-11-20 02:44:14 -08:00
</Td>
</TrBody>
2022-10-08 04:37:08 -07:00
)
})}
</tbody>
2022-11-20 02:44:14 -08:00
</Table>
2022-10-08 04:37:08 -07:00
) : (
<div>
{serumMarkets.map((market) => {
return (
<MobileSpotMarketItem
key={market.publicKey.toString()}
market={market}
/>
)
})}
</div>
)}
</ContentBox>
)
}
export default SpotMarketsTable
const MobileSpotMarketItem = ({ market }: { market: Serum3Market }) => {
const { t } = useTranslation('common')
2022-11-18 11:11:06 -08:00
const { isLoading: loadingPrices, data: coingeckoPrices } = useCoingecko()
const { group } = useMangoGroup()
2022-10-08 04:37:08 -07:00
const { theme } = useTheme()
const bank = group?.getFirstBankByTokenIndex(market.baseTokenIndex)
const coingeckoData = useMemo(() => {
2022-11-18 11:11:06 -08:00
if (!loadingPrices && bank) {
return coingeckoPrices.find((asset) => asset.symbol === bank?.name)
2022-10-08 04:37:08 -07:00
}
return null
2022-11-18 11:11:06 -08:00
}, [loadingPrices, bank])
2022-10-08 04:37:08 -07:00
const change = useMemo(() => {
if (coingeckoData) {
2022-10-08 04:37:08 -07:00
return (
((coingeckoData.prices[coingeckoData.prices.length - 1][1] -
coingeckoData.prices[0][1]) /
coingeckoData.prices[0][1]) *
2022-10-08 04:37:08 -07:00
100
)
}
return 0
}, [coingeckoData])
const chartData = useMemo(() => {
if (coingeckoData) {
return coingeckoData.prices
}
return undefined
}, [coingeckoData])
2022-10-08 04:37:08 -07:00
return (
<div className="border-b border-th-bkg-3 px-6 py-4">
<div className="flex items-center justify-between">
<div className="flex items-center">
2022-10-10 19:16:13 -07:00
<MarketLogos market={market} />
2022-10-08 04:37:08 -07:00
<div>
<p className="text-th-fgd-1">{market.name}</p>
<div className="flex items-center space-x-3">
<p className="font-mono">
2022-11-19 11:20:36 -08:00
{bank?.uiPrice ? formatFixedDecimals(bank.uiPrice, true) : '-'}
2022-10-08 04:37:08 -07:00
</p>
<Change change={change} />
2022-10-08 04:37:08 -07:00
</div>
</div>
</div>
2022-11-18 11:11:06 -08:00
{!loadingPrices ? (
chartData !== undefined ? (
2022-10-08 04:37:08 -07:00
<SimpleAreaChart
2022-11-30 19:32:32 -08:00
color={change >= 0 ? COLORS.UP[theme] : COLORS.DOWN[theme]}
2022-10-08 04:37:08 -07:00
data={chartData}
height={40}
name={bank!.name}
width={104}
xKey="0"
yKey="1"
2022-10-08 04:37:08 -07:00
/>
) : bank?.name === 'USDC' || bank?.name === 'USDT' ? null : (
<p className="mb-0 text-th-fgd-4">{t('unavailable')}</p>
2022-10-08 04:37:08 -07:00
)
) : (
<div className="h-10 w-[104px] animate-pulse rounded bg-th-bkg-3" />
)}
</div>
</div>
)
}