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

214 lines
7.0 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 { useEffect, 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-09 17:48:03 -08:00
import SheenLoader from '@components/shared/SheenLoader'
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')
const actions = mangoStore((s) => s.actions)
2022-10-08 04:37:08 -07:00
const group = mangoStore((s) => s.group)
const serumMarkets = mangoStore((s) => s.serumMarkets)
const serumMarketPrices = mangoStore((s) => s.serumMarketPrices.data)
const loadingSerumMarketPrices = mangoStore(
(s) => s.serumMarketPrices.loading
)
2022-10-08 04:37:08 -07:00
const { theme } = useTheme()
const { width } = useViewport()
const showTableView = width ? width > breakpoints.md : false
useEffect(() => {
actions.fetchSerumMarketPrices()
}, [actions])
2022-10-08 04:37:08 -07:00
return (
<ContentBox hideBorder hidePadding>
{showTableView ? (
2022-10-29 04:38:40 -07:00
<table className="min-w-full">
2022-10-08 04:37:08 -07:00
<thead>
<tr>
<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>
</tr>
</thead>
<tbody>
{serumMarkets.map((market) => {
const bank = group?.getFirstBankByTokenIndex(
market.baseTokenIndex
)
const oraclePrice = bank?.uiPrice
const chartData = serumMarketPrices.find(
(m) =>
m.length &&
m[0].address === market.serumMarketExternal.toString()
2022-10-08 04:37:08 -07:00
)
const change = chartData
? ((chartData[chartData.length - 1].value -
chartData[0].value) /
chartData[0].value) *
2022-10-08 04:37:08 -07:00
100
2022-11-09 17:48:03 -08:00
: 'unavailable'
2022-10-08 04:37:08 -07:00
return (
<tr key={market.publicKey.toString()}>
<td>
<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>
</td>
<td>
<div className="flex flex-col text-right">
<p>{formatFixedDecimals(oraclePrice!, true)}</p>
</div>
</td>
<td>
{!loadingSerumMarketPrices ? (
2022-10-08 04:37:08 -07:00
chartData !== undefined ? (
<SimpleAreaChart
color={
change >= 0
? COLORS.GREEN[theme]
: COLORS.RED[theme]
}
data={chartData}
height={40}
name={bank!.name}
width={104}
xKey="unixTime"
yKey="value"
2022-10-08 04:37:08 -07:00
/>
) : (
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" />
)}
</td>
<td>
<div className="flex flex-col items-end">
2022-11-09 17:48:03 -08:00
{change !== 'unavailable' ? (
loadingSerumMarketPrices ? (
<SheenLoader>
<div className="h-5 w-12 rounded bg-th-bkg-2" />
</SheenLoader>
) : (
<Change change={change} />
)
) : (
<p className="text-th-fgd-4">{t('unavailable')}</p>
)}
2022-10-08 04:37:08 -07:00
</div>
</td>
</tr>
)
})}
</tbody>
</table>
) : (
<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')
const serumMarketPrices = mangoStore((s) => s.serumMarketPrices.data)
const loadingSerumMarketPrices = mangoStore(
(s) => s.serumMarketPrices.loading
)
2022-10-08 04:37:08 -07:00
const group = mangoStore((s) => s.group)
const { theme } = useTheme()
const bank = group?.getFirstBankByTokenIndex(market.baseTokenIndex)
const chartData = useMemo(() => {
if (!loadingSerumMarketPrices) {
return serumMarketPrices.find(
(m) =>
m.length && m[0].address === market.serumMarketExternal.toString()
2022-10-08 04:37:08 -07:00
)
}
return null
}, [loadingSerumMarketPrices])
2022-10-08 04:37:08 -07:00
const change = useMemo(() => {
if (chartData) {
2022-10-08 04:37:08 -07:00
return (
((chartData[chartData.length - 1].value - chartData[0].value) /
chartData[0].value) *
2022-10-08 04:37:08 -07:00
100
)
}
return 0
}, [chartData])
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">
{formatFixedDecimals(bank?.uiPrice!, true)}
</p>
{change ? (
<Change change={change} />
) : (
<p className="text-th-fgd-4">{t('unavailable')}</p>
)}
2022-10-08 04:37:08 -07:00
</div>
</div>
</div>
{!loadingSerumMarketPrices ? (
chartData ? (
2022-10-08 04:37:08 -07:00
<SimpleAreaChart
color={change >= 0 ? COLORS.GREEN[theme] : COLORS.RED[theme]}
data={chartData}
height={40}
name={bank!.name}
width={104}
xKey="unixTime"
yKey="value"
2022-10-08 04:37:08 -07:00
/>
) : (
<p className="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>
)
}