Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat: replace fuzzy with ufuzzy in most places (#138)

gbirman632ef120d0be
diff --git a/js/app/packages/app/component/UnifiedListView.tsx b/js/app/packages/app/component/UnifiedListView.tsx
index 45aa682562..6179139e98 100644
--- a/js/app/packages/app/component/UnifiedListView.tsx
+++ b/js/app/packages/app/component/UnifiedListView.tsx
@@ -32,7 +32,7 @@ import { TOKENS } from '@core/hotkey/tokens';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { isMobileWidth } from '@core/mobile/mobileWidth';
import { useCombinedRecipients } from '@core/signal/useCombinedRecipient';
-import { fuzzyNameMatch } from '@core/util/fuzzyName';
+import { fuzzyMatch } from '@core/util/fuzzy';
import SearchIcon from '@icon/regular/magnifying-glass.svg?component-solid';
import LoadingSpinner from '@icon/regular/spinner.svg?component-solid';
import XIcon from '@icon/regular/x.svg?component-solid';
@@ -444,26 +444,18 @@ export function UnifiedListView(props: UnifiedListViewProps) {
if (!searchText() || searchText().length === 0) return items;
const query = searchText();
+ const matchResults = fuzzyMatch(query, items, (item) => item.name);
- return items
- .map((item) => {
- const matchResult = fuzzyNameMatch(query, item.name);
-
- if (!matchResult) return null;
-
- return {
- ...item,
- search: {
- nameHighlight: matchResult.nameHighlight,
- contentHighlights: null,
- source: 'local',
- },
- } as WithNotification<WithSearch<EntityData>>;
- })
- .filter(
- (item): item is WithNotification<WithSearch<EntityData>> =>
- item !== null
- );
+ return matchResults.map((result) => {
+ return {
+ ...result.item,
+ search: {
+ nameHighlight: result.nameHighlight,
+ contentHighlights: null,
+ source: 'local',
+ },
+ } as WithNotification<WithSearch<EntityData>>;
+ });
}
: undefined
);
diff --git a/js/app/packages/block-channel/component/AttachMenu.tsx b/js/app/packages/block-channel/component/AttachMenu.tsx
index f82a9679c3..4aebc7cd62 100644
--- a/js/app/packages/block-channel/component/AttachMenu.tsx
+++ b/js/app/packages/block-channel/component/AttachMenu.tsx
@@ -6,6 +6,7 @@ import clickOutside from '@core/directive/clickOutside';
import { fileSelector } from '@core/directive/fileSelector';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
import type { InputAttachment } from '@core/store/cacheChannelInput';
+import { fuzzyFilter } from '@core/util/fuzzy';
import { getItemBlockName } from '@core/util/getItemBlockName';
import {
autoUpdate,
@@ -19,7 +20,6 @@ import DeviceMobileIcon from '@icon/regular/device-mobile-speaker.svg';
import LaptopIcon from '@icon/regular/laptop.svg';
import SearchIcon from '@icon/regular/magnifying-glass.svg';
import { useHistory } from '@service-storage/history';
-import fuzzy from 'fuzzy';
import {
createEffect,
createMemo,
@@ -89,13 +89,8 @@ export function AttachMenu(props: AttachMenuProps) {
});
const rankedHistory = createMemo(() => {
- const searchQuery = input().toLowerCase();
- if (!searchQuery) return baseHistory();
- return fuzzy
- .filter(searchQuery, baseHistory(), {
- extract: (item) => item.name,
- })
- .map((item) => item.original);
+ const searchQuery = input();
+ return fuzzyFilter(searchQuery, baseHistory(), (item) => item.name);
});
createEffect(() => {
diff --git a/js/app/packages/block-project/component/Block.tsx b/js/app/packages/block-project/component/Block.tsx
index 25e83a02c8..214fa1849e 100644
--- a/js/app/packages/block-project/component/Block.tsx
+++ b/js/app/packages/block-project/component/Block.tsx
@@ -28,6 +28,7 @@ import { setExpandedProject } from '@core/component/FileList/fileTree';
import { ENABLE_FOLDER_UPLOAD } from '@core/constant/featureFlags';
import { fileFolderDrop } from '@core/directive/fileFolderDrop';
import { fileSelector } from '@core/directive/fileSelector';
+import { fuzzyTest } from '@core/util/fuzzy';
import { isErr } from '@core/util/maybeResult';
import {
handleFileFolderDrop,
@@ -43,7 +44,6 @@ import { useHistoryTree } from '@service-storage/history';
import { refetchResources } from '@service-storage/util/refetchResources';
import { useSearchParams } from '@solidjs/router';
import { toast } from 'core/component/Toast/Toast';
-import fuzzy from 'fuzzy';
import {
type Component,
createEffect,
@@ -177,7 +177,7 @@ const Block: Component = () => {
);
const filteredItems = items.filter(
(item) =>
- fuzzy.test(fileSearchQuery(), item.name) &&
+ fuzzyTest(fileSearchQuery(), item.name) &&
matchesActiveFilters(
item,
activeFilters(),
diff --git a/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx b/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx
index dd818e8f97..f055f752c2 100644
--- a/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx
+++ b/js/app/packages/core/component/AI/component/input/ChatAttachMenu.tsx
@@ -1,9 +1,5 @@
import { withAnalytics } from '@coparse/analytics';
import { SUPPORTED_ATTACHMENT_EXTENSIONS } from '@core/component/AI/constant';
-import type { Item } from '@service-storage/generated/schemas/item';
-
-const { track, TrackingEvents } = withAnalytics();
-
import {
useChatAttachableHistory,
useGetChatAttachmentInfo,
@@ -14,6 +10,7 @@ import { OldMenu, OldMenuItem } from '@core/component/OldMenu';
import clickOutside from '@core/directive/clickOutside';
import { fileSelector } from '@core/directive/fileSelector';
import { isTouchDevice } from '@core/mobile/isTouchDevice';
+import { fuzzyFilter } from '@core/util/fuzzy';
import { getItemBlockName } from '@core/util/getItemBlockName';
import {
autoUpdate,
@@ -26,7 +23,7 @@ import {
import DeviceMobileIcon from '@icon/regular/device-mobile-speaker.svg';
import LaptopIcon from '@icon/regular/laptop.svg';
import SearchIcon from '@icon/regular/magnifying-glass.svg';
-import fuzzy from 'fuzzy';
+import type { Item } from '@service-storage/generated/schemas/item';
import {
createEffect,
createMemo,
@@ -36,6 +33,8 @@ import {
} from 'solid-js';
import { type VirtualizerHandle, VList } from 'virtua/solid';
+const { track, TrackingEvents } = withAnalytics();
+
// NOTE: solid directives
false && fileSelector;
false && clickOutside;
@@ -89,13 +88,8 @@ export function ChatAttachMenu(props: ChatAttachMenuProps) {
};
const rankedHistory = createMemo(() => {
- const searchQuery = input().toLowerCase();
- if (!searchQuery) return history();
- return fuzzy
- .filter(searchQuery, history(), {
- extract: (item) => item.name,
- })
- .map((item) => item.original);
+ const searchQuery = input();
+ return fuzzyFilter(searchQuery, history(), (item) => item.name);
});
// Reset selected index when results change
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/menu/ActionsMenu.tsx b/js/app/packages/core/component/LexicalMarkdown/component/menu/ActionsMenu.tsx
index 934e7f2c77..6615967d7d 100644
--- a/js/app/packages/core/component/LexicalMarkdown/component/menu/ActionsMenu.tsx
+++ b/js/app/packages/core/component/LexicalMarkdown/component/menu/ActionsMenu.tsx
@@ -2,8 +2,8 @@ import { BozzyBracketInnerSibling } from '@core/component/BozzyBracket';
import { type PortalScope, ScopedPortal } from '@core/component/ScopedPortal';
import clickOutside from '@core/directive/clickOutside';
import { isMobileWidth } from '@core/mobile/mobileWidth';
+import { fuzzyFilter } from '@core/util/fuzzy';
import { debounce } from '@solid-primitives/scheduled';
-import fuzzy from 'fuzzy';
import type { LexicalEditor } from 'lexical';
import {
createEffect,
@@ -112,14 +112,9 @@ export function ActionMenu(props: {
});
const filteredItems = createMemo(() => {
- return fuzzy
- .filter(searchTerm(), ACTIONS, {
- extract: (item) => {
- return [item.name, ...item.keywords].join(' ');
- },
- })
- .map((result) => result.original)
- .slice(0, maxItems());
+ return fuzzyFilter(searchTerm(), ACTIONS, (item) =>
+ [item.name, ...item.keywords].join(' ')
+ ).slice(0, maxItems());
});
const [escapeSpaceState, setEscapeSpaceState] = createSignal<
diff --git a/js/app/packages/core/util/fuzzy.ts b/js/app/packages/core/util/fuzzy.ts
new file mode 100644
index 0000000000..6d40ce10dd
--- /dev/null
+++ b/js/app/packages/core/util/fuzzy.ts
@@ -0,0 +1,98 @@
+import uFuzzy from '@leeoniya/ufuzzy';
+
+export interface FuzzyNameMatchResult {
+ nameHighlight: string;
+ score: number;
+}
+
+export interface FuzzyNameMatchResultWithItem<T> {
+ item: T;
+ nameHighlight: string;
+ score: number;
+}
+
+const uf = new uFuzzy({});
+
+const mark = (part: string, matched: boolean) =>
+ matched ? `<macro_em>${part}</macro_em>` : part;
+
+const append = (accum: string, part: string) => accum + part;
+
+/**
+ * Fuzzy matches items against a query, returning filtered and ranked results with highlighted matches.
+ * Returns all items with no highlights if query is empty.
+ */
+export function fuzzyMatch<T>(
+ query: string,
+ items: T[],
+ extract: (item: T) => string
+): FuzzyNameMatchResultWithItem<T>[] {
+ if (!query)
+ return items.map((item) => ({
+ item,
+ nameHighlight: extract(item),
+ score: 0,
+ }));
+
+ const haystack = items.map(extract);
+ const idxs = uf.filter(haystack, query);
+
+ if (!idxs || idxs.length === 0) return [];
+
+ const info = uf.info(idxs, haystack, query);
+ const order = uf.sort(info, haystack, query);
+
+ if (!order || order.length === 0) return [];
+
+ return order.map((orderIdx) => {
+ const infoIdx = info.idx[orderIdx];
+ const ranges = info.ranges[orderIdx];
+
+ const nameHighlight = ranges
+ ? uFuzzy.highlight(haystack[infoIdx], ranges, mark, '', append)
+ : haystack[infoIdx];
+
+ return {
+ item: items[infoIdx],
+ nameHighlight,
+ score: orderIdx,
+ };
+ });
+}
+
+/**
+ * Fuzzy filters and ranks items against a query without highlighting.
+ * Returns all items if query is empty.
+ */
+export function fuzzyFilter<T>(
+ query: string,
+ items: T[],
+ extract: (item: T) => string
+): T[] {
+ if (!query) return items;
+
+ const haystack = items.map(extract);
+ const idxs = uf.filter(haystack, query);
+
+ if (!idxs || idxs.length === 0) return [];
+
+ const info = uf.info(idxs, haystack, query);
+ const order = uf.sort(info, haystack, query);
+
+ if (!order || order.length === 0) return [];
+
+ return order.map((orderIdx) => items[info.idx[orderIdx]]);
+}
+
+/**
+ * Tests if text matches a fuzzy query.
+ * Returns true if query is empty or matches.
+ */
+export function fuzzyTest(query: string, text: string): boolean {
+ if (!query) return true;
+
+ const haystack = [text];
+ const idxs = uf.filter(haystack, query);
+
+ return idxs !== null && idxs.length > 0;
+}
diff --git a/js/app/packages/core/util/fuzzyName.ts b/js/app/packages/core/util/fuzzyName.ts
deleted file mode 100644
index d2b81e83fc..0000000000
--- a/js/app/packages/core/util/fuzzyName.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-import uFuzzy from '@leeoniya/ufuzzy';
-
-export interface FuzzyNameMatchResult {
- nameHighlight: string;
- score: number;
-}
-
-const uf = new uFuzzy({});
-
-const mark = (part: string, matched: boolean) =>
- matched ? `<macro_em>${part}</macro_em>` : part;
-
-const append = (accum: string, part: string) => accum + part;
-
-export function fuzzyNameMatch(
- query: string,
- name: string
-): FuzzyNameMatchResult | null {
- const needle = query;
- const haystack = [name];
- const idxs = uf.filter(haystack, needle);
-
- if (!idxs || idxs.length === 0) return null;
-
- const info = uf.info(idxs, haystack, needle);
- const order = uf.sort(info, haystack, needle);
-
- if (!order || order.length === 0) return null;
-
- const infoIdx = order[0];
- const ranges = info.ranges[infoIdx];
-
- if (!ranges) return null;
-
- const nameHighlight = uFuzzy.highlight(
- haystack[info.idx[infoIdx]],
- ranges,
- mark,
- '',
- append
- );
-
- return {
- nameHighlight,
- score: info.idx[infoIdx],
- };
-}