keycloak-scim/src/components/bread-crumb/PageBreadCrumbs.tsx

40 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-03-05 10:18:47 +00:00
import React, { isValidElement } from "react";
import { Link } from "react-router-dom";
2021-03-05 10:18:47 +00:00
import useBreadcrumbs, { BreadcrumbData } from "use-react-router-breadcrumbs";
import { useTranslation } from "react-i18next";
2021-03-05 10:18:47 +00:00
import _ from "lodash";
import { Breadcrumb, BreadcrumbItem } from "@patternfly/react-core";
import { useRealm } from "../../context/realm-context/RealmContext";
import { routes } from "../../route-config";
import { GroupBreadCrumbs } from "./GroupBreadCrumbs";
export const PageBreadCrumbs = () => {
const { t } = useTranslation();
const { realm } = useRealm();
2021-03-05 10:18:47 +00:00
const elementText = (crumb: BreadcrumbData) =>
isValidElement(crumb.breadcrumb) && crumb.breadcrumb.props.children;
const crumbs = _.uniqBy(
useBreadcrumbs(routes(t), {
excludePaths: ["/", `/${realm}`],
}),
elementText
);
return (
<>
{crumbs.length > 1 && (
<Breadcrumb>
{crumbs.map(({ match, breadcrumb: crumb }, i) => (
<BreadcrumbItem key={i} isActive={crumbs.length - 1 === i}>
{crumbs.length - 1 !== i && <Link to={match.url}>{crumb}</Link>}
{crumbs.length - 1 === i && <>{crumb}</>}
</BreadcrumbItem>
))}
</Breadcrumb>
)}
<GroupBreadCrumbs />
</>
);
};