2023-01-16 17:51:30 +00:00
|
|
|
import type { RefObject } from 'react';
|
|
|
|
import { useInView } from 'react-intersection-observer';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { isNumeric } from '@vegaprotocol/utils';
|
2022-11-30 11:02:59 +00:00
|
|
|
import {
|
2023-01-16 17:51:30 +00:00
|
|
|
useThrottledDataProvider,
|
2022-11-30 11:02:59 +00:00
|
|
|
useYesterday,
|
|
|
|
} from '@vegaprotocol/react-helpers';
|
2023-02-28 18:56:29 +00:00
|
|
|
import { PriceChangeCell } from '@vegaprotocol/datagrid';
|
2022-12-08 21:13:30 +00:00
|
|
|
import * as Schema from '@vegaprotocol/types';
|
2022-10-11 12:30:07 +00:00
|
|
|
import type { CandleClose } from '@vegaprotocol/types';
|
2022-11-30 11:02:59 +00:00
|
|
|
import { marketCandlesProvider } 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;
|
|
|
|
initialValue?: string[];
|
|
|
|
isHeader?: boolean;
|
|
|
|
noUpdate?: boolean;
|
2023-01-16 17:51:30 +00:00
|
|
|
inViewRoot?: RefObject<Element>;
|
2022-11-30 11:02:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const Last24hPriceChange = ({
|
|
|
|
marketId,
|
|
|
|
decimalPlaces,
|
|
|
|
initialValue,
|
2023-01-16 17:51:30 +00:00
|
|
|
inViewRoot,
|
2022-11-30 11:02:59 +00:00
|
|
|
}: Props) => {
|
2023-01-16 17:51:30 +00:00
|
|
|
const [ref, inView] = useInView({ root: inViewRoot?.current });
|
2022-10-11 12:30:07 +00:00
|
|
|
const yesterday = useYesterday();
|
2023-03-09 10:03:50 +00:00
|
|
|
const { data, error } = useThrottledDataProvider(
|
2023-01-16 17:51:30 +00:00
|
|
|
{
|
|
|
|
dataProvider: marketCandlesProvider,
|
2023-03-09 10:03:50 +00:00
|
|
|
variables: {
|
|
|
|
marketId: marketId || '',
|
|
|
|
interval: Schema.Interval.INTERVAL_I1H,
|
|
|
|
since: new Date(yesterday).toISOString(),
|
|
|
|
},
|
2023-01-16 17:51:30 +00:00
|
|
|
skip: !marketId || !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 candles =
|
|
|
|
data
|
|
|
|
?.map((candle) => candle?.close)
|
|
|
|
.filter((c): c is CandleClose => c !== null) || initialValue;
|
2022-11-30 11:02:59 +00:00
|
|
|
|
2023-01-16 17:51:30 +00:00
|
|
|
if (error || !isNumeric(decimalPlaces)) {
|
|
|
|
return <span ref={ref}>-</span>;
|
|
|
|
}
|
|
|
|
return (
|
2023-02-28 18:56:29 +00:00
|
|
|
<PriceChangeCell
|
2023-01-16 17:51:30 +00:00
|
|
|
candles={candles || []}
|
|
|
|
decimalPlaces={decimalPlaces}
|
|
|
|
ref={ref}
|
|
|
|
/>
|
2022-10-11 12:30:07 +00:00
|
|
|
);
|
|
|
|
};
|