kbar icon indicating copy to clipboard operation
kbar copied to clipboard

support overriding command-score for command ranking

Open gvacaliuc opened this issue 2 years ago • 0 comments

Hi there! I've been a super happy user of this library, but recently hit some performance issues that I traced back to the use of command-score. Full disclosure, I'm probably abusing this library a bit by using it for a full site search (~5k "actions").

I hacked together an approach in my application by vendoring most of useMatches.tsx and plumbing in a:

type SearchFn = (actions: ActionImpl[], search: string) => Match[]

which roughly replaces these lines: https://github.com/timc1/kbar/blob/b4666e06d2df1be9a84e03132e6d9f4c56f17f3c/src/useMatches.tsx#L188-L201

example SearchFn using fuzzysort:

function fuzzysortSearch(actions: ActionImpl[], search: string): Match[] {
  // dont return results with a fuzzysort score lower than -1000
  const threshold = 1000;
  // only return up to 100 results
  const limit = 100;
  const fuzzysortResults = fuzzysort.go(search, actions, {
    key: 'keywords',
    limit: limit,
    threshold: -threshold,
  });
  // map the fuzzysort scores to what kbar expects (0 = no match, 1 = perfect match)
  return fuzzysortResults.map(result => {
    return { score: 1 + result.score / threshold, action: result.obj }
  });
}

any interest in supporting a modular command ranking api?

gvacaliuc avatar Sep 08 '22 04:09 gvacaliuc