Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: algolia search sorting #2328

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions src/pages/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,52 @@ export function DocSearchModal() {

return (
<DocSearch
transformItems={(item) => {
return item.reverse();
transformItems={(items) => {
/*
* Group items by hierarchy and remove items without hierarchy
*/
const groupBy = items.reduce((acc, item) => {
if (!item.hierarchy) {
return acc;
}
const list = acc[item.hierarchy.lvl1] || [];

return {
...acc,
[item.hierarchy.lvl1]: list.concat(item),
};
}, {});

/**
* Group based on parent page
*/
const groups = Object.keys(groupBy).map((level) => ({
items: groupBy[level],
}));

const groupItems = groups?.[0]?.items || [];

/**
* Find the parent page from groupItems
*/
const parent = groupItems?.find((item) => {
if (
item.hierarchy.lvl1 !== null &&
item.hierarchy.lvl2 == null &&
item.hierarchy.lvl3 == null &&
item.hierarchy.lvl4 == null &&
item.hierarchy.lvl5 == null &&
item.hierarchy.lvl6 == null
) {
return item;
}
});

/**
* find the index of the parent from groupItems and put it in the first index
*/
const parentIndex = groupItems?.indexOf(parent);
return groupItems.splice(parentIndex, 1).concat(groupItems);
}}
placeholder="Search docs..."
maxResultsPerGroup={100}
Expand Down
Loading