2
2
import { Box , Text } from '@lace/ui' ;
3
3
import { SortField } from 'features/BrowsePools/types' ;
4
4
import debounce from 'lodash/debounce' ;
5
- import { useCallback , useEffect , useMemo , useRef , useState } from 'react' ;
5
+ import { useCallback , useMemo , useRef , useState } from 'react' ;
6
6
import { useTranslation } from 'react-i18next' ;
7
7
import { useMediaQuery } from 'react-responsive' ;
8
8
import { ListRange } from 'react-virtuoso' ;
9
- import useResizeObserver from 'use-resize-observer' ;
9
+ import useResizeObserver , { ObservedSize } from 'use-resize-observer' ;
10
10
import { STAKE_POOL_CARD_HEIGHT , StakePoolCardSkeleton } from '../StakePoolCard' ;
11
11
import { StakePoolsListRowProps } from '../StakePoolsList/types' ;
12
12
import { Grid } from './Grid' ;
@@ -36,7 +36,7 @@ export const StakePoolsGrid = ({
36
36
} : StakePoolsGridProps ) => {
37
37
const { t } = useTranslation ( ) ;
38
38
const ref = useRef < HTMLDivElement > ( null ) ;
39
- const { width : containerWidth } = useResizeObserver < HTMLDivElement > ( { ref } ) ;
39
+ const [ containerWidth , setContainerWidth ] = useState < number > ( ) ;
40
40
const [ numberOfItemsPerRow , setNumberOfItemsPerRow ] = useState < numOfItemsType > ( ) ;
41
41
42
42
const matchThreeColumnsLayout = useMediaQuery ( { maxWidth : 1023 } ) ;
@@ -52,7 +52,7 @@ export const StakePoolsGrid = ({
52
52
[ matchFiveColumnsLayout , matchFourColumnsLayout , matchThreeColumnsLayout ]
53
53
) ;
54
54
55
- const getNumberOfItemsInRow = useCallback ( ( ) => {
55
+ const updateNumberOfItemsInRow = useCallback ( ( ) => {
56
56
if ( ! ref ?. current ) return ;
57
57
58
58
const result = Number (
@@ -62,14 +62,22 @@ export const StakePoolsGrid = ({
62
62
setNumberOfItemsPerRow ( result ) ;
63
63
} , [ numberOfItemsPerMediaQueryMap ] ) ;
64
64
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 ]
68
73
) ;
69
74
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 } ) ;
73
81
74
82
const poolsLength = pools . length ;
75
83
const selectedPoolsLength = selectedPools ?. length ;
0 commit comments