« get me outta code hell

client: general search code cleanup - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-05-02 14:17:42 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-05-31 12:11:47 -0300
commit04a683494b762f6fc6ed8a237874d2009ba5bee1 (patch)
treeba00d30eefc23617e8dbee73b27ed73cee1497fa /src/static
parent57d18bac36c3451f4b100e5cb031869ca730a51a (diff)
client: general search code cleanup
Diffstat (limited to 'src/static')
-rw-r--r--src/static/js/client.js42
1 files changed, 27 insertions, 15 deletions
diff --git a/src/static/js/client.js b/src/static/js/client.js
index 8a1ee021..4836bedd 100644
--- a/src/static/js/client.js
+++ b/src/static/js/client.js
@@ -6,8 +6,15 @@
 // ephemeral nature.
 
 import {getColors} from '../shared-util/colors.js';
-import {accumulateSum, atOffset, empty, filterMultipleArrays, stitchArrays}
-  from '../shared-util/sugar.js';
+
+import {
+  accumulateSum,
+  atOffset,
+  empty,
+  filterMultipleArrays,
+  stitchArrays,
+  withEntries,
+} from '../shared-util/sugar.js';
 
 import FlexSearch from '../lib/flexsearch/flexsearch.bundle.module.min.js';
 
@@ -3423,9 +3430,8 @@ clientSteps.addPageListeners.push(addArtistExternalLinkTooltipPageListeners);
 // Internal search functionality --------------------------
 
 async function initSearch() {
-
-  // Copied directly from server search.js
-  const indexes = makeSearchIndexes(FlexSearch);
+  const indexes =
+    makeSearchIndexes(FlexSearch);
 
   const searchData =
     await fetch('/search-data/index.json')
@@ -3444,13 +3450,12 @@ async function initSearch() {
 }
 
 function searchAll(query, options = {}) {
-  const results = {};
-
-  for (const [indexName, index] of Object.entries(window.searchIndexes)) {
-    results[indexName] = index.search(query, options);
-  }
-
-  return results;
+  return (
+    withEntries(window.searchIndexes, entries => entries
+      .map(([indexName, index]) => [
+        indexName,
+        index.search(query, options),
+      ])));
 }
 
 document.addEventListener('DOMContentLoaded', initSearch);
@@ -3539,7 +3544,16 @@ function addSidebarSearchListeners() {
 }
 
 function activateSidebarSearch(query) {
-  showSidebarSearchResults(searchAll(query, {enrich: true}));
+  const {state} = sidebarSearchInfo;
+
+  if (state.stoppedTypingTimeout) {
+    clearTimeout(state.stoppedTypingTimeout);
+    state.stoppedTypingTimeout = null;
+  }
+
+  const results = searchAll(query, {enrich: true});
+
+  showSidebarSearchResults(results);
 }
 
 function clearSidebarSearch() {
@@ -3565,8 +3579,6 @@ function showSidebarSearchResults(results) {
             data: doc,
           }))));
 
-  console.log(flatResults);
-
   while (info.results.firstChild) {
     info.results.firstChild.remove();
   }