Skip to content

Commit

Permalink
Write mergeCrumbs method
Browse files Browse the repository at this point in the history
It merges the n last crumbs if they share the same href
  • Loading branch information
nas-tabchiche committed Dec 20, 2024
1 parent 0c1dc71 commit 72531da
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions frontend/src/lib/utils/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ const homeCrumb: Breadcrumb = { label: m.home(), href: '/', icon: 'fa-regular fa
const createBreadcrumbs = (initialValue: Breadcrumb[]) => {
const breadcrumbs = writable<Breadcrumb[]>(initialValue);

function mergeCrumbs(crumbs: Breadcrumb[]) {
const mergedCrumbs: Breadcrumb[] = [];
for (const crumb of crumbs) {
const lastCrumb = mergedCrumbs[mergedCrumbs.length - 1];
if (lastCrumb?.href !== crumb.href) {
mergedCrumbs.push(crumb);
}
}
return mergedCrumbs;
}

function push(crumb: Breadcrumb[]) {
breadcrumbs.update((value) => {
const newCrumbs = [...value.slice(1, value.length), ...crumb];
const newCrumbs = mergeCrumbs([...value.slice(1, value.length), ...crumb]);
return [homeCrumb, ...newCrumbs.slice(-BREADCRUMBS_MAX_DEPTH)];
});
}

function replace(crumb: Breadcrumb[]) {
breadcrumbs.update(() => {
return [homeCrumb, ...crumb];
return mergeCrumbs([homeCrumb, ...crumb]);
});
}

Expand Down

0 comments on commit 72531da

Please sign in to comment.