@@ -13,11 +13,6 @@ interface UseScrollBasedChunksProps {
1313 overscanCount ?: number ;
1414}
1515
16- interface ChunksRange {
17- start : number ;
18- end : number ;
19- }
20-
2116const DEFAULT_OVERSCAN_COUNT = 1 ;
2217const THROTTLE_DELAY = 100 ;
2318
@@ -34,10 +29,10 @@ export const useScrollBasedChunks = ({
3429 [ chunkSize , totalItems ] ,
3530 ) ;
3631
37- const [ chunksRange , setChunksRange ] = React . useState < ChunksRange > ( {
38- start : 0 ,
39- end : Math . min ( overscanCount , chunksCount - 1 ) ,
40- } ) ;
32+ const [ startChunk , setStartChunk ] = React . useState ( 0 ) ;
33+ const [ endChunk , setEndChunk ] = React . useState (
34+ Math . min ( overscanCount , Math . max ( chunksCount - 1 , 0 ) ) ,
35+ ) ;
4136
4237 const calculateVisibleRange = React . useCallback ( ( ) => {
4338 const container = parentRef ?. current ;
@@ -51,50 +46,45 @@ export const useScrollBasedChunks = ({
5146 const visibleStart = Math . max ( containerScroll - tableOffset , 0 ) ;
5247 const visibleEnd = visibleStart + container . clientHeight ;
5348
54- const startChunk = Math . max (
55- Math . floor ( visibleStart / rowHeight / chunkSize ) - overscanCount ,
56- 0 ,
57- ) ;
58- const endChunk = Math . min (
49+ const start = Math . max ( Math . floor ( visibleStart / rowHeight / chunkSize ) - overscanCount , 0 ) ;
50+ const end = Math . min (
5951 Math . floor ( visibleEnd / rowHeight / chunkSize ) + overscanCount ,
60- chunksCount - 1 ,
52+ Math . max ( chunksCount - 1 , 0 ) ,
6153 ) ;
6254
63- return { start : startChunk , end : endChunk } ;
64- } , [ parentRef , tableRef , rowHeight , chunkSize , chunksCount , overscanCount ] ) ;
55+ return { start, end} ;
56+ } , [ parentRef , tableRef , rowHeight , chunkSize , overscanCount , chunksCount ] ) ;
6557
6658 const handleScroll = React . useCallback ( ( ) => {
6759 const newRange = calculateVisibleRange ( ) ;
68- if (
69- newRange &&
70- ( newRange . start !== chunksRange . start || newRange . end !== chunksRange . end )
71- ) {
72- setChunksRange ( newRange ) ;
60+ if ( newRange ) {
61+ setStartChunk ( newRange . start ) ;
62+ setEndChunk ( newRange . end ) ;
7363 }
74- } , [ calculateVisibleRange , chunksRange . end , chunksRange . start ] ) ;
75-
76- const throttledHandleScroll = React . useMemo (
77- ( ) => throttle ( handleScroll , THROTTLE_DELAY , { leading : true , trailing : true } ) ,
78- [ handleScroll ] ,
79- ) ;
64+ } , [ calculateVisibleRange ] ) ;
8065
8166 React . useEffect ( ( ) => {
8267 const container = parentRef ?. current ;
8368 if ( ! container ) {
8469 return undefined ;
8570 }
8671
72+ const throttledHandleScroll = throttle ( handleScroll , THROTTLE_DELAY , {
73+ leading : true ,
74+ trailing : true ,
75+ } ) ;
76+
8777 container . addEventListener ( 'scroll' , throttledHandleScroll ) ;
8878 return ( ) => {
8979 container . removeEventListener ( 'scroll' , throttledHandleScroll ) ;
9080 throttledHandleScroll . cancel ( ) ;
9181 } ;
92- } , [ parentRef , throttledHandleScroll ] ) ;
82+ } , [ handleScroll , parentRef ] ) ;
9383
9484 return React . useMemo ( ( ) => {
9585 const activeChunkIds = Array . from (
96- { length : chunksRange . end - chunksRange . start + 1 } ,
97- ( _ , i ) => chunksRange . start + i ,
86+ { length : endChunk - startChunk + 1 } ,
87+ ( _ , i ) => startChunk + i ,
9888 ) ;
9989
10090 // Create boolean array where true represents active chunks
@@ -104,5 +94,5 @@ export const useScrollBasedChunks = ({
10494 } ) ;
10595
10696 return activeChunks ;
107- } , [ chunksRange . start , chunksRange . end , chunksCount ] ) ;
97+ } , [ endChunk , startChunk , chunksCount ] ) ;
10898} ;
0 commit comments