mango-v4-ui/components/trade/AdvancedMarketHeader.tsx

90 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-11-30 07:46:20 -08:00
import { PerpMarket } from '@blockworks-foundation/mango-v4'
2023-01-18 18:42:29 -08:00
import { IconButton } from '@components/shared/Button'
2022-10-05 22:11:28 -07:00
import Change from '@components/shared/Change'
2023-01-18 18:42:29 -08:00
import { ChartBarIcon } from '@heroicons/react/20/solid'
2022-11-18 11:11:06 -08:00
import { useCoingecko } from 'hooks/useCoingecko'
2022-11-20 12:20:27 -08:00
import useSelectedMarket from 'hooks/useSelectedMarket'
2022-09-13 23:24:26 -07:00
import { useTranslation } from 'next-i18next'
2022-11-30 07:46:20 -08:00
import { useMemo } from 'react'
2023-01-14 21:01:30 -08:00
import { getDecimalCount } from 'utils/numbers'
2022-11-30 07:46:20 -08:00
import MarketSelectDropdown from './MarketSelectDropdown'
import PerpFundingRate from './PerpFundingRate'
2022-09-13 23:24:26 -07:00
2023-01-18 18:42:29 -08:00
const AdvancedMarketHeader = ({
showChart,
setShowChart,
}: {
showChart?: boolean
setShowChart?: (x: boolean) => void
}) => {
2022-10-03 03:38:05 -07:00
const { t } = useTranslation(['common', 'trade'])
2023-01-14 21:01:30 -08:00
const { serumOrPerpMarket, baseSymbol, price } = useSelectedMarket()
2022-11-18 11:11:06 -08:00
const { data: tokenPrices } = useCoingecko()
2022-11-09 17:48:03 -08:00
2023-01-14 21:01:30 -08:00
const coingeckoData = useMemo(() => {
2023-01-22 18:20:44 -08:00
return tokenPrices.find(
(asset) => asset.symbol.toUpperCase() === baseSymbol?.toUpperCase()
2023-01-14 21:01:30 -08:00
)
}, [baseSymbol, tokenPrices])
2022-09-13 23:24:26 -07:00
2023-01-14 21:01:30 -08:00
const change = useMemo(() => {
return coingeckoData
? ((coingeckoData.prices[coingeckoData.prices.length - 1][1] -
coingeckoData.prices[0][1]) /
coingeckoData.prices[0][1]) *
100
: 0
}, [coingeckoData])
2022-09-13 23:24:26 -07:00
return (
2022-12-06 21:25:37 -08:00
<div className="flex flex-col bg-th-bkg-1 md:h-12 md:flex-row md:items-center">
<div className=" w-full px-5 md:w-auto md:py-0 md:pr-6 lg:pb-0">
<MarketSelectDropdown />
2022-09-13 23:24:26 -07:00
</div>
2023-01-18 18:42:29 -08:00
<div className="border-t border-th-bkg-3 py-2 px-5 md:border-t-0 md:py-0 md:px-0">
<div className="flex items-center justify-between">
<div className="flex items-center">
<div id="trade-step-two" className="flex-col md:ml-6">
<div className="text-xs text-th-fgd-4">
{t('trade:oracle-price')}
</div>
<div className="font-mono text-xs text-th-fgd-2">
{price ? (
`$${price.toFixed(
getDecimalCount(serumOrPerpMarket?.tickSize || 0.01)
)}`
) : (
<span className="text-th-fgd-4"></span>
)}
</div>
2022-12-06 21:25:37 -08:00
</div>
2023-01-18 18:42:29 -08:00
<div className="ml-6 flex-col">
<div className="text-xs text-th-fgd-4">{t('rolling-change')}</div>
<Change change={change} size="small" suffix="%" />
</div>
{serumOrPerpMarket instanceof PerpMarket ? (
<div className="ml-6 flex-col">
<div className="text-xs text-th-fgd-4">
{t('trade:funding-rate')}
</div>
<PerpFundingRate />
</div>
) : null}
2022-12-06 21:25:37 -08:00
</div>
2023-01-18 18:42:29 -08:00
{setShowChart ? (
<IconButton
className={showChart ? 'text-th-active' : 'text-th-fgd-2'}
onClick={() => setShowChart(!showChart)}
hideBg
>
<ChartBarIcon className="h-5 w-5" />
</IconButton>
) : null}
</div>
2022-12-06 21:25:37 -08:00
</div>
2022-09-13 23:24:26 -07:00
</div>
)
}
export default AdvancedMarketHeader