Skip to content

Commit 445e147

Browse files
committed
[B][E] Adjust library mobile breadcrumbs
1 parent ceda3ec commit 445e147

File tree

2 files changed

+77
-75
lines changed

2 files changed

+77
-75
lines changed

client/src/global/components/navigation/Mobile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ export class NavigationMobile extends Component {
142142
}
143143
}
144144
});
145+
if (this.props.location.pathname === "/project-collections") {
146+
active.push("frontendProjects");
147+
}
145148
return active;
146149
}
147150

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,49 @@
1-
import React, { PureComponent } from "react";
21
import classNames from "classnames";
3-
import { matchPath } from "react-router-dom";
4-
import { NavLink } from "react-router-dom";
2+
import { useLocation, NavLink, matchPath } from "react-router-dom";
53
import PropTypes from "prop-types";
4+
import { useTranslation } from "react-i18next";
65
import lh from "helpers/linkHandler";
76
import IconComposer from "global/components/utility/IconComposer";
8-
import { withTranslation } from "react-i18next";
97

10-
class MobileBreadcrumb extends PureComponent {
11-
static propTypes = {
12-
links: PropTypes.array.isRequired,
13-
location: PropTypes.object.isRequired,
14-
t: PropTypes.func
8+
export default function MobileBreadcrumb({ links, journalIsActive }) {
9+
const location = useLocation();
10+
const { t } = useTranslation();
11+
12+
const pathForLink = link => {
13+
if (link.externalUrl) return link.externalUrl;
14+
const args = link.args || [];
15+
const route = link.linksTo || link.route;
16+
return lh.link(route, ...args);
17+
};
18+
19+
const match = (linksToMatch, exact = false) => {
20+
if (!linksToMatch) return null;
21+
return linksToMatch.find(link => {
22+
if (link.matchType === "link" || link.externalUrl || exact) {
23+
return location.pathname === pathForLink(link);
24+
}
25+
26+
if (
27+
location.pathname === "/project-collections" &&
28+
link.route === "frontendProjects"
29+
)
30+
return true;
31+
32+
// Check if this route is in the current matches by route name
33+
const routeMatch = matchPath(location.pathname, link);
34+
if (routeMatch) return true;
35+
36+
// Fallback: check if pathname starts with the link path
37+
const linkPath = pathForLink(link);
38+
return location.pathname.startsWith(linkPath);
39+
});
1540
};
1641

17-
get segments() {
18-
const journalIsActive = this.props.journalIsActive;
42+
const getSegments = () => {
1943
const segments = [];
2044
if (typeof journalIsActive !== "boolean") return segments;
2145

22-
const firstMatch = this.match(this.props.links);
46+
const firstMatch = match(links);
2347
/* eslint-disable no-nested-ternary */
2448
const first =
2549
journalIsActive && firstMatch
@@ -33,75 +57,50 @@ class MobileBreadcrumb extends PureComponent {
3357

3458
if (first) {
3559
segments.push(first);
36-
const second = this.match(first.children);
60+
const second = match(first.children, true);
3761
if (second) {
3862
segments.push(second);
3963
}
4064
}
4165
return segments;
42-
}
43-
44-
get isBackend() {
45-
return this.props.location.pathname.includes("backend");
46-
}
47-
48-
match(links) {
49-
if (!links) return null;
50-
return links.find(link => {
51-
const route = lh.routeFromName(link.route);
52-
53-
if (link.matchType === "link" || link.externalUrl) {
54-
return this.props.location.pathname === this.pathForLink(link);
55-
}
56-
return (
57-
matchPath(this.props.location.pathname, route) !== null ||
58-
this.props.location.pathname.startsWith(route.path)
59-
);
60-
});
61-
}
62-
63-
pathForLink(link) {
64-
if (link.externalUrl) return link.externalUrl;
65-
const args = link.args || [];
66-
const route = link.linksTo || link.route;
67-
return lh.link(route, ...args);
68-
}
66+
};
6967

70-
render() {
71-
let count = 0;
72-
const segments = this.segments;
73-
const size = segments.length;
68+
const isBackend = location.pathname.includes("backend");
69+
const segments = getSegments();
70+
const size = segments.length;
71+
let count = 0;
7472

75-
return (
76-
<nav
77-
className={classNames("breadcrumb-list", {
78-
"hide-100": this.isBackend,
79-
"hide-82": !this.isBackend
80-
})}
81-
>
82-
{this.segments.map(link => {
83-
count += 1;
84-
return (
85-
<span key={count}>
86-
<NavLink
87-
className="breadcrumb-list__link"
88-
to={this.pathForLink(link)}
89-
>
90-
{this.props.t(link.label)}
91-
</NavLink>
92-
{count < size && (
93-
<IconComposer
94-
icon="disclosureDown16"
95-
size="default"
96-
className="breadcrumb-list__icon"
97-
/>
98-
)}
99-
</span>
100-
);
101-
})}
102-
</nav>
103-
);
104-
}
73+
return (
74+
<nav
75+
className={classNames("breadcrumb-list", {
76+
"hide-100": isBackend,
77+
"hide-82": !isBackend
78+
})}
79+
>
80+
{segments.map(link => {
81+
count += 1;
82+
return (
83+
<span key={count}>
84+
<NavLink className="breadcrumb-list__link" to={pathForLink(link)}>
85+
{t(link.label)}
86+
</NavLink>
87+
{count < size && (
88+
<IconComposer
89+
icon="disclosureDown16"
90+
size="default"
91+
className="breadcrumb-list__icon"
92+
/>
93+
)}
94+
</span>
95+
);
96+
})}
97+
</nav>
98+
);
10599
}
106100

107-
export default withTranslation()(MobileBreadcrumb);
101+
MobileBreadcrumb.displayName = "Navigation.Mobile.Breadcrumb";
102+
103+
MobileBreadcrumb.propTypes = {
104+
links: PropTypes.array.isRequired,
105+
journalIsActive: PropTypes.bool
106+
};

0 commit comments

Comments
 (0)