import React, { useCallback, useMemo, useState } from 'react' import useMangoStore from '../stores/useMangoStore' import usePrevious from '../hooks/usePrevious' import useInterval from '../hooks/useInterval' import ChartApi from '../utils/chartDataConnector' import UiLock from './UiLock' import ManualRefresh from './ManualRefresh' import useOraclePrice from '../hooks/useOraclePrice' import DayHighLow from './DayHighLow' const MarketHeader = () => { const oraclePrice = useOraclePrice() const marketConfig = useMangoStore((s) => s.selectedMarket.config) const baseSymbol = marketConfig.baseSymbol const selectedMarketName = marketConfig.name const previousMarketName: string = usePrevious(selectedMarketName) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const connected = useMangoStore((s) => s.wallet.connected) const [ohlcv, setOhlcv] = useState(null) const [loading, setLoading] = useState(false) const change = ohlcv ? ((ohlcv.c[0] - ohlcv.o[0]) / ohlcv.o[0]) * 100 : '--' const volume = ohlcv ? ohlcv.v[0] : '--' const fetchOhlcv = useCallback(async () => { if (!selectedMarketName) return // calculate from and to date (0:00UTC to 23:59:59UTC) const date = new Date() const utcFrom = new Date( Date.UTC( date.getFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0 ) ) const utcTo = new Date( Date.UTC( date.getFullYear(), date.getUTCMonth(), date.getUTCDate(), 23, 59, 59 ) ) const from = utcFrom.getTime() / 1000 const to = utcTo.getTime() / 1000 const ohlcv = await ChartApi.getOhlcv(selectedMarketName, '1D', from, to) if (ohlcv) { setOhlcv(ohlcv) setLoading(false) } }, [selectedMarketName]) useInterval(async () => { fetchOhlcv() }, 5000) useMemo(() => { if (previousMarketName !== selectedMarketName) { setLoading(true) } }, [selectedMarketName]) return (
{baseSymbol}
{selectedMarketName.includes('PERP') ? '–' : '/'}
{selectedMarketName.split(/\/|-/)[1]}
Oracle price
{oraclePrice ? oraclePrice.toFixed(2) : '--'}
24h Change
{ohlcv && !loading ? (
0 ? `text-th-green` : change < 0 ? `text-th-red` : `text-th-fgd-1` }`} > {change > 0 && +} {change !== '--' ? `${change.toFixed(2)}%` : change}
) : ( )}
24h Vol
{ohlcv && !loading && volume ? ( volume !== '--' ? ( <> {volume.toFixed(2)} {baseSymbol} ) : ( volume ) ) : ( )}
{selectedMarketName.includes('PERP') ? ( <>
Funding (8h Ave)
0.001%
Open Interest
$XXXm
) : null}
{connected && mangoAccount ? : null}
) } export default MarketHeader const MarketDataLoader = () => (
)