@@ -20,6 +20,7 @@ import { Pagination } from "./pagination";
20
20
import { useCatalogue } from "@renderer/hooks/use-catalogue" ;
21
21
import { GameItem } from "./game-item" ;
22
22
import { FilterItem } from "./filter-item" ;
23
+ import { debounce } from "lodash-es" ;
23
24
24
25
const filterCategoryColors = {
25
26
genres : "hsl(262deg 50% 47%)" ,
@@ -58,26 +59,36 @@ export default function Catalogue() {
58
59
59
60
const { getRepacksForObjectId } = useRepacks ( ) ;
60
61
62
+ const debouncedSearch = useRef (
63
+ debounce ( async ( filters , pageSize , offset ) => {
64
+ const abortController = new AbortController ( ) ;
65
+ abortControllerRef . current = abortController ;
66
+
67
+ const response = await window . electron . searchGames (
68
+ filters ,
69
+ pageSize ,
70
+ offset
71
+ ) ;
72
+
73
+ if ( abortController . signal . aborted ) return ;
74
+
75
+ setResults ( response . edges ) ;
76
+ setItemsCount ( response . count ) ;
77
+ setIsLoading ( false ) ;
78
+ } , 500 )
79
+ ) . current ;
80
+
61
81
useEffect ( ( ) => {
62
82
setResults ( [ ] ) ;
63
83
setIsLoading ( true ) ;
64
84
abortControllerRef . current ?. abort ( ) ;
65
85
66
- const abortController = new AbortController ( ) ;
67
- abortControllerRef . current = abortController ;
68
-
69
- window . electron
70
- . searchGames ( filters , PAGE_SIZE , ( page - 1 ) * PAGE_SIZE )
71
- . then ( ( response ) => {
72
- if ( abortController . signal . aborted ) {
73
- return ;
74
- }
75
-
76
- setResults ( response . edges ) ;
77
- setItemsCount ( response . count ) ;
78
- setIsLoading ( false ) ;
79
- } ) ;
80
- } , [ filters , page , dispatch ] ) ;
86
+ debouncedSearch ( filters , PAGE_SIZE , ( page - 1 ) * PAGE_SIZE ) ;
87
+
88
+ return ( ) => {
89
+ debouncedSearch . cancel ( ) ;
90
+ } ;
91
+ } , [ filters , page , debouncedSearch ] ) ;
81
92
82
93
useEffect ( ( ) => {
83
94
downloadSourcesTable . toArray ( ) . then ( ( sources ) => {
0 commit comments