From 9b2d84b2d53f4fabe3a52fb9acd347c7fd425f63 Mon Sep 17 00:00:00 2001
From: Mark Haussmann <mark.haussmann@gmail.com>
Date: Fri, 17 May 2024 10:56:49 +1200
Subject: [PATCH] feat: use fuzzy search for command palette

When typing in workflow names using the command palette, I often
find myself wanting Sublime Text like fuzzy matching so I don't
have to remember the full, exact name of the workflow.

This PR adds the fuzzaldrin-plus package that is the successor of
the package used in the Atom editor to do fuzzy filtering.

* https://github.com/jeancroy/fuzz-aldrin-plus
* https://github.com/atom/fuzzaldrin
---
 package.json                       |  1 +
 src/content/commandPalette/App.vue | 11 ++++++-----
 yarn.lock                          |  5 +++++
 3 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/package.json b/package.json
index 60427e748..aa5bbbbd8 100644
--- a/package.json
+++ b/package.json
@@ -63,6 +63,7 @@
     "dayjs": "^1.11.6",
     "defu": "^6.1.2",
     "dexie": "^3.2.3",
+    "fuzzaldrin-plus": "^0.6.0",
     "html2canvas": "^1.4.1",
     "idb": "^7.0.2",
     "js-base64": "^3.7.5",
diff --git a/src/content/commandPalette/App.vue b/src/content/commandPalette/App.vue
index 9579d1855..99a232c9a 100644
--- a/src/content/commandPalette/App.vue
+++ b/src/content/commandPalette/App.vue
@@ -185,6 +185,7 @@ import {
 } from 'vue';
 import browser from 'webextension-polyfill';
 import cloneDeep from 'lodash.clonedeep';
+import { filter } from 'fuzzaldrin-plus';
 import workflowParameters from '@business/parameters';
 import { sendMessage } from '@/utils/message';
 import { debounce, parseJSON } from '@/utils/helper';
@@ -226,11 +227,11 @@ const paramsState = reactive({
 
 const rootElement = inject('rootElement');
 
-const workflows = computed(() =>
-  state.workflows.filter((workflow) =>
-    workflow.name.toLocaleLowerCase().includes(state.query.toLocaleLowerCase())
-  )
-);
+const workflows = computed(() => {
+  if (!state.query) return state.workflows;
+
+  return filter(state.workflows, state.query, { key: 'name' });
+});
 
 function getReadableShortcut(str) {
   const list = {
diff --git a/yarn.lock b/yarn.lock
index c80ee64bb..5eb4a8320 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -4035,6 +4035,11 @@ functions-have-names@^1.2.3:
   resolved "https://registry.yarnpkg.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834"
   integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ==
 
+fuzzaldrin-plus@^0.6.0:
+  version "0.6.0"
+  resolved "https://registry.yarnpkg.com/fuzzaldrin-plus/-/fuzzaldrin-plus-0.6.0.tgz#832f6489fbe876769459599c914a670ec22947ee"
+  integrity sha512-srIDThJHkdp3aPwJpR/HNzYZCRJwm07b/igxseoHSB7qR8e/gQp4F6lMGknE3TQI1Aq14TiFf/wzrHOp9LY/EA==
+
 gensync@^1.0.0-beta.2:
   version "1.0.0-beta.2"
   resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"