1- import { DataZoomComponentOption } from 'echarts'
21import ReactECharts , { type EChartsOption } from 'echarts-for-react'
3- import { useMemo , useRef , useEffect , useState , useCallback } from 'react'
2+ import { useMemo , useRef , useCallback , useState , useEffect } from 'react'
43import { createRoot } from 'react-dom/client'
54import Spinner , { SpinnerWrapper } from 'ui/src/Spinner'
5+ import { ChartDataPoint , BandsBalancesData , BandsChartPalette } from '@/llamalend/features/bands-chart/types'
66import { Token } from '@/llamalend/features/borrow/types'
7- import { ChartDataPoint , BandsBalancesData , BandsChartPalette } from '@/llamalend/widgets/bands-chart/types'
87import { Box , Stack , useTheme , ThemeProvider } from '@mui/material'
98import { DesignSystem } from '@ui-kit/themes/design'
109import { getChartOptions } from './chartOptions'
@@ -23,12 +22,6 @@ type BandsChartProps = {
2322 height ?: number
2423}
2524
26- type DataZoomEvent = {
27- start ?: number
28- end ?: number
29- batch ?: { start ?: number ; end ?: number } [ ]
30- }
31-
3225export const BandsChart = ( {
3326 collateralToken,
3427 borrowToken,
@@ -147,76 +140,17 @@ export const BandsChart = ({
147140 ] ,
148141 )
149142
150- // Persist dataZoom (start/end) across renders
151- const [ zoom , setZoom ] = useState < { start ?: number ; end ?: number } > ( { } )
152- const initializedZoomRef = useRef ( false )
153- const lastZoomRef = useRef < { start ?: number ; end ?: number } > ( { } )
154- const chartRef = useRef < ReactECharts | null > ( null )
143+ const [ initialZoom , setInitialZoom ] = useState < { start ?: number ; end ?: number } > ( { } )
155144
156145 useEffect ( ( ) => {
157- if ( ! initializedZoomRef . current && chartData . length > 0 && initialZoomIndices ) {
146+ if ( chartData . length > 0 && initialZoomIndices ) {
158147 const start = ( initialZoomIndices . startIndex / chartData . length ) * 100
159148 const end = ( ( initialZoomIndices . endIndex + 1 ) / chartData . length ) * 100
160- setZoom ( { start, end } )
161- lastZoomRef . current = { start, end }
162- initializedZoomRef . current = true
149+ setInitialZoom ( { start, end } )
163150 }
164151 } , [ chartData . length , initialZoomIndices ] )
165152
166- useEffect ( ( ) => {
167- if ( chartData . length === 0 || ! initialZoomIndices ) return
168-
169- const isFullRange =
170- lastZoomRef . current . start !== undefined &&
171- lastZoomRef . current . end !== undefined &&
172- lastZoomRef . current . start <= 0.0001 &&
173- lastZoomRef . current . end >= 99.999
174-
175- if ( Object . keys ( lastZoomRef . current ) . length === 0 || isFullRange ) {
176- const start = ( initialZoomIndices . startIndex / chartData . length ) * 100
177- const end = ( ( initialZoomIndices . endIndex + 1 ) / chartData . length ) * 100
178-
179- if ( lastZoomRef . current . start !== start || lastZoomRef . current . end !== end ) {
180- setZoom ( { start, end } )
181- lastZoomRef . current = { start, end }
182- }
183- }
184- } , [ chartData . length , userBandsBalances . length , oraclePrice , initialZoomIndices ] )
185-
186- const onEvents = useMemo (
187- ( ) => ( {
188- dataZoom : ( evt : DataZoomEvent ) => {
189- const payload = Array . isArray ( evt ?. batch ) && evt . batch . length ? evt . batch [ 0 ] : evt
190- const s = payload ?. start
191- const e = payload ?. end
192- if ( typeof s === 'number' && typeof e === 'number' ) {
193- const prev = lastZoomRef . current
194- if ( prev . start !== s || prev . end !== e ) {
195- lastZoomRef . current = { start : s , end : e }
196- setZoom ( { start : s , end : e } )
197- }
198- }
199- } ,
200- } ) ,
201- [ ] ,
202- )
203-
204- const optionWithZoom : EChartsOption = useMemo ( ( ) => {
205- if ( option . dataZoom && Array . isArray ( option . dataZoom ) && zoom ) {
206- const dz = option . dataZoom . map ( ( z : DataZoomComponentOption ) => {
207- if ( z && 'type' in z && z . type === 'slider' ) {
208- return {
209- ...z ,
210- ...( zoom . start != null ? { start : zoom . start } : { } ) ,
211- ...( zoom . end != null ? { end : zoom . end } : { } ) ,
212- }
213- }
214- return z
215- } )
216- return { ...option , dataZoom : dz }
217- }
218- return option
219- } , [ option , zoom ] )
153+ const chartRef = useRef < ReactECharts | null > ( null )
220154
221155 if ( chartData . length === 0 ) {
222156 return (
@@ -244,10 +178,21 @@ export const BandsChart = ({
244178 < Box sx = { { width : '99%' , fontVariantNumeric : 'tabular-nums' , height } } >
245179 < ReactECharts
246180 ref = { chartRef }
247- option = { optionWithZoom }
181+ option = { {
182+ ...option ,
183+ dataZoom : option . dataZoom ?. map ( ( z : any ) => {
184+ if ( z && 'type' in z && z . type === 'slider' ) {
185+ return {
186+ ...z ,
187+ ...( initialZoom . start != null ? { start : initialZoom . start } : { } ) ,
188+ ...( initialZoom . end != null ? { end : initialZoom . end } : { } ) ,
189+ }
190+ }
191+ return z
192+ } ) ,
193+ } }
248194 style = { { width : '100%' , height : '100%' } }
249- opts = { { renderer : 'svg' } }
250- onEvents = { onEvents }
195+ opts = { { renderer : 'canvas' } }
251196 lazyUpdate
252197 />
253198 </ Box >
0 commit comments