Limit search history to 5 items and isolate suggestion fetches

This commit is contained in:
2026-05-27 09:59:35 -04:00
parent 586acbe9a1
commit e5b5f2d4f4
+16 -17
View File
@@ -451,7 +451,7 @@
const vault = document.getElementById("vault-filter").value; const vault = document.getElementById("vault-filter").value;
// History — always show filtered history // History — always show filtered history
const historyItems = SearchHistory.filter(inputValue); const historyItems = SearchHistory.filter(inputValue).slice(0, 5);
this._renderHistory(historyItems, inputValue); this._renderHistory(historyItems, inputValue);
// Title and tag suggestions from API (debounced) — always fetch both // Title and tag suggestions from API (debounced) — always fetch both
@@ -482,25 +482,24 @@
async _fetchSuggestions(prefix, vault) { async _fetchSuggestions(prefix, vault) {
suggestAbortController = new AbortController(); suggestAbortController = new AbortController();
// Fetch titles and tags independently so one failure doesn't block the other
try { try {
const [titlesRes, tagsRes] = await Promise.all([ const titlesRes = await api(`/api/suggest?q=${encodeURIComponent(prefix)}&vault=${encodeURIComponent(vault)}&limit=5`, { signal: suggestAbortController.signal });
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 }),
]);
this._renderTitles(titlesRes.suggestions || [], prefix); this._renderTitles(titlesRes.suggestions || [], prefix);
this._renderTags(tagsRes.suggestions || [], prefix); this._titlesSection.hidden = !(titlesRes.suggestions || []).length;
if (titlesRes.suggestions?.length) this.show();
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();
} catch (err) { } 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) { _renderHistory(items, query) {
@@ -4957,7 +4956,7 @@
// --- Focus handler: show history dropdown --- // --- Focus handler: show history dropdown ---
input.addEventListener("focus", () => { input.addEventListener("focus", () => {
if (input.value.length === 0) { if (input.value.length === 0) {
const historyItems = SearchHistory.filter(""); const historyItems = SearchHistory.filter("").slice(0, 5);
if (historyItems.length > 0) { if (historyItems.length > 0) {
AutocompleteDropdown.populate("", 0); AutocompleteDropdown.populate("", 0);
} }