fix active page indicator on the sidebar being broken for some links

Took 9 minutes
This commit is contained in:
Braydon 2024-10-07 20:21:35 -04:00
parent 54230367e1
commit c4f7d4bf7e

View File

@ -44,7 +44,7 @@ const CategoryItem = ({
depth?: number;
isLast?: boolean;
}) => {
const path = usePathname();
const path = decodeURIComponent(usePathname());
const active =
(path === "/" && node.slug === "intro") || path === `/${node.slug}`;
const [isOpen, setIsOpen] = useState(true);
@ -136,35 +136,32 @@ const CategoryItem = ({
const buildTree = (pages: DocsContentMetadata[]): Record<string, TreeNode> => {
const tree: Record<string, TreeNode> = {};
// Sort pages by the order property
const sortedPages = pages.sort(
(a: DocsContentMetadata, b: DocsContentMetadata) => {
pages
.sort((a: DocsContentMetadata, b: DocsContentMetadata) => {
const orderA = a.order ?? Number.MAX_SAFE_INTEGER;
const orderB = b.order ?? Number.MAX_SAFE_INTEGER;
return orderA - orderB;
}
);
})
.forEach((page: DocsContentMetadata) => {
const parts: string[] | undefined = page.slug?.split("/");
let currentLevel = tree;
sortedPages.forEach((page: DocsContentMetadata) => {
const parts: string[] | undefined = page.slug?.split("/");
let currentLevel = tree;
parts?.forEach((part: string, index: number) => {
if (!currentLevel[part]) {
currentLevel[part] = {
title: part,
slug: parts.slice(0, index + 1).join("/"),
isFolder: index < parts.length - 1,
children: {},
};
}
if (index === parts.length - 1) {
currentLevel[part].title = page.title;
currentLevel[part].isFolder = false;
}
currentLevel = currentLevel[part].children;
parts?.forEach((part: string, index: number) => {
if (!currentLevel[part]) {
currentLevel[part] = {
title: part,
slug: parts.slice(0, index + 1).join("/"),
isFolder: index < parts.length - 1,
children: {},
};
}
if (index === parts.length - 1) {
currentLevel[part].title = page.title;
currentLevel[part].isFolder = false;
}
currentLevel = currentLevel[part].children;
});
});
});
return tree;
};