2023-01-16 17:51:30 +00:00
|
|
|
import type { RefObject } from 'react';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { useInView } from 'react-intersection-observer';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { addDecimalsFormatNumber, isNumeric } from '@vegaprotocol/utils';
|
|
|
|
import { useThrottledDataProvider } from '@vegaprotocol/react-helpers';
|
|
|
|
import { PriceCell } from '@vegaprotocol/datagrid';
|
2022-10-11 12:30:07 +00:00
|
|
|
import type {
|
|
|
|
MarketData,
|
|
|
|
MarketDataUpdateFieldsFragment,
|
|
|
|
} from '@vegaprotocol/market-list';
|
2022-11-30 11:02:59 +00:00
|
|
|
import { marketDataProvider } from '@vegaprotocol/market-list';
|
2023-01-16 17:51:30 +00:00
|
|
|
import { THROTTLE_UPDATE_TIME } from '../constants';
|
2022-10-11 12:30:07 +00:00
|
|
|
|
2022-11-30 11:02:59 +00:00
|
|
|
interface Props {
|
|
|
|
marketId?: string;
|
|
|
|
decimalPlaces?: number;
|
2023-01-16 17:51:30 +00:00
|
|
|
asPriceCell?: boolean;
|
|
|
|
inViewRoot?: RefObject<Element>;
|
2022-11-30 11:02:59 +00:00
|
|
|
initialValue?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MarketMarkPrice = ({
|
|
|
|
marketId,
|
|
|
|
decimalPlaces,
|
|
|
|
initialValue,
|
2023-01-16 17:51:30 +00:00
|
|
|
inViewRoot,
|
|
|
|
asPriceCell,
|
2022-11-30 11:02:59 +00:00
|
|
|
}: Props) => {
|
2023-01-16 17:51:30 +00:00
|
|
|
const [ref, inView] = useInView({ root: inViewRoot?.current });
|
|
|
|
const variables = useMemo(() => ({ marketId }), [marketId]);
|
2022-11-30 11:02:59 +00:00
|
|
|
|
2023-01-16 17:51:30 +00:00
|
|
|
const { data } = useThrottledDataProvider<
|
|
|
|
MarketData,
|
|
|
|
MarketDataUpdateFieldsFragment
|
|
|
|
>(
|
|
|
|
{
|
|
|
|
dataProvider: marketDataProvider,
|
|
|
|
variables,
|
|
|
|
skip: !inView,
|
2022-10-11 12:30:07 +00:00
|
|
|
},
|
2023-01-16 17:51:30 +00:00
|
|
|
THROTTLE_UPDATE_TIME
|
2022-10-11 12:30:07 +00:00
|
|
|
);
|
|
|
|
|
2023-01-16 17:51:30 +00:00
|
|
|
const marketPrice = data?.markPrice || initialValue;
|
2022-10-11 12:30:07 +00:00
|
|
|
|
2023-01-16 17:51:30 +00:00
|
|
|
if (!marketPrice || !isNumeric(decimalPlaces)) {
|
|
|
|
return <span ref={ref}>-</span>;
|
|
|
|
}
|
|
|
|
if (asPriceCell) {
|
|
|
|
return (
|
2022-11-30 11:02:59 +00:00
|
|
|
<PriceCell
|
2023-01-16 17:51:30 +00:00
|
|
|
ref={ref}
|
2022-11-30 11:02:59 +00:00
|
|
|
value={Number(marketPrice)}
|
|
|
|
valueFormatted={addDecimalsFormatNumber(marketPrice, decimalPlaces, 2)}
|
|
|
|
/>
|
|
|
|
);
|
2023-01-16 17:51:30 +00:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<span ref={ref}>{addDecimalsFormatNumber(marketPrice, decimalPlaces)}</span>
|
2022-10-11 12:30:07 +00:00
|
|
|
);
|
|
|
|
};
|