Skip to content

Commit

Permalink
sort global search results
Browse files Browse the repository at this point in the history
  • Loading branch information
dliu27 committed Dec 18, 2024
1 parent 910838e commit 5651cea
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ const initialState: State = {

const DEBOUNCE_MSEC = 100;

// sort by Fuse score ascending, lower is better
const sortResultsByFuseScore = (
a: Fuse.FuseResult<SearchResult>,
b: Fuse.FuseResult<SearchResult>,
) => {
return (a.score ?? 0) - (b.score ?? 0);
};

export const SearchDialog = () => {
const history = useHistory();
const {initialize, loading, searchPrimary, searchSecondary} = useGlobalSearch({
Expand All @@ -82,7 +90,7 @@ export const SearchDialog = () => {
const [state, dispatch] = React.useReducer(reducer, initialState);
const {shown, queryString, primaryResults, secondaryResults, highlight} = state;

const results = [...primaryResults, ...secondaryResults];
const results = [...primaryResults, ...secondaryResults].sort(sortResultsByFuseScore);
const renderedResults = results.slice(0, MAX_DISPLAYED_RESULTS);
const numRenderedResults = renderedResults.length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ const fuseOptions = {
threshold: 0.3,
useExtendedSearch: true,
includeMatches: true,
includeScore: true,

// Allow searching to continue to the end of the string.
ignoreLocation: true,
Expand Down

0 comments on commit 5651cea

Please sign in to comment.