-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathindex.ts
183 lines (156 loc) · 5.32 KB
/
index.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import React, { useContext } from "react";
import warnOnce from "warn-once";
import { I18nContext } from "@contexts/i18n";
import { useRouterType } from "@contexts/router/picker";
import { pickNotDeprecated } from "@definitions";
import { getActionRoutesFromResource } from "@definitions/helpers/router";
import { composeRoute } from "@definitions/helpers/router/compose-route";
import { useRefineContext, useResource, useTranslate } from "@hooks";
import { useParsed } from "@hooks/router/use-parsed";
import type { IResourceItem } from "../../contexts/resource/types";
import { pickResource } from "../../definitions/helpers/pick-resource/index";
export type BreadcrumbsType = {
label: string;
href?: string;
icon?: React.ReactNode;
};
type UseBreadcrumbReturnType = {
breadcrumbs: BreadcrumbsType[];
};
type UseBreadcrumbProps = {
/**
* Additional params to be used in the route generation process.
*/
meta?: Record<string, string | number>;
};
export const useBreadcrumb = ({
meta: metaFromProps = {},
}: UseBreadcrumbProps = {}): UseBreadcrumbReturnType => {
const routerType = useRouterType();
const { i18nProvider } = useContext(I18nContext);
const parsed = useParsed();
const translate = useTranslate();
const { resources, resource, action } = useResource();
const {
options: { textTransformers },
} = useRefineContext();
const breadcrumbs: BreadcrumbsType[] = [];
if (!resource?.name) {
return { breadcrumbs };
}
const addBreadcrumb = (parentName: string | IResourceItem) => {
const parentResource =
typeof parentName === "string"
? pickResource(parentName, resources, routerType === "legacy") ?? {
name: parentName,
}
: parentName;
if (parentResource) {
const grandParentName = pickNotDeprecated(
parentResource?.meta?.parent,
parentResource?.parentName,
);
if (grandParentName) {
addBreadcrumb(grandParentName);
}
const listActionOfResource = getActionRoutesFromResource(
parentResource,
resources,
routerType === "legacy",
).find((r) => r.action === "list");
const hrefRaw = listActionOfResource?.resource?.list
? listActionOfResource?.route
: undefined;
const href = hrefRaw
? routerType === "legacy"
? hrefRaw
: composeRoute(hrefRaw, parentResource?.meta, parsed, metaFromProps)
: undefined;
// innner route params: /:id in main-route/:id/sub-route
if (hrefRaw) {
// [^/]+ --> any number of letters not `/`
// \/: ---> start with /:
// [^/]+ --> any number of letters not `/`
// \/ -- end with /
const regex = /[^/]+\/:[^/]+\//g;
const hrefRegexMatch = hrefRaw.match(regex);
if (hrefRegexMatch) {
const matches = hrefRegexMatch.map((match) => {
const end = hrefRaw.indexOf(match) + match.length;
return hrefRaw.slice(0, end);
});
if (matches && parsed.pathname) {
const lastMatch = matches.at(-1);
if (lastMatch) {
const pathnameSegments = parsed.pathname
.split("/")
.filter((segment) => segment);
const lastMatchSegments = lastMatch
.split("/")
.filter((segment) => segment);
const urlBits: string[] = [];
lastMatchSegments.forEach((segment, index) => {
let x = segment;
if (segment !== pathnameSegments[index]) {
x = pathnameSegments[index];
}
urlBits.push(x);
});
const label = urlBits.at(-1);
const href = [...urlBits].join("/");
breadcrumbs.push({ label: String(label), href });
}
}
}
}
breadcrumbs.push({
label:
pickNotDeprecated(
parentResource.meta?.label,
parentResource.options?.label,
) ??
translate(
`${parentResource.name}.${parentResource.name}`,
textTransformers.humanize(parentResource.name),
),
href: href,
icon: pickNotDeprecated(
parentResource.meta?.icon,
parentResource.options?.icon,
parentResource.icon,
),
});
}
};
addBreadcrumb(resource);
if (action && action !== "list") {
const key = `actions.${action}`;
const actionLabel = translate(key);
if (typeof i18nProvider !== "undefined" && actionLabel === key) {
warnOnce(
true,
`[useBreadcrumb]: Breadcrumb missing translate key for the "${action}" action. Please add "actions.${action}" key to your translation file.\nFor more information, see https://refine.dev/docs/api-reference/core/hooks/useBreadcrumb/#i18n-support`,
);
breadcrumbs.push({
label: translate(
`buttons.${action}`,
textTransformers.humanize(action),
),
});
} else {
breadcrumbs.push({
label: translate(key, textTransformers.humanize(action)),
});
}
// add action path param to breadcrum
if (parsed.id) {
breadcrumbs.push({
label: String(parsed.id),
href: parsed.pathname,
});
}
}
return {
breadcrumbs,
};
};