Skip to content

Commit 2df76dc

Browse files
yanggggjieovflowd
andauthored
Fix breadcrumbs not updating (#6431)
Co-authored-by: Claudio Wunder <[email protected]>
1 parent 507fc92 commit 2df76dc

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

components/Common/Breadcrumbs/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import BreadcrumbRoot from '@/components/Common/Breadcrumbs/BreadcrumbRoot';
88
import BreadcrumbTruncatedItem from '@/components/Common/Breadcrumbs/BreadcrumbTruncatedItem';
99
import type { FormattedMessage } from '@/types';
1010

11-
type BreadcrumbLink = {
11+
export type BreadcrumbLink = {
1212
label: FormattedMessage;
1313
href: string | undefined;
1414
};

components/withBreadcrumbs.tsx

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
import type { FC } from 'react';
44

5+
import type { BreadcrumbLink } from '@/components/Common/Breadcrumbs';
56
import Breadcrumbs from '@/components/Common/Breadcrumbs';
67
import { useClientContext, useMediaQuery, useSiteNavigation } from '@/hooks';
78
import type { NavigationKeys } from '@/types';
9+
import { dashToCamelCase } from '@/util/stringUtils';
810

911
const WithBreadcrumbs: FC = () => {
1012
const { navigationItems, getSideNavigation } = useSiteNavigation();
1113
const { pathname } = useClientContext();
12-
1314
const isMobileScreen = useMediaQuery('(max-width: 639px)');
1415

16+
const maxLength = isMobileScreen ? 2 : 4;
17+
1518
const getBreadrumbs = () => {
1619
const [navigationKey] =
1720
navigationItems.find(([, item]) => pathname.includes(item.link)) || [];
@@ -20,16 +23,36 @@ const WithBreadcrumbs: FC = () => {
2023
return [];
2124
}
2225

23-
return getSideNavigation([navigationKey as NavigationKeys])
24-
.map(([, item]) => item.items)
25-
.flat()
26-
.filter(([, item]) => pathname.includes(item.link))
27-
.map(([, item]) => ({ label: item.label, href: item.link }));
26+
const navigationTree = getSideNavigation([navigationKey as NavigationKeys]);
27+
28+
const pathList = pathname
29+
.split('/')
30+
.filter(item => item !== '')
31+
.map(dashToCamelCase);
32+
33+
let currentNode = navigationTree;
34+
35+
// Reduce the pathList to a breadcrumbs array by finding each path in the current navigation layer,
36+
// updating the currentNode to the found node's items(next layer) for the next iteration.
37+
return pathList.reduce((breadcrumbs, path) => {
38+
const nodeWithCurrentPath = currentNode.find(
39+
([nodePath]) => nodePath === path
40+
);
41+
42+
if (nodeWithCurrentPath) {
43+
const [, { label, link = '', items = [] }] = nodeWithCurrentPath;
44+
45+
// Goes deeper on the tree of items if there are any.
46+
currentNode = items;
47+
48+
return label ? [...breadcrumbs, { label, href: link }] : breadcrumbs;
49+
}
50+
51+
return breadcrumbs;
52+
}, [] as Array<BreadcrumbLink>);
2853
};
2954

30-
return (
31-
<Breadcrumbs links={getBreadrumbs()} maxLength={isMobileScreen ? 2 : 4} />
32-
);
55+
return <Breadcrumbs links={getBreadrumbs()} maxLength={maxLength} />;
3356
};
3457

3558
export default WithBreadcrumbs;

util/stringUtils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,8 @@ export const parseRichTextIntoPlainText = (richText: string) =>
2121
.replace(/^[ ]+|[ ]+$/gm, '')
2222
// replaces leading numbers and dots from each line with an empty string
2323
.replace(/^\d+\.\s/gm, '');
24+
25+
export const dashToCamelCase = (str: string) =>
26+
str
27+
.replace(/-([a-z])/g, (match, chr) => chr.toUpperCase())
28+
.replace(/^[A-Z]/, chr => chr.toLowerCase());

0 commit comments

Comments
 (0)