22import { Box , Text } from '@lace/ui' ;
33import { SortField } from 'features/BrowsePools/types' ;
44import debounce from 'lodash/debounce' ;
5- import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
5+ import { useCallback , useMemo , useRef , useState } from 'react' ;
66import { useTranslation } from 'react-i18next' ;
77import { useMediaQuery } from 'react-responsive' ;
88import { ListRange } from 'react-virtuoso' ;
9- import useResizeObserver from 'use-resize-observer' ;
9+ import useResizeObserver , { ObservedSize } from 'use-resize-observer' ;
1010import { STAKE_POOL_CARD_HEIGHT , StakePoolCardSkeleton } from '../StakePoolCard' ;
1111import { StakePoolsListRowProps } from '../StakePoolsList/types' ;
1212import { Grid } from './Grid' ;
@@ -36,7 +36,7 @@ export const StakePoolsGrid = ({
3636} : StakePoolsGridProps ) => {
3737 const { t } = useTranslation ( ) ;
3838 const ref = useRef < HTMLDivElement > ( null ) ;
39- const { width : containerWidth } = useResizeObserver < HTMLDivElement > ( { ref } ) ;
39+ const [ containerWidth , setContainerWidth ] = useState < number > ( ) ;
4040 const [ numberOfItemsPerRow , setNumberOfItemsPerRow ] = useState < numOfItemsType > ( ) ;
4141
4242 const matchThreeColumnsLayout = useMediaQuery ( { maxWidth : 1023 } ) ;
@@ -52,7 +52,7 @@ export const StakePoolsGrid = ({
5252 [ matchFiveColumnsLayout , matchFourColumnsLayout , matchThreeColumnsLayout ]
5353 ) ;
5454
55- const getNumberOfItemsInRow = useCallback ( ( ) => {
55+ const updateNumberOfItemsInRow = useCallback ( ( ) => {
5656 if ( ! ref ?. current ) return ;
5757
5858 const result = Number (
@@ -62,14 +62,22 @@ export const StakePoolsGrid = ({
6262 setNumberOfItemsPerRow ( result ) ;
6363 } , [ numberOfItemsPerMediaQueryMap ] ) ;
6464
65- const debouncedGetNumberOfItemsInRow = useMemo (
66- ( ) => debounce ( getNumberOfItemsInRow , DEFAULT_DEBOUNCE ) ,
67- [ getNumberOfItemsInRow ]
65+ const setContainerWidthCb = useCallback (
66+ ( size : ObservedSize ) => {
67+ if ( size . width !== containerWidth ) {
68+ updateNumberOfItemsInRow ( ) ;
69+ setContainerWidth ( size . width ) ;
70+ }
71+ } ,
72+ [ containerWidth , updateNumberOfItemsInRow ]
6873 ) ;
6974
70- useEffect ( ( ) => {
71- debouncedGetNumberOfItemsInRow ( ) ;
72- } , [ containerWidth , debouncedGetNumberOfItemsInRow ] ) ;
75+ const onResize = useMemo (
76+ ( ) => debounce ( setContainerWidthCb , DEFAULT_DEBOUNCE , { leading : true } ) ,
77+ [ setContainerWidthCb ]
78+ ) ;
79+
80+ useResizeObserver < HTMLDivElement > ( { onResize, ref } ) ;
7381
7482 const poolsLength = pools . length ;
7583 const selectedPoolsLength = selectedPools ?. length ;
0 commit comments