-
-
Notifications
You must be signed in to change notification settings - Fork 47k
/
useMenu.tsx
138 lines (130 loc) · 4.35 KB
/
useMenu.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
import React, { ReactNode, useMemo } from 'react';
import { MenuProps } from 'antd';
import { Link, useFullSidebarData, useSidebarData } from 'dumi';
import useLocation from './useLocation';
export type UseMenuOptions = {
before?: ReactNode;
after?: ReactNode;
};
const useMenu = (options: UseMenuOptions = {}): [MenuProps['items'], string] => {
const fullData = useFullSidebarData();
const { pathname } = useLocation();
const sidebarData = useSidebarData();
const { before, after } = options;
const menuItems = useMemo<MenuProps['items']>(() => {
const sidebarItems = [...(sidebarData ?? [])];
// 将设计文档未分类的放在最后
if (pathname.startsWith('/docs/spec')) {
const notGrouped = sidebarItems.splice(0, 1);
sidebarItems.push(...notGrouped);
}
// 把 /changelog 拼到开发文档中
if (pathname.startsWith('/docs/react')) {
const changelogData = Object.entries(fullData).find(([key]) =>
key.startsWith('/changelog'),
)?.[1];
if (changelogData) {
sidebarItems.push(...changelogData);
}
}
if (pathname.startsWith('/changelog')) {
const reactDocData = Object.entries(fullData).find(([key]) =>
key.startsWith('/docs/react'),
)?.[1];
if (reactDocData) {
sidebarItems.unshift(...reactDocData);
}
}
return (
sidebarItems?.reduce<Exclude<MenuProps['items'], undefined>>((result, group) => {
if (group.title) {
// 设计文档特殊处理二级分组
if (pathname.startsWith('/docs/spec')) {
const childrenGroup = group.children.reduce<
Record<string, ReturnType<typeof useSidebarData>[number]['children']>
>((childrenResult, child) => {
const type = (child.frontmatter as any).type ?? 'default';
if (!childrenResult[type]) {
childrenResult[type] = [];
}
childrenResult[type].push(child);
return childrenResult;
}, {});
const childItems = [];
childItems.push(
...childrenGroup.default.map((item) => ({
label: (
<Link to={item.link}>
{before}
{item.title}
{after}
</Link>
),
key: item.link.replace(/(-cn$)/g, ''),
})),
);
Object.entries(childrenGroup).forEach(([type, children]) => {
if (type !== 'default') {
childItems.push({
type: 'group',
label: type,
key: type,
children: children?.map((item) => ({
label: (
<Link to={item.link}>
{before}
{item.title}
{after}
</Link>
),
key: item.link.replace(/(-cn$)/g, ''),
})),
});
}
});
result.push({
label: group.title,
key: group.title,
children: childItems,
});
} else {
result.push({
type: 'group',
label: group.title,
key: group.title,
children: group.children?.map((item) => ({
label: (
<Link to={item.link}>
{before}
<span key="english">{item.title}</span>
<span className="chinese" key="chinese">
{(item.frontmatter as any).subtitle}
</span>
{after}
</Link>
),
key: item.link.replace(/(-cn$)/g, ''),
})),
});
}
} else {
result.push(
...group.children?.map((item) => ({
label: (
<Link to={item.link}>
{before}
{item.title}
{after}
</Link>
),
key: item.link.replace(/(-cn$)/g, ''),
})),
);
}
return result;
}, []) ?? []
);
}, [sidebarData, fullData, pathname]);
return [menuItems, pathname];
};
export default useMenu;