fix: adding debounce to search

This commit is contained in:
Chubby Granny Chaser 2024-12-24 03:35:29 +00:00
parent 54c6b1f1af
commit f67395c94c
No known key found for this signature in database

View File

@ -20,6 +20,7 @@ import { Pagination } from "./pagination";
import { useCatalogue } from "@renderer/hooks/use-catalogue"; import { useCatalogue } from "@renderer/hooks/use-catalogue";
import { GameItem } from "./game-item"; import { GameItem } from "./game-item";
import { FilterItem } from "./filter-item"; import { FilterItem } from "./filter-item";
import { debounce } from "lodash-es";
const filterCategoryColors = { const filterCategoryColors = {
genres: "hsl(262deg 50% 47%)", genres: "hsl(262deg 50% 47%)",
@ -58,26 +59,36 @@ export default function Catalogue() {
const { getRepacksForObjectId } = useRepacks(); const { getRepacksForObjectId } = useRepacks();
const debouncedSearch = useRef(
debounce(async (filters, pageSize, offset) => {
const abortController = new AbortController();
abortControllerRef.current = abortController;
const response = await window.electron.searchGames(
filters,
pageSize,
offset
);
if (abortController.signal.aborted) return;
setResults(response.edges);
setItemsCount(response.count);
setIsLoading(false);
}, 500)
).current;
useEffect(() => { useEffect(() => {
setResults([]); setResults([]);
setIsLoading(true); setIsLoading(true);
abortControllerRef.current?.abort(); abortControllerRef.current?.abort();
const abortController = new AbortController(); debouncedSearch(filters, PAGE_SIZE, (page - 1) * PAGE_SIZE);
abortControllerRef.current = abortController;
window.electron return () => {
.searchGames(filters, PAGE_SIZE, (page - 1) * PAGE_SIZE) debouncedSearch.cancel();
.then((response) => { };
if (abortController.signal.aborted) { }, [filters, page, debouncedSearch]);
return;
}
setResults(response.edges);
setItemsCount(response.count);
setIsLoading(false);
});
}, [filters, page, dispatch]);
useEffect(() => { useEffect(() => {
downloadSourcesTable.toArray().then((sources) => { downloadSourcesTable.toArray().then((sources) => {