Breadcrumbs: Only dedupe breacrumb items for matching node names (#78077)

only dedupe breactumb items for matching node names
pull/78279/head
Gilles De Mey 2 years ago committed by GitHub
parent 0e5ce50b90
commit 2659409191
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 23
      public/app/core/components/Breadcrumbs/utils.test.ts
  2. 7
      public/app/core/components/Breadcrumbs/utils.ts

@ -141,5 +141,28 @@ describe('breadcrumb utils', () => {
{ text: 'My page', href: '/my-page' },
]);
});
it('does not ignore duplicates with different text', () => {
const pageNav: NavModelItem = {
text: 'My page',
url: '/my-page',
parentItem: {
text: 'Another section',
// same url as section nav, but this one should win/overwrite it
url: '/my-section?from=1h&to=now',
},
};
const sectionNav: NavModelItem = {
text: 'My section',
url: '/my-section',
};
expect(buildBreadcrumbs(sectionNav, pageNav, mockHomeNav)).toEqual([
{ text: 'My section', href: '/my-section' },
{ text: 'Another section', href: '/my-section?from=1h&to=now' },
{ text: 'My page', href: '/my-page' },
]);
});
});
});

@ -7,6 +7,7 @@ export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelIte
const crumbs: Breadcrumb[] = [];
let foundHome = false;
let lastPath: string | undefined = undefined;
let lastText: string | undefined = undefined;
function addCrumbs(node: NavModelItem) {
if (foundHome) {
@ -33,10 +34,14 @@ export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelIte
// This enabled app plugins to control breadcrumbs of their root pages
const isSamePathAsLastBreadcrumb = urlToMatch.length > 0 && lastPath === urlToMatch;
const isSameTextAsLastBreadcrumb = node.text === lastText;
// Remember this path for the next breadcrumb
lastPath = urlToMatch;
lastText = node.text;
if (!node.hideFromBreadcrumbs && !isSamePathAsLastBreadcrumb) {
const shouldMergeBreadcrumb = isSamePathAsLastBreadcrumb && isSameTextAsLastBreadcrumb;
if (!node.hideFromBreadcrumbs && !shouldMergeBreadcrumb) {
crumbs.unshift({ text: node.text, href: node.url ?? '' });
}

Loading…
Cancel
Save