Skip to content

Commit c8da1a5

Browse files
committed
fix: layout
1 parent 1b08a98 commit c8da1a5

File tree

2 files changed

+45
-79
lines changed

2 files changed

+45
-79
lines changed

src/modules/reserve-overview/graphs/MeritApyGraphContainer.tsx

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,46 @@
11
import { Trans } from '@lingui/macro';
22
import { Box, Button, CircularProgress, Typography } from '@mui/material';
3-
import React, { useEffect, useRef, useState } from 'react';
3+
import { ParentSize } from '@visx/responsive';
4+
import React from 'react';
45

56
import { GraphLegend } from './GraphLegend';
6-
import { MeritApyDataItem, MeritApyGraph, MeritApyPlaceholderChart } from './MeritApyGraph';
7+
import { MeritApyDataItem, MeritApyGraph } from './MeritApyGraph';
78

8-
// Stable chart component that avoids ParentSize recalculations
9-
const StableChart = React.memo(
9+
const ResponsiveChart = React.memo(
1010
({
1111
data,
1212
lineColor,
1313
showAverage,
14+
width,
15+
height,
1416
}: {
1517
data: MeritApyDataItem[];
1618
lineColor: string;
1719
showAverage: boolean;
20+
width: number;
21+
height: number;
1822
}) => {
19-
const containerRef = useRef<HTMLDivElement>(null);
20-
const [chartWidth, setChartWidth] = useState(600); // Default width
21-
const CHART_HEIGHT = 155;
22-
23-
useEffect(() => {
24-
// Calculate width once when component mounts
25-
if (containerRef.current) {
26-
const width = containerRef.current.offsetWidth;
27-
if (width > 0 && width !== chartWidth) {
28-
setChartWidth(width);
29-
}
30-
}
31-
}, []); // Only run once on mount
32-
33-
// Memoize the chart to prevent re-renders when data hasn't actually changed
23+
// Memoize the chart to prevent unnecessary re-renders
3424
const chartContent = React.useMemo(() => {
35-
return data.length > 0 ? (
25+
// Early return if dimensions are too small
26+
if (width < 10) return null;
27+
28+
return (
3629
<MeritApyGraph
37-
width={chartWidth}
38-
height={CHART_HEIGHT}
30+
width={width}
31+
height={height}
3932
data={data}
4033
lineColor={lineColor}
4134
showAverage={showAverage}
4235
/>
43-
) : (
44-
<MeritApyPlaceholderChart height={CHART_HEIGHT} width={chartWidth} />
4536
);
46-
}, [data, chartWidth, lineColor, showAverage]);
37+
}, [data, width, height, lineColor, showAverage]);
4738

4839
return (
4940
<Box
50-
ref={containerRef}
5141
sx={{
5242
width: '100%',
53-
height: CHART_HEIGHT,
43+
height: height,
5444
position: 'relative',
5545
}}
5646
>
@@ -60,7 +50,7 @@ const StableChart = React.memo(
6050
}
6151
);
6252

63-
StableChart.displayName = 'StableChart';
53+
ResponsiveChart.displayName = 'ResponsiveChart';
6454

6555
export type MeritApyGraphContainerProps = {
6656
data?: MeritApyDataItem[];
@@ -70,6 +60,7 @@ export type MeritApyGraphContainerProps = {
7060
lineColor?: string;
7161
showAverage?: boolean;
7262
title?: string;
63+
height?: number;
7364
};
7465

7566
/**
@@ -84,8 +75,8 @@ export const MeritApyGraphContainer = ({
8475
lineColor = '#2EBAC6',
8576
showAverage = true,
8677
title = 'Merit APY',
78+
height = 155,
8779
}: MeritApyGraphContainerProps): JSX.Element => {
88-
const CHART_HEIGHT = 155;
8980
const CHART_HEIGHT_LOADING_FIX = 3;
9081

9182
// Legend data
@@ -94,7 +85,7 @@ export const MeritApyGraphContainer = ({
9485
const graphLoading = (
9586
<Box
9687
sx={{
97-
height: CHART_HEIGHT + CHART_HEIGHT_LOADING_FIX,
88+
height: height + CHART_HEIGHT_LOADING_FIX,
9889
width: 'auto',
9990
display: 'flex',
10091
flexDirection: 'column',
@@ -112,7 +103,7 @@ export const MeritApyGraphContainer = ({
112103
const graphError = (
113104
<Box
114105
sx={{
115-
height: CHART_HEIGHT + CHART_HEIGHT_LOADING_FIX,
106+
height: height + CHART_HEIGHT_LOADING_FIX,
116107
width: 'auto',
117108
display: 'flex',
118109
flexDirection: 'column',
@@ -148,11 +139,21 @@ export const MeritApyGraphContainer = ({
148139
<GraphLegend labels={legendFields} />
149140
</Box>
150141

151-
<Box sx={{ flex: 1, minHeight: CHART_HEIGHT, width: '100%' }}>
142+
<Box sx={{ flex: 1, minHeight: height, width: '100%' }}>
152143
{loading && graphLoading}
153144
{error && graphError}
154145
{!loading && !error && (
155-
<StableChart data={data} lineColor={lineColor} showAverage={showAverage} />
146+
<ParentSize>
147+
{({ width }) => (
148+
<ResponsiveChart
149+
data={data}
150+
lineColor={lineColor}
151+
showAverage={showAverage}
152+
width={width}
153+
height={height}
154+
/>
155+
)}
156+
</ParentSize>
156157
)}
157158
</Box>
158159
</Box>

src/modules/sGho/SGhoDepositPanel.tsx

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
} from '@mui/material';
1717
import { BigNumber } from 'ethers';
1818
import { formatEther, formatUnits } from 'ethers/lib/utils';
19-
import React, { memo } from 'react';
19+
import React from 'react';
2020
import { DarkTooltip } from 'src/components/infoTooltips/DarkTooltip';
2121
import { FormattedNumber } from 'src/components/primitives/FormattedNumber';
2222
import { TokenIcon } from 'src/components/primitives/TokenIcon';
@@ -30,46 +30,9 @@ import { useStakeTokenAPR } from 'src/hooks/useStakeTokenAPR';
3030
import { useWeb3Context } from 'src/libs/hooks/useWeb3Context';
3131
import { GENERAL } from 'src/utils/events';
3232

33-
import { MeritApyDataItem } from '../reserve-overview/graphs/MeritApyGraph';
3433
import { MeritApyGraphContainer } from '../reserve-overview/graphs/MeritApyGraphContainer';
3534
import { StakeActionBox } from '../staking/StakeActionBox';
3635

37-
// Memoized chart component to prevent unnecessary re-renders
38-
const MemoizedMeritApyChart = memo(
39-
({
40-
data,
41-
loading,
42-
error,
43-
onRetry,
44-
}: {
45-
data: MeritApyDataItem[];
46-
loading: boolean;
47-
error: boolean;
48-
onRetry: () => void;
49-
}) => (
50-
<MeritApyGraphContainer
51-
data={data}
52-
loading={loading}
53-
error={error}
54-
title="GHO APR"
55-
lineColor="#2EBAC6"
56-
showAverage={true}
57-
onRetry={onRetry}
58-
/>
59-
),
60-
(prevProps, nextProps) => {
61-
// Custom comparison function to prevent unnecessary re-renders
62-
return (
63-
prevProps.loading === nextProps.loading &&
64-
prevProps.error === nextProps.error &&
65-
prevProps.data === nextProps.data && // Reference equality check
66-
prevProps.onRetry === nextProps.onRetry
67-
);
68-
}
69-
);
70-
71-
MemoizedMeritApyChart.displayName = 'MemoizedMeritApyChart';
72-
7336
export interface SGHODepositPanelProps {
7437
onStakeAction?: () => void;
7538
onStakeRewardClaimAction?: () => void;
@@ -531,14 +494,16 @@ export const SGHODepositPanel: React.FC<SGHODepositPanelProps> = ({
531494
</Box>
532495
</Stack>
533496

534-
<Box>
535-
<MemoizedMeritApyChart
536-
data={meritApyHistory}
537-
loading={loadingMeritApy}
538-
error={errorMeritApyHistory}
539-
onRetry={refetchMeritApyHistory}
540-
/>
541-
</Box>
497+
<MeritApyGraphContainer
498+
data={meritApyHistory}
499+
loading={loadingMeritApy}
500+
error={errorMeritApyHistory}
501+
onRetry={refetchMeritApyHistory}
502+
title="GHO APR"
503+
lineColor="#2EBAC6"
504+
showAverage={true}
505+
height={155}
506+
/>
542507
</Grid>
543508
</Grid>
544509
</>

0 commit comments

Comments
 (0)