grafana/public/app/core/components/Page/usePageTitle.ts
Ashley Harrison 824a562b03
Navigation: share logic between buildBreadcrumbs and usePageTitle (#58819)
* simplify usePageTitle logic a bit

* use buildBreadcrumbs logic in usePageTitle

* always add home item to navTree, fix some tests

* fix remaining unit tests
2022-11-22 16:48:07 +00:00

24 lines
890 B
TypeScript

import { useEffect } from 'react';
import { NavModel, NavModelItem } from '@grafana/data';
import { HOME_NAV_ID } from 'app/core/reducers/navModel';
import { useSelector } from 'app/types';
import { Branding } from '../Branding/Branding';
import { buildBreadcrumbs } from '../Breadcrumbs/utils';
export function usePageTitle(navModel?: NavModel, pageNav?: NavModelItem) {
const homeNav = useSelector((state) => state.navIndex)[HOME_NAV_ID];
useEffect(() => {
const sectionNav = (navModel?.node !== navModel?.main ? navModel?.node : navModel?.main) ?? { text: 'Grafana' };
const parts: string[] = buildBreadcrumbs(sectionNav, pageNav, homeNav)
.map((crumb) => crumb.text)
.reverse();
// Override `Home` with the custom brand title
parts[parts.length - 1] = Branding.AppTitle;
document.title = parts.join(' - ');
}, [homeNav, navModel, pageNav]);
}