feat: search updates

This commit is contained in:
Zamitto 2024-09-12 12:43:09 -03:00
parent b8c8e534b4
commit 5c363810c8

View file

@ -6,7 +6,7 @@ import type { CatalogueEntry } from "@types";
import type { DebouncedFunc } from "lodash"; import type { DebouncedFunc } from "lodash";
import { debounce } from "lodash"; import { debounce } from "lodash";
import { InboxIcon } from "@primer/octicons-react"; import { InboxIcon, SearchIcon } from "@primer/octicons-react";
import { clearSearch } from "@renderer/features"; import { clearSearch } from "@renderer/features";
import { useAppDispatch } from "@renderer/hooks"; import { useAppDispatch } from "@renderer/hooks";
import { useEffect, useRef, useState } from "react"; import { useEffect, useRef, useState } from "react";
@ -25,6 +25,7 @@ export function SearchResults() {
const [searchResults, setSearchResults] = useState<CatalogueEntry[]>([]); const [searchResults, setSearchResults] = useState<CatalogueEntry[]>([]);
const [isLoading, setIsLoading] = useState(false); const [isLoading, setIsLoading] = useState(false);
const [showMessage, setShowMessage] = useState(false);
const debouncedFunc = useRef<DebouncedFunc<() => void> | null>(null); const debouncedFunc = useRef<DebouncedFunc<() => void> | null>(null);
@ -35,19 +36,40 @@ export function SearchResults() {
navigate(buildGameDetailsPath(game)); navigate(buildGameDetailsPath(game));
}; };
const fetchGames = (query: string) => {
window.electron
.searchGames(query)
.then((results) => {
console.log(
"original query: ",
query,
"current value: ",
searchParams.get("query")
);
if (query != searchParams.get("query")) return;
setSearchResults(results);
})
.finally(() => {
setIsLoading(false);
});
};
useEffect(() => { useEffect(() => {
setIsLoading(true); setIsLoading(true);
if (debouncedFunc.current) debouncedFunc.current.cancel(); if (debouncedFunc.current) debouncedFunc.current.cancel();
debouncedFunc.current = debounce(() => { debouncedFunc.current = debounce(() => {
window.electron const query = searchParams.get("query") ?? "";
.searchGames(searchParams.get("query") ?? "")
.then((results) => { if (query.length < 3) {
setSearchResults(results); setIsLoading(false);
}) setShowMessage(true);
.finally(() => { setSearchResults([]);
setIsLoading(false); return;
}); }
setShowMessage(false);
fetchGames(query);
}, 500); }, 500);
debouncedFunc.current(); debouncedFunc.current();
@ -75,6 +97,14 @@ export function SearchResults() {
)} )}
</section> </section>
{!isLoading && showMessage && (
<div className={styles.noResults}>
<SearchIcon size={56} />
<p>{"Comece a digitar para pesquisar…"}</p>
</div>
)}
{!isLoading && searchResults.length === 0 && ( {!isLoading && searchResults.length === 0 && (
<div className={styles.noResults}> <div className={styles.noResults}>
<InboxIcon size={56} /> <InboxIcon size={56} />