kbar icon indicating copy to clipboard operation
kbar copied to clipboard

Search doesn't match last word when entry is coming from `useRegisterActions`

Open brydar opened this issue 2 years ago • 6 comments

Hello! 👋

Since version 0.1.0-beta.42 the search matching has changed regarding the last word of the name of the action.

This seems to happen only on actions added using useRegisterActions.

Repro

Here is a repro of the issue: https://codesandbox.io/s/kbar-test-forked-8rj7cg?file=/src/index.js:393-418 If you search for "aaa" the results will contain the root action but not the one from the useRegisterActions

I tried to investigate the useMatches file but didn't find the fuse option that could differ from both cases 😅

Results

V0.1.0-beta.41 brydar 2023-10-23 at 10 09 24@2x

V0.1.0-beta.42 brydar 2023-10-23 at 10 10 56@2x

brydar avatar Oct 23 '23 09:10 brydar

Can confirm the issue also exists on beta43! Worked around it by downgrading to Beta 41. Thanks for sharing @brydar

charlesvinette avatar Oct 26 '23 16:10 charlesvinette

Here is just a minimal repro with fuse only:

const fuse_js_1 = require("fuse.js");

const filtered = [
  {
    priority: 0,
    ancestors: [],
    children: [],
    id: "blog",
    name: "Blog long name aaa",
    keywords: "",
    command: {},
  },
  {
    priority: 0,
    ancestors: [],
    children: [],
    id: "contact",
    name: "Contact",
    keywords: "",
    command: {},
  },
  {
    priority: 0,
    ancestors: [],
    children: [],
    id: "test1",
    name: "test registration bug aaa",
    keywords: "",
  },
];
const fuseOptions = {
  keys: [
    {
      name: "name",
      weight: 0.5,
    },
    {
      name: "keywords",
      getFn: function (item) {
        return item.keywords.split(",");
      },
      weight: 0.5,
    },
    "subtitle",
  ],
  includeScore: true,
  includeMatches: true,
  threshold: 0.2,
  minMatchCharLength: 1,
  tokenize: function (str) {
    // Example: Preserve hyphens and special characters as separate tokens
    return str.split(/[\s\-,.!()]+/).filter(Boolean);
  },
};

const fuse = new fuse_js_1(filtered, fuseOptions);
console.log(fuse.search("aaa")); // only receiving "Blog long name aaa" but not "test registration bug aaa"

It seems that the main question is: where to put the bar for threshold? 0.2 is probably too low for many cases, but what would be the best?

dubzzz avatar Dec 13 '23 12:12 dubzzz

Based on this page, the behaviour is expected given the current options passed to fuse. Actually we pass distance=100 and threshold=0.2, meaning that for strings longer than 0.2*100=20 characters we will have to match lots of characters before seeing them into the list of suggestions.

@timc1 Do you think there could be a way to let users configure part of fuse? I mean overriding some of the configuration to put threshold to something larger (or smaller). See other suggestion in the message right after

dubzzz avatar Dec 15 '23 13:12 dubzzz

I think setting the ignoreLocation to true might help. If such option is ok to be added into the configuration of fuse, maybe I can open a PR to add it. Cc @timc1

dubzzz avatar Dec 15 '23 13:12 dubzzz

Normally this issue should be fixed now with https://github.com/timc1/kbar/pull/350 (not released yet)

dubzzz avatar Jan 03 '24 14:01 dubzzz

Hey! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 03 '24 02:05 stale[bot]