Navigation: Fix finding the active nav item for plugins (#62123)

fix: look for the active page under children first
This commit is contained in:
Levente Balogh 2023-01-26 15:10:09 +01:00 committed by GitHub
parent 75ffbe422b
commit 995e2715ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -69,6 +69,13 @@ describe('buildPluginSectionNav', () => {
expect(result?.node.text).toBe('page2'); expect(result?.node.text).toBe('page2');
}); });
it('Should only set the most specific match as active (not the parents)', () => {
config.featureToggles.topnav = true;
const result = buildPluginSectionNav(appsSection, null, '/a/plugin1/page2');
expect(result?.main.children![0].children![1].active).toBe(true);
expect(result?.main.children![0].active).not.toBe(true); // Parent should not be active
});
it('Should set app section to active', () => { it('Should set app section to active', () => {
config.featureToggles.topnav = true; config.featureToggles.topnav = true;
const result = buildPluginSectionNav(appsSection, null, '/a/plugin1'); const result = buildPluginSectionNav(appsSection, null, '/a/plugin1');

View File

@ -49,6 +49,8 @@ export function buildPluginSectionNav(
return page; return page;
} }
// Check if there is already an active page found with with a more specific url (possibly a child of the current page)
// (In this case we bail out early and don't mark the parent as active)
if (activePage && (activePage.url?.length ?? 0) > (page.url?.length ?? 0)) { if (activePage && (activePage.url?.length ?? 0) > (page.url?.length ?? 0)) {
return page; return page;
} }
@ -58,15 +60,20 @@ export function buildPluginSectionNav(
} }
activePage = { ...page, active: true }; activePage = { ...page, active: true };
return activePage; return activePage;
} }
// Find and set active page // Find and set active page
copiedPluginNavSection.children = (copiedPluginNavSection?.children ?? []).map((child) => { copiedPluginNavSection.children = (copiedPluginNavSection?.children ?? []).map((child) => {
if (child.children) { if (child.children) {
// Doing this here to make sure that first we check if any of the children is active
// (In case yes, then the check for the parent will not mark it as active)
const children = child.children.map((pluginPage) => setPageToActive(pluginPage, currentUrl));
return { return {
...setPageToActive(child, currentUrl), ...setPageToActive(child, currentUrl),
children: child.children.map((pluginPage) => setPageToActive(pluginPage, currentUrl)), children,
}; };
} }