mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
Breadcrumbs: Enable plugins to override breadcrumbs that are generated by pages defined in plugin.json (#75218)
* Breadcrumbs: Make it more flexible for plugins to override items defined in plugin.json * only check when we have a url * better fix
This commit is contained in:
parent
d531f5ab42
commit
115038a35b
@ -144,5 +144,27 @@ describe('breadcrumb utils', () => {
|
||||
{ text: 'My page', href: '/my-page' },
|
||||
]);
|
||||
});
|
||||
|
||||
it('does ignore duplicates', () => {
|
||||
const pageNav: NavModelItem = {
|
||||
text: 'My page',
|
||||
url: '/my-page',
|
||||
parentItem: {
|
||||
text: 'My 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?from=1h&to=now' },
|
||||
{ text: 'My page', href: '/my-page' },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -5,23 +5,38 @@ import { Breadcrumb } from './types';
|
||||
export function buildBreadcrumbs(sectionNav: NavModelItem, pageNav?: NavModelItem, homeNav?: NavModelItem) {
|
||||
const crumbs: Breadcrumb[] = [];
|
||||
let foundHome = false;
|
||||
let lastPath: string | undefined = undefined;
|
||||
|
||||
function addCrumbs(node: NavModelItem) {
|
||||
if (foundHome) {
|
||||
return;
|
||||
}
|
||||
|
||||
// construct the URL to match
|
||||
// we want to ignore query params except for the editview query param
|
||||
const urlSearchParams = new URLSearchParams(node.url?.split('?')[1]);
|
||||
let urlToMatch = `${node.url?.split('?')[0]}`;
|
||||
const urlParts = node.url?.split('?') ?? ['', ''];
|
||||
let urlToMatch = urlParts[0];
|
||||
|
||||
const urlSearchParams = new URLSearchParams(urlParts[1]);
|
||||
|
||||
if (urlSearchParams.has('editview')) {
|
||||
urlToMatch += `?editview=${urlSearchParams.get('editview')}`;
|
||||
}
|
||||
|
||||
if (!foundHome && !node.hideFromBreadcrumbs) {
|
||||
if (homeNav && urlToMatch === homeNav.url) {
|
||||
crumbs.unshift({ text: homeNav.text, href: node.url ?? '' });
|
||||
foundHome = true;
|
||||
} else {
|
||||
crumbs.unshift({ text: node.text, href: node.url ?? '' });
|
||||
}
|
||||
// This enabled app plugins to control breadcrumbs of their root pages
|
||||
const isSamePathAsLastBreadcrumb = urlToMatch.length > 0 && lastPath === urlToMatch;
|
||||
// Remember this path for the next breadcrumb
|
||||
lastPath = urlToMatch;
|
||||
|
||||
// Check if we found home/root if if so return early
|
||||
if (homeNav && urlToMatch === homeNav.url) {
|
||||
crumbs.unshift({ text: homeNav.text, href: node.url ?? '' });
|
||||
foundHome = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!node.hideFromBreadcrumbs && !isSamePathAsLastBreadcrumb) {
|
||||
crumbs.unshift({ text: node.text, href: node.url ?? '' });
|
||||
}
|
||||
|
||||
if (node.parentItem) {
|
||||
|
Loading…
Reference in New Issue
Block a user