Skip to content

Commit

Permalink
fix: nodes not having real ids
Browse files Browse the repository at this point in the history
  • Loading branch information
onursagir committed May 10, 2024
1 parent ce3c394 commit 15ee557
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-mice-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"web": patch
---

Fixed breadcrumbs not rendering properly when parent of node isn't present
48 changes: 28 additions & 20 deletions apps/web/src/services/cms/get-navbar-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,34 @@ export type NavbarNode = {

export async function getNavbarTree() {
const pages = await getNavbarPages();

const root: NavbarNode = { name: 'root', children: new Map(), url: '/', id: '_' };

for (const page of pages) {
if (!page.slug || !page.name) continue;

const parts = page.name.split('/').filter(Boolean);

let parent = root;

for (const part of parts) {
if (!parent.children.has(part))
parent.children.set(part, { name: part, parent, children: new Map(), url: '', id: String(page.id) });

parent = parent.children.get(part)!;
}

parent.url = page.slug;
parent.name = parts.at(-1) || '';
}
const root: NavbarNode = { name: 'root', id: 'root', url: '', children: new Map() };

pages.forEach((item) => {
const parts = item.name!.split('/');
let current = root;

parts.forEach((part, index) => {
if (!current.children.has(part)) {
const nodeId = `synthetic:${Math.random().toString(36).substring(2, 9)}`;
const newNode: NavbarNode = {
url: '',
id: nodeId,
name: part,
parent: current,
children: new Map(),
};

current.children.set(part, newNode);
}

current = current.children.get(part)!;

if (index === parts.length - 1) {
current.url = item.slug!;
current.id = String(item.id);
}
});
});

return root;
}

0 comments on commit 15ee557

Please sign in to comment.