From e5b5f2d4f44af0ad5235f0ecc8f3fed1ad9ced4d Mon Sep 17 00:00:00 2001 From: Bruno Charest Date: Wed, 27 May 2026 09:59:35 -0400 Subject: [PATCH] Limit search history to 5 items and isolate suggestion fetches --- frontend/app.js | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/frontend/app.js b/frontend/app.js index 183c068..bdf5cfc 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -451,7 +451,7 @@ const vault = document.getElementById("vault-filter").value; // History — always show filtered history - const historyItems = SearchHistory.filter(inputValue); + const historyItems = SearchHistory.filter(inputValue).slice(0, 5); this._renderHistory(historyItems, inputValue); // Title and tag suggestions from API (debounced) — always fetch both @@ -482,25 +482,24 @@ async _fetchSuggestions(prefix, vault) { suggestAbortController = new AbortController(); + // Fetch titles and tags independently so one failure doesn't block the other try { - const [titlesRes, tagsRes] = await Promise.all([ - api(`/api/suggest?q=${encodeURIComponent(prefix)}&vault=${encodeURIComponent(vault)}&limit=8`, { signal: suggestAbortController.signal }), - api(`/api/tags/suggest?q=${encodeURIComponent(prefix)}&vault=${encodeURIComponent(vault)}&limit=8`, { signal: suggestAbortController.signal }), - ]); - + const titlesRes = await api(`/api/suggest?q=${encodeURIComponent(prefix)}&vault=${encodeURIComponent(vault)}&limit=5`, { signal: suggestAbortController.signal }); this._renderTitles(titlesRes.suggestions || [], prefix); - this._renderTags(tagsRes.suggestions || [], prefix); - - const hasTitles = (titlesRes.suggestions || []).length > 0; - const hasTags = (tagsRes.suggestions || []).length > 0; - this._titlesSection.hidden = !hasTitles; - this._tagsSection.hidden = !hasTags; - - if (hasTitles || hasTags) this.show(); - this._collectItems(); + this._titlesSection.hidden = !(titlesRes.suggestions || []).length; + if (titlesRes.suggestions?.length) this.show(); } catch (err) { - if (err.name !== "AbortError") console.error("Suggestion fetch error:", err); + if (err.name !== "AbortError") this._titlesSection.hidden = true; } + try { + const tagsRes = await api(`/api/tags/suggest?q=${encodeURIComponent(prefix)}&vault=${encodeURIComponent(vault)}&limit=5`, { signal: suggestAbortController.signal }); + this._renderTags(tagsRes.suggestions || [], prefix); + this._tagsSection.hidden = !(tagsRes.suggestions || []).length; + if (tagsRes.suggestions?.length) this.show(); + } catch (err) { + if (err.name !== "AbortError") this._tagsSection.hidden = true; + } + this._collectItems(); }, _renderHistory(items, query) { @@ -4957,7 +4956,7 @@ // --- Focus handler: show history dropdown --- input.addEventListener("focus", () => { if (input.value.length === 0) { - const historyItems = SearchHistory.filter(""); + const historyItems = SearchHistory.filter("").slice(0, 5); if (historyItems.length > 0) { AutocompleteDropdown.populate("", 0); }