Files
Checkmate/client/src/Components/design-elements/Breadcrumb.tsx
T

140 lines
3.3 KiB
TypeScript
Raw Normal View History

2026-01-23 19:58:37 +00:00
import MuiBreadcrumbs from "@mui/material/Breadcrumbs";
import Typography from "@mui/material/Typography";
import { Link, useLocation } from "react-router-dom";
import { useTheme } from "@mui/material/styles";
2026-01-24 20:41:21 +00:00
import { ChevronRight } from "lucide-react";
2026-01-29 21:08:36 +00:00
import { useTranslation } from "react-i18next";
2026-02-02 21:57:07 +00:00
import type { ReactNode } from "react";
2026-01-23 19:58:37 +00:00
2026-01-24 20:41:21 +00:00
const isId = (segment: string): boolean => {
return segment.length === 24 || /^[a-f0-9-]{36}$/.test(segment);
};
2026-01-29 21:08:26 +00:00
const actionSegments = ["create", "configure"];
2026-02-02 21:57:07 +00:00
const BreadcrumbWrapper = ({ children }: { children: ReactNode }) => {
const theme = useTheme();
return (
<MuiBreadcrumbs
separator={
<ChevronRight
size={16}
strokeWidth={1.5}
/>
}
sx={{
fontSize: "14px",
marginBottom: theme.spacing(6),
"& .MuiBreadcrumbs-separator": {
color: theme.palette.text.secondary,
},
}}
>
{children}
</MuiBreadcrumbs>
);
};
2026-02-02 21:51:06 +00:00
export const Breadcrumb = ({
breadcrumbOverride,
}: {
breadcrumbOverride?: string[] | undefined;
}) => {
2026-01-29 21:08:36 +00:00
const { t } = useTranslation();
2026-01-23 19:58:37 +00:00
const theme = useTheme();
const location = useLocation();
2026-02-02 21:51:06 +00:00
// If override is an empty array, hide entirely
if (breadcrumbOverride !== undefined && breadcrumbOverride.length === 0) {
return null;
}
// If override has items, render them directly
if (breadcrumbOverride !== undefined && breadcrumbOverride.length > 0) {
return (
2026-02-02 21:57:07 +00:00
<BreadcrumbWrapper>
2026-02-02 21:51:06 +00:00
{breadcrumbOverride.map((item, index) => {
const isLast = index === breadcrumbOverride.length - 1;
return (
<Typography
key={index}
sx={{
fontSize: "14px",
fontWeight: isLast ? 600 : 400,
color: isLast ? theme.palette.primary.main : theme.palette.text.secondary,
}}
>
{item}
</Typography>
);
})}
2026-02-02 21:57:07 +00:00
</BreadcrumbWrapper>
2026-02-02 21:51:06 +00:00
);
}
// Default behavior: use location pathname
2026-01-24 20:41:21 +00:00
const segments = location.pathname.split("/").filter((x) => x);
2026-01-23 19:58:37 +00:00
2026-02-02 21:51:06 +00:00
if (segments.length === 0) {
return null;
}
2026-01-29 21:08:26 +00:00
// Build simplified breadcrumb: "uptime" or "uptime / details" or "uptime / create"
2026-01-29 21:08:36 +00:00
const basePage = segments[0] || t("common.breadcrumbs.home");
2026-01-29 21:08:26 +00:00
const secondSegment = segments[1];
2026-02-02 21:57:07 +00:00
const isActionPage = secondSegment && actionSegments.includes(secondSegment);
const isDetailsPage = secondSegment && isId(secondSegment);
2026-01-29 21:08:26 +00:00
const hasSubPage = isActionPage || isDetailsPage;
const getSubPageLabel = (): string => {
if (isActionPage) {
return secondSegment.charAt(0).toUpperCase() + secondSegment.slice(1);
}
2026-01-29 21:08:36 +00:00
return t("common.breadcrumbs.details");
2026-01-29 21:08:26 +00:00
};
2026-01-23 19:58:37 +00:00
return (
2026-02-02 21:57:07 +00:00
<BreadcrumbWrapper>
2026-01-29 21:08:26 +00:00
{hasSubPage ? (
2026-01-24 20:41:21 +00:00
<Link
to={`/${basePage}`}
style={{ textDecoration: "none" }}
2026-01-23 19:58:37 +00:00
>
<Typography
sx={{
fontSize: "14px",
2026-01-24 20:41:21 +00:00
color: theme.palette.text.secondary,
"&:hover": {
color: theme.palette.primary.main,
},
2026-01-23 19:58:37 +00:00
}}
>
2026-01-24 20:41:21 +00:00
{basePage.charAt(0).toUpperCase() + basePage.slice(1)}
2026-01-23 19:58:37 +00:00
</Typography>
2026-01-24 20:41:21 +00:00
</Link>
) : (
<Typography
sx={{
fontSize: "14px",
fontWeight: 600,
color: theme.palette.primary.main,
}}
>
{basePage.charAt(0).toUpperCase() + basePage.slice(1)}
</Typography>
)}
2026-01-29 21:08:26 +00:00
{hasSubPage && (
2026-01-24 20:41:21 +00:00
<Typography
sx={{
fontSize: "14px",
fontWeight: 600,
color: theme.palette.primary.main,
}}
>
2026-01-29 21:08:26 +00:00
{getSubPageLabel()}
2026-01-24 20:41:21 +00:00
</Typography>
)}
2026-02-02 21:57:07 +00:00
</BreadcrumbWrapper>
2026-01-23 19:58:37 +00:00
);
};