2022-10-24 12:16:09 +00:00
|
|
|
import {
|
|
|
|
calcCandleVolume,
|
|
|
|
marketCandlesProvider,
|
|
|
|
marketProvider,
|
|
|
|
} from '@vegaprotocol/market-list';
|
|
|
|
import {
|
|
|
|
addDecimalsFormatNumber,
|
|
|
|
t,
|
|
|
|
useDataProvider,
|
|
|
|
useYesterday,
|
|
|
|
} from '@vegaprotocol/react-helpers';
|
2022-11-14 11:37:15 +00:00
|
|
|
import { Schema } from '@vegaprotocol/types';
|
2022-10-24 12:16:09 +00:00
|
|
|
import throttle from 'lodash/throttle';
|
|
|
|
import { useCallback, useMemo, useRef, useState } from 'react';
|
|
|
|
|
|
|
|
import * as constants from '../constants';
|
|
|
|
import { HeaderStat } from '../header';
|
|
|
|
|
|
|
|
import type {
|
|
|
|
SingleMarketFieldsFragment,
|
|
|
|
Candle,
|
|
|
|
} from '@vegaprotocol/market-list';
|
2022-11-18 17:08:48 +00:00
|
|
|
export const Last24hVolume = ({ marketId }: { marketId?: string }) => {
|
2022-10-24 12:16:09 +00:00
|
|
|
const [candleVolume, setCandleVolume] = useState<string>();
|
|
|
|
const yesterday = useYesterday();
|
|
|
|
// Cache timestamp for yesterday to prevent full unmount of market page when
|
|
|
|
// a rerender occurs
|
|
|
|
const yTimestamp = useMemo(() => {
|
|
|
|
return new Date(yesterday).toISOString();
|
|
|
|
}, [yesterday]);
|
|
|
|
|
|
|
|
const marketVariables = useMemo(
|
|
|
|
() => ({
|
|
|
|
marketId: marketId,
|
|
|
|
}),
|
|
|
|
[marketId]
|
|
|
|
);
|
|
|
|
|
|
|
|
const variables = useMemo(
|
|
|
|
() => ({
|
|
|
|
marketId: marketId,
|
2022-11-14 11:37:15 +00:00
|
|
|
interval: Schema.Interval.INTERVAL_I1H,
|
2022-10-24 12:16:09 +00:00
|
|
|
since: yTimestamp,
|
|
|
|
}),
|
|
|
|
[marketId, yTimestamp]
|
|
|
|
);
|
|
|
|
|
|
|
|
const { data, error } = useDataProvider<SingleMarketFieldsFragment, never>({
|
|
|
|
dataProvider: marketProvider,
|
|
|
|
variables: marketVariables,
|
|
|
|
skip: !marketId,
|
|
|
|
});
|
|
|
|
|
|
|
|
const throttledSetCandles = useRef(
|
|
|
|
throttle((data: Candle[]) => {
|
|
|
|
setCandleVolume(calcCandleVolume(data));
|
|
|
|
}, constants.DEBOUNCE_UPDATE_TIME)
|
|
|
|
).current;
|
|
|
|
const update = useCallback(
|
2022-11-10 19:08:13 +00:00
|
|
|
({ data }: { data: Candle[] | null }) => {
|
|
|
|
if (data) {
|
|
|
|
throttledSetCandles(data);
|
|
|
|
}
|
2022-10-24 12:16:09 +00:00
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[throttledSetCandles]
|
|
|
|
);
|
|
|
|
|
|
|
|
useDataProvider<Candle[], Candle>({
|
|
|
|
dataProvider: marketCandlesProvider,
|
|
|
|
update,
|
|
|
|
variables,
|
|
|
|
skip: !marketId || !data,
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<HeaderStat
|
|
|
|
heading={t('Volume (24h)')}
|
|
|
|
testId="market-volume"
|
|
|
|
description={
|
|
|
|
error && candleVolume && data?.positionDecimalPlaces
|
|
|
|
? t('The total amount of assets traded in the last 24 hours.')
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{!error && candleVolume && data?.positionDecimalPlaces
|
|
|
|
? addDecimalsFormatNumber(candleVolume, data.positionDecimalPlaces)
|
|
|
|
: '-'}
|
|
|
|
</HeaderStat>
|
|
|
|
);
|
|
|
|
};
|