From f78357589528fdbd26cc7d581a3add3dbd673bcd Mon Sep 17 00:00:00 2001 From: Tim Date: Wed, 16 Aug 2023 15:33:48 -0400 Subject: [PATCH] chore: fuse cleanup (#329) --- src/useMatches.tsx | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/src/useMatches.tsx b/src/useMatches.tsx index 7423fe8..595fc86 100644 --- a/src/useMatches.tsx +++ b/src/useMatches.tsx @@ -9,6 +9,25 @@ export const NO_GROUP = { priority: Priority.NORMAL, }; +const fuseOptions: Fuse.IFuseOptions = { + keys: [ + { + name: "name", + weight: 0.5, + }, + { + name: "keywords", + getFn: (item) => (item.keywords ?? "").split(","), + weight: 0.5, + }, + "subtitle", + ], + includeScore: true, + includeMatches: true, + threshold: 0.2, + minMatchCharLength: 1, +}; + function order(a, b) { /** * Larger the priority = higher up the list @@ -74,29 +93,7 @@ export function useMatches() { return getDeepResults(rootResults); }, [getDeepResults, rootResults, emptySearch]); - const fuseOptions = { - keys: [ - { - name: "name", - weight: 0.5, - }, - { - name: "keywords", - getFn: (item) => item.keywords.split(","), // make keyword an array. so fuse can look through words individually - weight: 0.5, - }, - "subtitle", - ], - includeScore: true, - includeMatches: true, - threshold: 0.2, - minMatchCharLength: 1, - tokenize: (str) => { - // Example: Preserve hyphens and special characters as separate tokens - return str.split(/[\s\-,.!()]+/).filter(Boolean); - }, - }; - const fuse = new Fuse(filtered, fuseOptions); + const fuse = React.useMemo(() => new Fuse(filtered, fuseOptions), [filtered]); const matches = useInternalMatches(filtered, search, fuse);