From cd99bfec6885c0ccea093dbf62917368b92a8443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 23 Jan 2023 11:03:28 +0100 Subject: [PATCH] TopNav: Fixes breadcrumb issues and title for apps when topnav is disabled (#61835) --- .../core/components/Breadcrumbs/utils.test.ts | 17 +++++++++++++++++ public/app/core/components/Breadcrumbs/utils.ts | 14 +++++++++++++- .../features/plugins/components/AppRootPage.tsx | 3 ++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/public/app/core/components/Breadcrumbs/utils.test.ts b/public/app/core/components/Breadcrumbs/utils.test.ts index c696d65a4d3..4f67743c2a6 100644 --- a/public/app/core/components/Breadcrumbs/utils.test.ts +++ b/public/app/core/components/Breadcrumbs/utils.test.ts @@ -144,5 +144,22 @@ describe('breadcrumb utils', () => { { text: 'My page', href: '/my-page' }, ]); }); + + it('Should add breadcrumbs for child pages that have not set parentItem', () => { + const pageNav: NavModelItem = { + text: 'My page', + url: '/my-page', + children: [ + { text: 'A', url: '/a', active: true }, + { text: 'B', url: '/b' }, + ], + }; + + expect(buildBreadcrumbs(mockHomeNav, pageNav, mockHomeNav)).toEqual([ + { text: 'Home', href: '/home' }, + { text: 'My page', href: '/my-page' }, + { text: 'A', href: '/a' }, + ]); + }); }); }); diff --git a/public/app/core/components/Breadcrumbs/utils.ts b/public/app/core/components/Breadcrumbs/utils.ts index cbf68fefb50..4fe8177a25f 100644 --- a/public/app/core/components/Breadcrumbs/utils.ts +++ b/public/app/core/components/Breadcrumbs/utils.ts @@ -16,6 +16,7 @@ export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelIte if (urlSearchParams.has('editview')) { urlToMatch += `?editview=${urlSearchParams.get('editview')}`; } + if (!foundHome && !node.hideFromBreadcrumbs) { if (homeNav && urlToMatch === homeNav.url) { crumbs.unshift({ text: getNavTitle(homeNav.id) ?? homeNav.text, href: node.url ?? '' }); @@ -31,7 +32,18 @@ export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelIte } if (pageNav) { - addCrumbs(pageNav); + if (pageNav.url && pageNav.children) { + const child = pageNav.children.find((child) => child.active); + if (child) { + addCrumbs(child); + // Some pages set up children but they are not connected to parent pageNav + if (child.parentItem !== pageNav) { + addCrumbs(pageNav); + } + } + } else { + addCrumbs(pageNav); + } } addCrumbs(sectionNav); diff --git a/public/app/features/plugins/components/AppRootPage.tsx b/public/app/features/plugins/components/AppRootPage.tsx index 0c19ca05dc9..d5b18275907 100644 --- a/public/app/features/plugins/components/AppRootPage.tsx +++ b/public/app/features/plugins/components/AppRootPage.tsx @@ -112,7 +112,8 @@ const stateSlice = createSlice({ ...pluginNav, node: { ...pluginNav.main, - hideFromBreadcrumbs: true, + // Because breadcumbs code is also used to set title when topnav should only set hideFromBreadcrumbs when topnav is enabled + hideFromBreadcrumbs: config.featureToggles.topnav, }, }; }