-
Notifications
You must be signed in to change notification settings - Fork 92
/
Breadcrumbs.tsx
139 lines (122 loc) · 3.8 KB
/
Breadcrumbs.tsx
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { removeUndefined, TGetStaticProps, TProductCategory } from '@cromwell/core';
import { useAppPropsContext } from '@cromwell/core-frontend';
import clsx from 'clsx';
import React from 'react';
import styles from './Breadcrumbs.module.scss';
import { Crumb } from './Crumb';
import { DefaultHomeIcon, DefaultWrapper } from './DefaultElements';
import { breadcrumbsGetData } from './getData';
export type BreadcrumbsData =
| {
categories?: TProductCategory[] | undefined;
}
| undefined
| null;
/** @internal */
type GetStaticPropsData = {
ccom_breadcrumbs?: BreadcrumbsData;
};
export type BreadcrumbsProps = {
classes?: Partial<Record<'root' | 'breadcrumb' | 'link', string>>;
elements?: {
Wrapper?: React.ComponentType<{
id?: string;
className?: string;
style?: React.CSSProperties;
maxItems?: number;
children?: React.ReactNode;
}>;
Breadcrumb?: React.ComponentType<{
className?: string;
id?: string;
label?: string;
style?: React.CSSProperties;
icon?: React.ReactNode | null;
children?: React.ReactNode | null;
}>;
HomeIcon?: React.ComponentType;
};
text?: {
home?: string;
};
/**
* Override data by manually calling `getData` function and passing its result
*/
data?: BreadcrumbsData;
/**
* Max breadcrumb items. Currently is not implemented by base component. Can be implemented
* by wrappers. Implemented by MuiBreadcrumbs.
*/
maxItems?: number;
/**
* Show first breadcrumb as a link to home page `/`
*/
showHome?: boolean;
/**
* Custom link resolver for each breadcrumb to category page.
*/
getBreadcrumbLink?: (crumb: TProductCategory) => string | undefined;
};
/**
* Represents breadcrumbs of categories on a product page.
*
* - `withGetProps` - optional or use getData
* - `getData` - available
* - `useData` - available
*/
export function Breadcrumbs(props: BreadcrumbsProps) {
const { maxItems, classes, elements, text, showHome } = props;
const appProps = useAppPropsContext<GetStaticPropsData>();
const data: BreadcrumbsData = Object.assign({}, appProps.pageProps?.ccom_breadcrumbs, props.data);
const Wrapper = elements?.Wrapper ?? DefaultWrapper;
const HomeIcon = elements?.HomeIcon ?? DefaultHomeIcon;
return (
<Wrapper maxItems={maxItems ?? 5} className={clsx(styles.Breadcrumbs, classes?.root)}>
{showHome && (
<Crumb
key="home"
link={'/'}
breadcrumbsProps={props}
icon={<HomeIcon />}
crumb={{
id: 0,
name: text?.home ?? 'Home',
}}
/>
)}
{data?.categories?.map((crumb) => (
<Crumb key={crumb.id} breadcrumbsProps={props} crumb={crumb} />
))}
</Wrapper>
);
}
/** @internal */
Breadcrumbs.withGetProps = (originalGetProps?: TGetStaticProps) => {
const getProps: TGetStaticProps<GetStaticPropsData> = async (context) => {
const originProps = (await originalGetProps?.(context)) ?? {};
const contextSlug = context?.params?.slug;
const slug = (contextSlug && typeof contextSlug === 'string' && contextSlug) || null;
return {
...originProps,
props: {
...(((originProps as any).props ?? {}) as Record<string, any>),
ccom_breadcrumbs:
removeUndefined(
slug &&
(await breadcrumbsGetData({ productSlug: slug }).catch((e) => {
console.error('Breadcrumbs getData error: ', e);
return null;
})),
) || null,
},
};
};
return getProps;
};
/** @internal */
Breadcrumbs.getData = (opts) => breadcrumbsGetData(opts);
/** @internal */
Breadcrumbs.useData = (): BreadcrumbsData => {
const appProps = useAppPropsContext<GetStaticPropsData>();
return appProps.pageProps?.ccom_breadcrumbs;
};