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: improve unified list search order (#134)

gbirmaneceb7cdb45e7
diff --git a/js/app/bun.lock b/js/app/bun.lock
index 6462a7ad7b..91a9ad677d 100644
--- a/js/app/bun.lock
+++ b/js/app/bun.lock
@@ -22,6 +22,7 @@
"@floating-ui/dom": "^1.7.4",
"@inkibra/tauri-plugins": "git+https://github.com/seanaye/tauri-plugins.git#0c932ffd3202145b40645246d87a38deb72ba09c",
"@kobalte/core": "^0.13.11",
+ "@leeoniya/ufuzzy": "^1.0.19",
"@lexical/history": "^0.32.1",
"@lexical/html": "^0.32.1",
"@lexical/link": "^0.32.1",
@@ -1073,6 +1074,8 @@
"@kwsites/promise-deferred": ["@kwsites/[email protected]", "", {}, "sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw=="],
+ "@leeoniya/ufuzzy": ["@leeoniya/[email protected]", "", {}, "sha512-0pikDeYt0IHEUPza5RTCDXc/17S1pTrYnReEMp8Aa6k1ovzw5QdZLwicW8TjljwEZRb6oYag0xmALohrcq/yOQ=="],
+
"@lexical/clipboard": ["@lexical/[email protected]", "", { "dependencies": { "@lexical/html": "0.32.1", "@lexical/list": "0.32.1", "@lexical/selection": "0.32.1", "@lexical/utils": "0.32.1", "lexical": "0.32.1" } }, "sha512-oO7CuMVh3EFEqtE6+7Ccf7jMD5RNUmSdTnFm/X4kYNGqs9lgGt8j5PgSk7oP9OuAjxKNdBTbltSlh54CX3AUIg=="],
"@lexical/code": ["@lexical/[email protected]", "", { "dependencies": { "@lexical/utils": "0.32.1", "lexical": "0.32.1", "prismjs": "^1.30.0" } }, "sha512-2rXj8s/CG32XKQ2EpORpACfpzyAxB+/SrQW2cjwczarLs5Fxnx6u6HwahZnxaF0z5UHIPUy90qDiOiRExc74Yg=="],
diff --git a/js/app/package.json b/js/app/package.json
index f1d5acf4ad..9c3d4066c7 100644
--- a/js/app/package.json
+++ b/js/app/package.json
@@ -58,6 +58,7 @@
"@floating-ui/dom": "^1.7.4",
"@inkibra/tauri-plugins": "git+https://github.com/seanaye/tauri-plugins.git#0c932ffd3202145b40645246d87a38deb72ba09c",
"@kobalte/core": "^0.13.11",
+ "@leeoniya/ufuzzy": "^1.0.19",
"@lexical/history": "^0.32.1",
"@lexical/html": "^0.32.1",
"@lexical/link": "^0.32.1",
diff --git a/js/app/packages/core/util/fuzzyName.ts b/js/app/packages/core/util/fuzzyName.ts
index dbf4c771e0..d2b81e83fc 100644
--- a/js/app/packages/core/util/fuzzyName.ts
+++ b/js/app/packages/core/util/fuzzyName.ts
@@ -1,53 +1,47 @@
-import fuzzy from 'fuzzy';
+import uFuzzy from '@leeoniya/ufuzzy';
export interface FuzzyNameMatchResult {
nameHighlight: string;
score: number;
}
-/**
- * Performs fuzzy name matching with quality filtering.
- * Returns null if no match or if the match is too scattered.
- */
+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 matchResult = fuzzy.match(query, name, {
- pre: '<macro_em>',
- post: '</macro_em>',
- });
+ const needle = query;
+ const haystack = [name];
+ const idxs = uf.filter(haystack, needle);
- if (!matchResult) return null;
+ if (!idxs || idxs.length === 0) return null;
- // Merge adjacent highlight tags
- const mergedHighlight = matchResult.rendered.replace(
- /<\/macro_em><macro_em>/g,
- ''
- );
+ const info = uf.info(idxs, haystack, needle);
+ const order = uf.sort(info, haystack, needle);
- const nameLower = name.toLowerCase();
- const queryLower = query.toLowerCase();
+ if (!order || order.length === 0) return null;
- // Always include exact matches, starts-with, or substring matches
- const isGoodMatch =
- nameLower === queryLower ||
- nameLower.startsWith(queryLower) ||
- nameLower.includes(queryLower);
+ const infoIdx = order[0];
+ const ranges = info.ranges[infoIdx];
- if (!isGoodMatch) {
- // Count how many separate highlight segments there are
- const highlightSegments = (mergedHighlight.match(/<macro_em>/g) || [])
- .length;
+ if (!ranges) return null;
- // Reject if more than 50% of characters are individual segments (too scattered)
- if (highlightSegments > query.length * 0.5) {
- return null;
- }
- }
+ const nameHighlight = uFuzzy.highlight(
+ haystack[info.idx[infoIdx]],
+ ranges,
+ mark,
+ '',
+ append
+ );
return {
- nameHighlight: mergedHighlight,
- score: matchResult.score,
+ nameHighlight,
+ score: info.idx[infoIdx],
};
}
diff --git a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
index 9fbde0d9b4..1182035fbe 100644
--- a/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
+++ b/js/app/packages/macro-entity/src/components/UnifiedInfiniteList.tsx
@@ -142,94 +142,14 @@ const deduplicateEntities = <T extends EntityData>(entities: T[]): T[] => {
};
/**
- * Calculates match quality score for local search results.
- * Higher score = better match.
- * Prioritizes: exact match > starts with > word boundary > fuzzy match
+ * Sorts entities for search mode
*/
-const getLocalMatchScore = (
- entityName: string,
- nameHighlight: string | null
-): number => {
- if (!nameHighlight) return 0;
-
- const name = entityName.toLowerCase();
-
- // Extract the search query from the highlighted text
- // The highlighted portion is wrapped in <macro_em>...</macro_em>
- const highlightedText = nameHighlight
- .replace(/<\/?macro_em>/g, '')
- .toLowerCase();
-
- // Count number of highlight segments (fewer segments = more contiguous match)
- const highlightSegments = (nameHighlight.match(/<macro_em>/g) || []).length;
-
- // Exact match (highest priority)
- if (name === highlightedText) return 1000;
-
- // Starts with match
- if (name.startsWith(highlightedText)) return 900;
-
- // Single contiguous match (not scattered)
- if (highlightSegments === 1) {
- // Word boundary match (after space, comma, etc.)
- if (
- name.match(
- new RegExp(
- `[\\s,+]${highlightedText.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}`
- )
- )
- ) {
- return 800;
- }
- return 700; // Contiguous substring
- }
-
- // Multiple segments (fuzzy/scattered match) - penalize based on number of segments
- return Math.max(100, 500 - highlightSegments * 50);
-};
-
-/**
- * Sorts entities for search mode:
- * 1. Channels first
- * 2. Local search results before service results
- * 3. Within local results, better name matches come first
- */
-const sortEntitiesForSearch = <T extends EntityData>(a: T, b: T): number => {
+const _sortEntitiesForSearch = <T extends EntityData>(a: T, b: T): number => {
const aHasSearch = isSearchEntity(a);
const bHasSearch = isSearchEntity(b);
if (aHasSearch && bHasSearch) {
- // Channel results come before other results
- const aIsChannel = a.type === 'channel';
- const bIsChannel = b.type === 'channel';
-
- if (aIsChannel && !bIsChannel) return -1;
- if (!aIsChannel && bIsChannel) return 1;
-
- // Local results come before service results
- const aSource = a.search.source;
- const bSource = b.search.source;
-
- if (aSource === 'local' && bSource === 'service') return -1;
- if (aSource === 'service' && bSource === 'local') return 1;
-
- // Within local results, sort by match quality
- if (
- aSource === 'local' &&
- bSource === 'local' &&
- aIsChannel &&
- bIsChannel
- ) {
- const aScore = getLocalMatchScore(a.name, a.search.nameHighlight);
- const bScore = getLocalMatchScore(b.name, b.search.nameHighlight);
-
- if (aScore !== bScore) {
- return bScore - aScore; // Higher score first
- }
-
- // If same score, prefer shorter names (more focused match)
- return a.name.length - b.name.length;
- }
+ // custom sort here
}
return 0;
@@ -381,7 +301,10 @@ export function createUnifiedInfiniteList<T extends EntityData>({
const searching = isSearchActive?.();
if (searching) {
- return entities.toSorted(sortEntitiesForSearch);
+ // NOTE: the default sort will be channels, then local fuzzy name, then serach service
+ // avoiding doing an extra sort as a speed optimization
+ return entities;
+ // return entities.toSorted(sortEntitiesForSearch);
}
if (!sortFn) return entities;