11import { Trans } from '@lingui/macro' ;
22import { 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
56import { 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
6555export 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 >
0 commit comments