2022-10-11 12:30:07 +00:00
|
|
|
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
|
|
import throttle from 'lodash/throttle';
|
|
|
|
import {
|
|
|
|
addDecimalsFormatNumber,
|
|
|
|
t,
|
2022-11-30 11:02:59 +00:00
|
|
|
PriceCell,
|
2022-10-11 12:30:07 +00:00
|
|
|
useDataProvider,
|
2022-11-30 11:02:59 +00:00
|
|
|
isNumeric,
|
2022-10-11 12:30:07 +00:00
|
|
|
} from '@vegaprotocol/react-helpers';
|
|
|
|
import type {
|
|
|
|
MarketData,
|
|
|
|
MarketDataUpdateFieldsFragment,
|
|
|
|
} from '@vegaprotocol/market-list';
|
2022-11-30 11:02:59 +00:00
|
|
|
import { marketDataProvider } from '@vegaprotocol/market-list';
|
2022-10-11 12:30:07 +00:00
|
|
|
import { HeaderStat } from '../header';
|
|
|
|
import * as constants from '../constants';
|
|
|
|
|
2022-11-30 11:02:59 +00:00
|
|
|
interface Props {
|
|
|
|
marketId?: string;
|
|
|
|
decimalPlaces?: number;
|
|
|
|
isHeader?: boolean;
|
|
|
|
noUpdate?: boolean;
|
|
|
|
initialValue?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MarketMarkPrice = ({
|
|
|
|
marketId,
|
|
|
|
decimalPlaces,
|
|
|
|
initialValue,
|
|
|
|
isHeader = false,
|
|
|
|
noUpdate = false,
|
|
|
|
}: Props) => {
|
|
|
|
const [marketPrice, setMarketPrice] = useState<string | null>(
|
|
|
|
initialValue || null
|
|
|
|
);
|
2022-10-11 12:30:07 +00:00
|
|
|
const variables = useMemo(
|
|
|
|
() => ({
|
|
|
|
marketId: marketId,
|
|
|
|
}),
|
|
|
|
[marketId]
|
|
|
|
);
|
2022-11-30 11:02:59 +00:00
|
|
|
|
2022-10-11 12:30:07 +00:00
|
|
|
const throttledSetMarketPrice = useRef(
|
|
|
|
throttle((price: string) => {
|
2022-11-30 11:02:59 +00:00
|
|
|
noUpdate || setMarketPrice(price);
|
2022-10-11 12:30:07 +00:00
|
|
|
}, constants.DEBOUNCE_UPDATE_TIME)
|
|
|
|
).current;
|
|
|
|
const update = useCallback(
|
2022-11-10 19:08:13 +00:00
|
|
|
({ data: marketData }: { data: MarketData | null }) => {
|
2022-11-30 11:02:59 +00:00
|
|
|
throttledSetMarketPrice(marketData?.markPrice || '');
|
2022-10-11 12:30:07 +00:00
|
|
|
return true;
|
|
|
|
},
|
2022-11-30 11:02:59 +00:00
|
|
|
[throttledSetMarketPrice]
|
2022-10-11 12:30:07 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useDataProvider<MarketData, MarketDataUpdateFieldsFragment>({
|
|
|
|
dataProvider: marketDataProvider,
|
|
|
|
update,
|
|
|
|
variables,
|
2022-11-30 11:02:59 +00:00
|
|
|
skip: noUpdate || !marketId,
|
2022-10-11 12:30:07 +00:00
|
|
|
});
|
|
|
|
|
2022-11-30 11:02:59 +00:00
|
|
|
const content = useMemo(() => {
|
|
|
|
if (!marketPrice || !isNumeric(decimalPlaces)) {
|
|
|
|
return <>-</>;
|
|
|
|
}
|
|
|
|
return isHeader ? (
|
|
|
|
<div>{addDecimalsFormatNumber(marketPrice, decimalPlaces)}</div>
|
|
|
|
) : (
|
|
|
|
<PriceCell
|
|
|
|
value={Number(marketPrice)}
|
|
|
|
valueFormatted={addDecimalsFormatNumber(marketPrice, decimalPlaces, 2)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}, [marketPrice, decimalPlaces, isHeader]);
|
|
|
|
|
|
|
|
return isHeader ? (
|
2022-10-14 08:15:34 +00:00
|
|
|
<HeaderStat heading={t('Price')} testId="market-price">
|
2022-11-30 11:02:59 +00:00
|
|
|
{content}
|
2022-10-11 12:30:07 +00:00
|
|
|
</HeaderStat>
|
2022-11-30 11:02:59 +00:00
|
|
|
) : (
|
|
|
|
content
|
2022-10-11 12:30:07 +00:00
|
|
|
);
|
|
|
|
};
|