chore(trading): refactor last price change render prop

This commit is contained in:
Madalina Raicu 2024-02-07 14:39:25 +00:00
parent d41e9456bc
commit 2c1a3d106c
No known key found for this signature in database
GPG Key ID: 688B7B31149C1DCD
3 changed files with 8 additions and 16 deletions

View File

@ -54,7 +54,9 @@ export const MarketHeaderStats = ({ market }: MarketHeaderStatsProps) => {
<Last24hPriceChange <Last24hPriceChange
marketId={market.id} marketId={market.id}
decimalPlaces={market.decimalPlaces} decimalPlaces={market.decimalPlaces}
/> >
<span>-</span>
</Last24hPriceChange>
</HeaderStat> </HeaderStat>
<HeaderStat heading={t('Volume (24h)')} testId="market-volume"> <HeaderStat heading={t('Volume (24h)')} testId="market-volume">
<Last24hVolume <Last24hVolume

View File

@ -71,7 +71,6 @@ export const MobileMarketHeader = () => {
<Last24hPriceChange <Last24hPriceChange
marketId={data.id} marketId={data.id}
decimalPlaces={data.decimalPlaces} decimalPlaces={data.decimalPlaces}
hideZero={true}
/> />
</span> </span>
<MarketMarkPrice <MarketMarkPrice

View File

@ -18,15 +18,15 @@ interface Props {
initialValue?: string[]; initialValue?: string[];
isHeader?: boolean; isHeader?: boolean;
noUpdate?: boolean; noUpdate?: boolean;
// to render nothing instead of '-' when there is no price change // render prop for no price change
hideZero?: boolean; children?: React.ReactNode;
} }
export const Last24hPriceChange = ({ export const Last24hPriceChange = ({
marketId, marketId,
decimalPlaces, decimalPlaces,
initialValue, initialValue,
hideZero, children,
}: Props) => { }: Props) => {
const t = useT(); const t = useT();
const { oneDayCandles, error, fiveDaysCandles } = useCandles({ const { oneDayCandles, error, fiveDaysCandles } = useCandles({
@ -37,10 +37,6 @@ export const Last24hPriceChange = ({
fiveDaysCandles.length > 0 && fiveDaysCandles.length > 0 &&
(!oneDayCandles || oneDayCandles?.length === 0) (!oneDayCandles || oneDayCandles?.length === 0)
) { ) {
// render nothing instead of '-' when there is no price change
if (hideZero) {
return null;
}
return ( return (
<Tooltip <Tooltip
description={ description={
@ -55,24 +51,19 @@ export const Last24hPriceChange = ({
</span> </span>
} }
> >
<span>-</span> <span>{children}</span>
</Tooltip> </Tooltip>
); );
} }
if (error || !isNumeric(decimalPlaces)) { if (error || !isNumeric(decimalPlaces)) {
return <span>-</span>; return <span>{children}</span>;
} }
const candles = oneDayCandles?.map((c) => c.close) || initialValue || []; const candles = oneDayCandles?.map((c) => c.close) || initialValue || [];
const change = priceChange(candles); const change = priceChange(candles);
const changePercentage = priceChangePercentage(candles); const changePercentage = priceChangePercentage(candles);
// render nothing instead of '-' when there is no price change
if (!change && !changePercentage && hideZero) {
return null;
}
return ( return (
<span <span
className={classNames( className={classNames(