vega-frontend-monorepo/apps/trading/components/last-24h-volume/last-24h-volume.tsx
m.ray ee3b9a56c9
feat: 24h volume + market links in orders table (#1836)
* feat: #1659 add link to market in orders table

* feat: #1794 use volume 24h instead of indicative volume

* fix: remove total fees that was defined but not used in select columns

* fix: fix cypress test for volume (24h) header

* fix: add volume 24h tooltip

* fix: trading mode and volume order
2022-10-24 13:16:09 +01:00

91 lines
2.3 KiB
TypeScript

import {
calcCandleVolume,
marketCandlesProvider,
marketProvider,
} from '@vegaprotocol/market-list';
import {
addDecimalsFormatNumber,
t,
useDataProvider,
useYesterday,
} from '@vegaprotocol/react-helpers';
import { Interval } from '@vegaprotocol/types';
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';
export const Last24hVolume = ({ marketId }: { marketId: string }) => {
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,
interval: Interval.INTERVAL_I1H,
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(
({ data }: { data: Candle[] }) => {
throttledSetCandles(data);
return true;
},
[throttledSetCandles]
);
useDataProvider<Candle[], Candle>({
dataProvider: marketCandlesProvider,
update,
variables,
skip: !marketId || !data,
updateOnInit: true,
});
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>
);
};