2022-10-11 12:30:07 +00:00
|
|
|
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
|
|
import throttle from 'lodash/throttle';
|
|
|
|
import {
|
|
|
|
addDecimalsFormatNumber,
|
|
|
|
t,
|
|
|
|
useDataProvider,
|
|
|
|
} from '@vegaprotocol/react-helpers';
|
|
|
|
import type {
|
|
|
|
MarketData,
|
|
|
|
MarketDataUpdateFieldsFragment,
|
|
|
|
SingleMarketFieldsFragment,
|
|
|
|
} from '@vegaprotocol/market-list';
|
|
|
|
import { marketDataProvider, marketProvider } from '@vegaprotocol/market-list';
|
|
|
|
import { HeaderStat } from '../header';
|
|
|
|
import * as constants from '../constants';
|
|
|
|
|
2022-11-18 17:08:48 +00:00
|
|
|
export const MarketMarkPrice = ({ marketId }: { marketId?: string }) => {
|
2022-10-11 12:30:07 +00:00
|
|
|
const [marketPrice, setMarketPrice] = useState<string | null>(null);
|
|
|
|
const variables = useMemo(
|
|
|
|
() => ({
|
|
|
|
marketId: marketId,
|
|
|
|
}),
|
|
|
|
[marketId]
|
|
|
|
);
|
|
|
|
const { data } = useDataProvider<SingleMarketFieldsFragment, never>({
|
|
|
|
dataProvider: marketProvider,
|
|
|
|
variables,
|
|
|
|
skip: !marketId,
|
|
|
|
});
|
|
|
|
const throttledSetMarketPrice = useRef(
|
|
|
|
throttle((price: string) => {
|
|
|
|
setMarketPrice(price);
|
|
|
|
}, constants.DEBOUNCE_UPDATE_TIME)
|
|
|
|
).current;
|
|
|
|
const update = useCallback(
|
2022-11-10 19:08:13 +00:00
|
|
|
({ data: marketData }: { data: MarketData | null }) => {
|
2022-10-11 12:30:07 +00:00
|
|
|
throttledSetMarketPrice(
|
2022-11-10 19:08:13 +00:00
|
|
|
marketData?.markPrice && data?.decimalPlaces
|
2022-10-11 12:30:07 +00:00
|
|
|
? addDecimalsFormatNumber(marketData.markPrice, data.decimalPlaces)
|
|
|
|
: '-'
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[data?.decimalPlaces, throttledSetMarketPrice]
|
|
|
|
);
|
|
|
|
|
|
|
|
useDataProvider<MarketData, MarketDataUpdateFieldsFragment>({
|
|
|
|
dataProvider: marketDataProvider,
|
|
|
|
update,
|
|
|
|
variables,
|
|
|
|
skip: !marketId || !data,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
2022-10-14 08:15:34 +00:00
|
|
|
<HeaderStat heading={t('Price')} testId="market-price">
|
|
|
|
<div>{marketPrice}</div>
|
2022-10-11 12:30:07 +00:00
|
|
|
</HeaderStat>
|
|
|
|
);
|
|
|
|
};
|