2022-06-28 08:47:12 -05:00
|
|
|
import { css } from '@emotion/css';
|
|
|
|
import React from 'react';
|
|
|
|
|
|
|
|
import { GrafanaTheme2, NavModelItem } from '@grafana/data';
|
|
|
|
import { useStyles2, Icon, IconName } from '@grafana/ui';
|
|
|
|
|
2022-07-06 10:00:56 -05:00
|
|
|
export interface Props {
|
2022-06-28 08:47:12 -05:00
|
|
|
sectionNav: NavModelItem;
|
2022-07-06 10:00:56 -05:00
|
|
|
pageNav?: NavModelItem;
|
2022-06-28 08:47:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface Breadcrumb {
|
|
|
|
text?: string;
|
|
|
|
icon?: IconName;
|
|
|
|
href?: string;
|
|
|
|
}
|
|
|
|
|
2022-07-06 10:00:56 -05:00
|
|
|
export function Breadcrumbs({ sectionNav, pageNav }: Props) {
|
2022-06-28 08:47:12 -05:00
|
|
|
const styles = useStyles2(getStyles);
|
2022-07-06 10:00:56 -05:00
|
|
|
const crumbs: Breadcrumb[] = [{ icon: 'home-alt', href: '/' }];
|
2022-06-28 08:47:12 -05:00
|
|
|
|
|
|
|
function addCrumbs(node: NavModelItem) {
|
|
|
|
if (node.parentItem) {
|
|
|
|
addCrumbs(node.parentItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
crumbs.push({ text: node.text, href: node.url });
|
|
|
|
}
|
|
|
|
|
|
|
|
addCrumbs(sectionNav);
|
|
|
|
|
2022-07-06 10:00:56 -05:00
|
|
|
if (pageNav) {
|
|
|
|
addCrumbs(pageNav);
|
2022-06-28 08:47:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className={styles.breadcrumbs}>
|
|
|
|
{crumbs.map((breadcrumb, index) => (
|
|
|
|
<li className={styles.breadcrumb} key={index}>
|
|
|
|
{breadcrumb.href && (
|
|
|
|
<a className={styles.breadcrumbLink} href={breadcrumb.href}>
|
|
|
|
{breadcrumb.text}
|
|
|
|
{breadcrumb.icon && <Icon name={breadcrumb.icon} />}
|
|
|
|
</a>
|
|
|
|
)}
|
|
|
|
{!breadcrumb.href && <span className={styles.breadcrumbLink}>{breadcrumb.text}</span>}
|
|
|
|
{index + 1 < crumbs.length && (
|
|
|
|
<div className={styles.separator}>
|
|
|
|
<Icon name="angle-right" />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const getStyles = (theme: GrafanaTheme2) => {
|
|
|
|
return {
|
|
|
|
breadcrumbs: css({
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
2022-07-14 13:34:00 -05:00
|
|
|
flexWrap: 'nowrap',
|
2022-06-28 08:47:12 -05:00
|
|
|
fontWeight: theme.typography.fontWeightMedium,
|
|
|
|
}),
|
|
|
|
breadcrumb: css({
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
}),
|
|
|
|
separator: css({
|
|
|
|
color: theme.colors.text.secondary,
|
|
|
|
padding: theme.spacing(0, 0.5),
|
|
|
|
}),
|
|
|
|
breadcrumbLink: css({
|
|
|
|
color: theme.colors.text.primary,
|
2022-07-14 13:34:00 -05:00
|
|
|
whiteSpace: 'nowrap',
|
2022-06-28 08:47:12 -05:00
|
|
|
'&:hover': {
|
|
|
|
textDecoration: 'underline',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
};
|