2023-01-16 17:51:30 +00:00
|
|
|
import type { RefObject } from 'react';
|
|
|
|
import { useInView } from 'react-intersection-observer';
|
2022-10-24 12:16:09 +00:00
|
|
|
import {
|
|
|
|
calcCandleVolume,
|
|
|
|
marketCandlesProvider,
|
|
|
|
} from '@vegaprotocol/market-list';
|
|
|
|
import {
|
|
|
|
addDecimalsFormatNumber,
|
2023-01-16 17:51:30 +00:00
|
|
|
useThrottledDataProvider,
|
2022-10-24 12:16:09 +00:00
|
|
|
useYesterday,
|
2022-11-30 11:02:59 +00:00
|
|
|
isNumeric,
|
2022-10-24 12:16:09 +00:00
|
|
|
} from '@vegaprotocol/react-helpers';
|
2022-12-08 21:13:30 +00:00
|
|
|
import * as Schema from '@vegaprotocol/types';
|
2023-01-16 17:51:30 +00:00
|
|
|
import { useMemo } from 'react';
|
2022-11-30 11:02:59 +00:00
|
|
|
import type { Candle } from '@vegaprotocol/market-list';
|
2023-01-16 17:51:30 +00:00
|
|
|
import { THROTTLE_UPDATE_TIME } from '../constants';
|
2022-10-24 12:16:09 +00:00
|
|
|
|
2022-11-30 11:02:59 +00:00
|
|
|
interface Props {
|
|
|
|
marketId?: string;
|
|
|
|
positionDecimalPlaces?: number;
|
2023-01-16 17:51:30 +00:00
|
|
|
formatDecimals?: number;
|
|
|
|
inViewRoot?: RefObject<Element>;
|
2022-11-30 11:02:59 +00:00
|
|
|
initialValue?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Last24hVolume = ({
|
|
|
|
marketId,
|
|
|
|
positionDecimalPlaces,
|
2023-01-16 17:51:30 +00:00
|
|
|
formatDecimals,
|
|
|
|
inViewRoot,
|
2022-11-30 11:02:59 +00:00
|
|
|
initialValue,
|
|
|
|
}: Props) => {
|
2022-10-24 12:16:09 +00:00
|
|
|
const yesterday = useYesterday();
|
2023-01-16 17:51:30 +00:00
|
|
|
const [ref, inView] = useInView({ root: inViewRoot?.current });
|
2022-10-24 12:16:09 +00:00
|
|
|
|
|
|
|
const variables = useMemo(
|
|
|
|
() => ({
|
|
|
|
marketId: marketId,
|
2022-11-14 11:37:15 +00:00
|
|
|
interval: Schema.Interval.INTERVAL_I1H,
|
2023-01-16 17:51:30 +00:00
|
|
|
since: new Date(yesterday).toISOString(),
|
2022-10-24 12:16:09 +00:00
|
|
|
}),
|
2023-01-16 17:51:30 +00:00
|
|
|
[marketId, yesterday]
|
2022-10-24 12:16:09 +00:00
|
|
|
);
|
|
|
|
|
2023-01-16 17:51:30 +00:00
|
|
|
const { data } = useThrottledDataProvider<Candle[], Candle>(
|
|
|
|
{
|
|
|
|
dataProvider: marketCandlesProvider,
|
|
|
|
variables,
|
|
|
|
skip: !(inView && marketId),
|
2022-10-24 12:16:09 +00:00
|
|
|
},
|
2023-01-16 17:51:30 +00:00
|
|
|
THROTTLE_UPDATE_TIME
|
2022-10-24 12:16:09 +00:00
|
|
|
);
|
2023-01-16 17:51:30 +00:00
|
|
|
const candleVolume = data ? calcCandleVolume(data) : initialValue;
|
|
|
|
return (
|
|
|
|
<span ref={ref}>
|
|
|
|
{candleVolume && isNumeric(positionDecimalPlaces)
|
|
|
|
? addDecimalsFormatNumber(
|
|
|
|
candleVolume,
|
|
|
|
positionDecimalPlaces,
|
|
|
|
formatDecimals
|
|
|
|
)
|
|
|
|
: '-'}
|
|
|
|
</span>
|
2022-10-24 12:16:09 +00:00
|
|
|
);
|
|
|
|
};
|