-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDynamicBreadcrumbs.tsx
More file actions
65 lines (58 loc) · 2.48 KB
/
Copy pathDynamicBreadcrumbs.tsx
File metadata and controls
65 lines (58 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from "react";
import { useLocation, Link, useMatch } from "react-router-dom";
import { Breadcrumbs } from "@equinor/eds-core-react";
import { useBreadcrumbStore } from "@/hooks/useBreadcrumbStore";
const DynamicBreadcrumbs: React.FC = () => {
const projectMatch = useMatch("/project/:projectId/*");
const projectId = projectMatch?.params?.projectId;
const simulationMatch = useMatch("/project/:projectId/simulation/:simulationId");
const simulationId = simulationMatch?.params?.simulationId;
const location = useLocation();
//TODO: need to fetch project and simulation names from db if state is empty
const projectName = useBreadcrumbStore((state) => state.projectName);
const simulationName = useBreadcrumbStore((state) => state.simulationName);
const getBreadcrumbs = () => {
const pathSegments = location.pathname.split("/").filter(Boolean);
const breadcrumbs = [];
breadcrumbs.push(
<Breadcrumbs.Breadcrumb key="home" as={Link} to={`/`}>
Home
</Breadcrumbs.Breadcrumb>
);
if (pathSegments.includes("project")) {
breadcrumbs.push(
<Breadcrumbs.Breadcrumb key="project" as={Link} to={`/project/${projectId}`}>
{projectName ? `Project: ${projectName}` : `Project: ${(projectId ?? "").slice(0, 8)}`}
</Breadcrumbs.Breadcrumb>
);
}
if (pathSegments.includes("simulation") && simulationId) {
breadcrumbs.push(
<Breadcrumbs.Breadcrumb
key="simulation"
as={Link}
to={`/project/${projectId}/simulation/${simulationId}`}
>
{simulationName || `${(simulationId ?? "").slice(0, 8)}`}
</Breadcrumbs.Breadcrumb>
);
}
if (pathSegments.includes("models")) {
breadcrumbs.push(
<Breadcrumbs.Breadcrumb key="models" as={Link} to={`/models`}>
Models
</Breadcrumbs.Breadcrumb>
);
}
if (pathSegments.includes("labresults")) {
breadcrumbs.push(
<Breadcrumbs.Breadcrumb key="labresults" as={Link} to={`/labresults`}>
Lab results
</Breadcrumbs.Breadcrumb>
);
}
return breadcrumbs;
};
return <Breadcrumbs>{getBreadcrumbs()}</Breadcrumbs>;
};
export default DynamicBreadcrumbs;