From 72531da3e2975a7a8a088163721f16286a1816da Mon Sep 17 00:00:00 2001 From: Nassim Tabchiche Date: Fri, 20 Dec 2024 19:33:38 +0100 Subject: [PATCH] Write mergeCrumbs method It merges the n last crumbs if they share the same href --- frontend/src/lib/utils/breadcrumbs.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/frontend/src/lib/utils/breadcrumbs.ts b/frontend/src/lib/utils/breadcrumbs.ts index 64f0b7521..5abe0fda6 100644 --- a/frontend/src/lib/utils/breadcrumbs.ts +++ b/frontend/src/lib/utils/breadcrumbs.ts @@ -15,16 +15,27 @@ const homeCrumb: Breadcrumb = { label: m.home(), href: '/', icon: 'fa-regular fa const createBreadcrumbs = (initialValue: Breadcrumb[]) => { const breadcrumbs = writable(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]); }); }