1
- import { useState } from "react" ;
1
+ import { useCallback , useEffect , useState } from "react" ;
2
2
import { useSlotsNavigation } from "./useSlotsNavigation" ;
3
3
import {
4
4
numQuickSearchSlots ,
@@ -8,37 +8,49 @@ import {
8
8
slotNavWithoutListWidth ,
9
9
slotSearchPadding ,
10
10
} from "../consts" ;
11
- import { useMount } from "react-use " ;
11
+ import { throttle } from "lodash " ;
12
12
13
13
export function useQuickSearchWidth ( ) {
14
- const [ width , setWidth ] = useState ( window . innerWidth ) ;
15
14
const { showOnlyEpochBar, showNav } = useSlotsNavigation ( ) ;
15
+ const getQuickSearchWidth = useCallback (
16
+ ( width : number ) => {
17
+ const outletWidth = showOnlyEpochBar
18
+ ? width - slotNavWithoutListWidth
19
+ : showNav
20
+ ? width - slotNavWidth
21
+ : width ;
16
22
17
- useMount ( ( ) => {
18
- function handleResize ( ) {
19
- setWidth ( window . innerWidth ) ;
20
- }
21
- window . addEventListener ( "resize" , handleResize ) ;
22
- return ( ) => window . removeEventListener ( "resize" , handleResize ) ;
23
- } ) ;
23
+ const numCardsPerRow = Math . min (
24
+ numQuickSearchSlots ,
25
+ Math . floor (
26
+ ( outletWidth - 2 * slotSearchPadding + quickSearchSpacing ) /
27
+ ( quickSearchCardWidth + quickSearchSpacing ) ,
28
+ ) ,
29
+ ) ;
24
30
25
- const outletWidth = showOnlyEpochBar
26
- ? width - slotNavWithoutListWidth
27
- : showNav
28
- ? width - slotNavWidth
29
- : width ;
31
+ return (
32
+ numCardsPerRow * quickSearchCardWidth +
33
+ ( numCardsPerRow - 1 ) * quickSearchSpacing +
34
+ 2 * slotSearchPadding
35
+ ) ;
36
+ } ,
37
+ [ showNav , showOnlyEpochBar ] ,
38
+ ) ;
30
39
31
- const numCardsPerRow = Math . min (
32
- numQuickSearchSlots ,
33
- Math . floor (
34
- ( outletWidth - 2 * slotSearchPadding + quickSearchSpacing ) /
35
- ( quickSearchCardWidth + quickSearchSpacing ) ,
36
- ) ,
40
+ const [ quickSearchWidth , setQuickSearchWidth ] = useState (
41
+ getQuickSearchWidth ( window . innerWidth ) ,
37
42
) ;
38
- const quickSearchWidth =
39
- numCardsPerRow * quickSearchCardWidth +
40
- ( numCardsPerRow - 1 ) * quickSearchSpacing +
41
- 2 * slotSearchPadding ;
43
+
44
+ useEffect ( ( ) => {
45
+ const handleResize = throttle (
46
+ ( ) => setQuickSearchWidth ( getQuickSearchWidth ( window . innerWidth ) ) ,
47
+ 50 ,
48
+ { leading : false , trailing : true } ,
49
+ ) ;
50
+ window . addEventListener ( "resize" , handleResize ) ;
51
+ handleResize ( ) ;
52
+ return ( ) => window . removeEventListener ( "resize" , handleResize ) ;
53
+ } , [ getQuickSearchWidth ] ) ;
42
54
43
55
return quickSearchWidth ;
44
56
}
0 commit comments