Skip to content

Commit

Permalink
fix: change sort priority - cmdk
Browse files Browse the repository at this point in the history
  • Loading branch information
kuri-sun committed Apr 25, 2024
1 parent 1ba2d02 commit 2fbad32
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions apps/docs/components/cmdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ export const Cmdk: FC<{}> = () => {
}
};

const prioritizeFirstLevelItems = (a: SearchResultItem, b: SearchResultItem) => {
if (a.type === "lvl1") {
return -1;
} else if (b.type === "lvl1") {
return 1;
}

return 0;
};

const results = useMemo<SearchResultItem[]>(
function getResults() {
if (query.length < 2) return [];
Expand All @@ -165,12 +175,22 @@ export const Cmdk: FC<{}> = () => {
if (words.length === 1) {
return matchSorter(data, query, {
keys: MATCH_KEYS,
sorter: (matches) => {
matches.sort((a, b) => prioritizeFirstLevelItems(a.item, b.item));

return matches;
},
}).slice(0, MAX_RESULTS);
}

const matchesForEachWord = words.map((word) =>
matchSorter(data, word, {
keys: MATCH_KEYS,
sorter: (matches) => {
matches.sort((a, b) => prioritizeFirstLevelItems(a.item, b.item));

return matches;
},
}),
);

Expand Down

0 comments on commit 2fbad32

Please sign in to comment.