mango-ui-v3/components/MarketHeader.tsx

183 lines
5.6 KiB
TypeScript
Raw Normal View History

import React, { useCallback, useMemo, useState } from 'react'
import styled from '@emotion/styled'
2021-04-19 06:45:59 -07:00
import useMangoStore from '../stores/useMangoStore'
import usePrevious from '../hooks/usePrevious'
import useInterval from '../hooks/useInterval'
import ChartApi from '../utils/chartDataConnector'
2021-04-19 06:45:59 -07:00
import UiLock from './UiLock'
import ManualRefresh from './ManualRefresh'
2021-07-05 08:03:57 -07:00
import useOraclePrice from '../hooks/useOraclePrice'
import DayHighLow from './DayHighLow'
export const StyledMarketInfoLabel = styled.div`
font-size: 0.7rem;
`
2021-04-19 06:45:59 -07:00
const MarketHeader = () => {
2021-07-05 08:03:57 -07:00
const oraclePrice = useOraclePrice()
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
2021-06-23 08:32:33 -07:00
const baseSymbol = marketConfig.baseSymbol
const selectedMarketName = marketConfig.name
const previousMarketName: string = usePrevious(selectedMarketName)
2021-06-23 08:32:33 -07:00
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()
2021-05-20 04:21:36 -07:00
const utcFrom = new Date(
2021-06-05 11:40:07 -07:00
Date.UTC(
date.getFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
0,
0,
0
)
)
2021-05-20 04:21:36 -07:00
const utcTo = new Date(
2021-06-05 11:40:07 -07:00
Date.UTC(
date.getFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
23,
59,
59
)
)
2021-06-05 11:40:07 -07:00
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])
2021-04-19 06:45:59 -07:00
return (
<div
2021-07-19 20:04:26 -07:00
className={`flex items-end sm:items-center justify-between pt-4 px-6 md:px-6`}
>
<div className="flex flex-col sm:flex-row sm:items-center">
<div className="pb-3 sm:pb-0 pr-8">
<div className="flex items-center">
<img
alt=""
width="24"
height="24"
src={`/assets/icons/${baseSymbol.toLowerCase()}.svg`}
className={`mr-2.5`}
/>
2021-06-23 06:16:37 -07:00
<div className="font-semibold pr-0.5 text-xl">{baseSymbol}</div>
<span className="text-th-fgd-4 text-xl">
{selectedMarketName.includes('PERP') ? '' : '/'}
</span>
<div className="font-semibold pl-0.5 text-xl">
{selectedMarketName.split(/\/|-/)[1]}
</div>
</div>
</div>
<div className="flex items-center">
<div className="pr-6">
<StyledMarketInfoLabel className="text-th-fgd-3">
Oracle price
</StyledMarketInfoLabel>
<div className="font-semibold text-th-fgd-1 text-xs">
2021-07-05 08:03:57 -07:00
{oraclePrice ? oraclePrice.toFixed(2) : '--'}
</div>
</div>
<div className="pr-4">
<StyledMarketInfoLabel className="text-th-fgd-3">
24h Change
</StyledMarketInfoLabel>
{ohlcv && !loading ? (
<div
className={`font-semibold text-xs ${
change > 0
? `text-th-green`
: change < 0
? `text-th-red`
: `text-th-fgd-1`
}`}
>
{change > 0 && <span className={`text-th-green`}>+</span>}
{change !== '--' ? `${change.toFixed(2)}%` : change}
</div>
) : (
<MarketDataLoader />
)}
</div>
<div className="pr-6">
<StyledMarketInfoLabel className="text-th-fgd-3">
24h Vol
</StyledMarketInfoLabel>
<div className="font-semibold text-th-fgd-1 text-xs">
2021-06-17 17:32:14 -07:00
{ohlcv && !loading && volume ? (
volume !== '--' ? (
<>
{volume.toFixed(2)}
<span className="ml-1 text-th-fgd-3">{baseSymbol}</span>
</>
) : (
volume
)
) : (
<MarketDataLoader />
)}
</div>
</div>
<DayHighLow />
{selectedMarketName.includes('PERP') ? (
<>
<div className="pr-6">
<StyledMarketInfoLabel className="text-th-fgd-3">
Funding (8h Ave)
</StyledMarketInfoLabel>
<div className="font-semibold text-th-fgd-1 text-xs">
0.001%
</div>
</div>
<div className="pr-6">
<StyledMarketInfoLabel className="text-th-fgd-3">
Open Interest
</StyledMarketInfoLabel>
<div className="font-semibold text-th-fgd-1 text-xs">$XXXm</div>
</div>
</>
) : null}
</div>
2021-04-19 06:45:59 -07:00
</div>
<div className="flex">
2021-04-19 06:45:59 -07:00
<UiLock />
2021-06-23 08:32:33 -07:00
{connected && mangoAccount ? <ManualRefresh className="pl-2" /> : null}
2021-04-19 06:45:59 -07:00
</div>
</div>
)
}
export default MarketHeader
const MarketDataLoader = () => (
<div className="animate-pulse bg-th-bkg-3 h-3.5 mt-0.5 w-10 rounded-sm" />
2021-04-19 06:45:59 -07:00
)