The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/public/app/core/components/Breadcrumbs/utils.ts

54 lines
1.5 KiB

import { NavModelItem } from '@grafana/data';
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, shouldDedupe = false) {
if (foundHome) {
return;
}
// construct the URL to match
const urlParts = node.url?.split('?') ?? ['', ''];
let urlToMatch = urlParts[0];
const urlSearchParams = new URLSearchParams(urlParts[1]);
if (urlSearchParams.has('editview')) {
urlToMatch += `?editview=${urlSearchParams.get('editview')}`;
}
// 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;
}
const isSamePathAsLastBreadcrumb = urlToMatch.length > 0 && lastPath === urlToMatch;
// Remember this path for the next breadcrumb
lastPath = urlToMatch;
const shouldAddCrumb = !node.hideFromBreadcrumbs && !(shouldDedupe && isSamePathAsLastBreadcrumb);
if (shouldAddCrumb) {
crumbs.unshift({ text: node.text, href: node.url ?? '' });
}
if (node.parentItem) {
addCrumbs(node.parentItem);
}
}
if (pageNav) {
addCrumbs(pageNav);
}
// shouldDedupe = true enables app plugins to control breadcrumbs of their root pages
addCrumbs(sectionNav, true);
return crumbs;
}