From 5c363810c8ccdbf2db442104dfd32929e41c021e Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Thu, 12 Sep 2024 12:43:09 -0300 Subject: [PATCH] feat: search updates --- .../src/pages/home/search-results.tsx | 48 +++++++++++++++---- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/src/renderer/src/pages/home/search-results.tsx b/src/renderer/src/pages/home/search-results.tsx index cc7a2a97..aedab79b 100644 --- a/src/renderer/src/pages/home/search-results.tsx +++ b/src/renderer/src/pages/home/search-results.tsx @@ -6,7 +6,7 @@ import type { CatalogueEntry } from "@types"; import type { DebouncedFunc } 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 { useAppDispatch } from "@renderer/hooks"; import { useEffect, useRef, useState } from "react"; @@ -25,6 +25,7 @@ export function SearchResults() { const [searchResults, setSearchResults] = useState([]); const [isLoading, setIsLoading] = useState(false); + const [showMessage, setShowMessage] = useState(false); const debouncedFunc = useRef void> | null>(null); @@ -35,19 +36,40 @@ export function SearchResults() { 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(() => { setIsLoading(true); if (debouncedFunc.current) debouncedFunc.current.cancel(); debouncedFunc.current = debounce(() => { - window.electron - .searchGames(searchParams.get("query") ?? "") - .then((results) => { - setSearchResults(results); - }) - .finally(() => { - setIsLoading(false); - }); + const query = searchParams.get("query") ?? ""; + + if (query.length < 3) { + setIsLoading(false); + setShowMessage(true); + setSearchResults([]); + return; + } + + setShowMessage(false); + fetchGames(query); }, 500); debouncedFunc.current(); @@ -75,6 +97,14 @@ export function SearchResults() { )} + {!isLoading && showMessage && ( +
+ + +

{"Comece a digitar para pesquisar…"}

+
+ )} + {!isLoading && searchResults.length === 0 && (