From 8f6b5f2468a4e811fac009dba5874a3872f9beaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Daubensch=C3=BCtz?= Date: Mon, 17 Feb 2025 09:46:12 +0100 Subject: [PATCH] refactor: Simplify search functionality and update UI layout --- src/views/search.mjs | 9 +++ src/web/src/Search.jsx | 126 ++++++++++++++--------------------------- 2 files changed, 51 insertions(+), 84 deletions(-) diff --git a/src/views/search.mjs b/src/views/search.mjs index 4871604..44698ab 100644 --- a/src/views/search.mjs +++ b/src/views/search.mjs @@ -98,6 +98,15 @@ export default async function (theme, query) { ${SecondHeader(theme, "search", null, null, sanitizedQuery)} + ${results.length > 0 + ? html` + + Search results for "${sanitizedQuery}" + + ` + : null} ${results.length > 0 ? await processSearchResults(results) : html` diff --git a/src/web/src/Search.jsx b/src/web/src/Search.jsx index b440fd9..9f0d3f7 100644 --- a/src/web/src/Search.jsx +++ b/src/web/src/Search.jsx @@ -37,42 +37,9 @@ const SearchInterface = () => { return () => window.removeEventListener("resize", checkMobile); }, []); - const performSearch = async (searchText) => { - try { - const response = await fetch("/api/v1/search", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ query: searchText }), - }); - - if (!response.ok) { - throw new Error("Search failed"); - } - - const data = await response.json(); - if (data.status === "success" && Array.isArray(data.data)) { - setResults(data.data); - } else { - setResults([]); - } - } catch (error) { - console.error("Search error:", error); - setResults([]); - } finally { - setIsLoading(false); - } - }; - const handleSearch = (value) => { - if (debounceTimer.current) { - clearTimeout(debounceTimer.current); - } - if (value.trim()) { - setIsLoading(true); - debounceTimer.current = setTimeout(() => performSearch(value), 300); - } else { - setResults([]); + window.location.href = `/search?q=${encodeURIComponent(value.trim())}`; } }; @@ -196,7 +163,7 @@ const SearchInterface = () => { disableDiscovery={iOS} PaperProps={{ style: { - height: "75vh", + height: "20vh", borderTopLeftRadius: "2px", borderTopRightRadius: "2px", backgroundColor: "var(--background-color0)", @@ -212,60 +179,51 @@ const SearchInterface = () => { flexDirection: "column", }} > -
- { - setQuery(e.target.value); - handleSearch(e.target.value); - }} - style={{ - width: "100%", - padding: "8px", - fontSize: "16px", - border: "var(--border-thin)", - borderRadius: "2px", - backgroundColor: "white", - fontFamily: "var(--font-family)", - outline: "none", - }} - placeholder="Search all of Kiwi..." - /> -
- -
- {isLoading ? ( -
+
+ setQuery(e.target.value)} + onKeyDown={(e) => { + if (e.key === 'Enter') { + handleSearch(e.target.value); + } + }} style={{ - textAlign: "center", - padding: "16px", - color: "var(--visited-link)", + flex: "0 0 70%", + padding: "8px", + fontSize: "16px", + border: "var(--border-thin)", + borderRadius: "2px", + backgroundColor: "white", + fontFamily: "var(--font-family)", + outline: "none", }} - > - Loading... -
- ) : results.length > 0 ? ( - results.map((result) => ( - - )) - ) : query ? ( -
+
- ) : null} + Search + +