2022-11-30 11:05:41 +08:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
import React, { useMemo } from 'react';
|
|
|
|
import type { MenuProps } from 'antd';
|
2022-11-09 12:28:04 +08:00
|
|
|
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();
|
2022-11-23 13:48:28 +08:00
|
|
|
const { pathname, search } = useLocation();
|
2022-11-09 12:28:04 +08:00
|
|
|
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) => {
|
2022-12-01 12:21:42 +08:00
|
|
|
if (group?.title) {
|
2022-11-09 12:28:04 +08:00
|
|
|
// 设计文档特殊处理二级分组
|
|
|
|
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(
|
2022-11-19 13:47:33 +08:00
|
|
|
...childrenGroup.default.map((item) => ({
|
2022-11-09 12:28:04 +08:00
|
|
|
label: (
|
2022-11-23 13:48:28 +08:00
|
|
|
<Link to={`${item.link}${search}`}>
|
2022-11-09 12:28:04 +08:00
|
|
|
{before}
|
2022-12-01 12:21:42 +08:00
|
|
|
{item?.title}
|
2022-11-09 12:28:04 +08:00
|
|
|
{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,
|
2022-11-19 13:47:33 +08:00
|
|
|
children: children?.map((item) => ({
|
2022-11-09 12:28:04 +08:00
|
|
|
label: (
|
2022-11-23 13:48:28 +08:00
|
|
|
<Link to={`${item.link}${search}`}>
|
2022-11-09 12:28:04 +08:00
|
|
|
{before}
|
2022-12-01 12:21:42 +08:00
|
|
|
{item?.title}
|
2022-11-09 12:28:04 +08:00
|
|
|
{after}
|
|
|
|
</Link>
|
|
|
|
),
|
|
|
|
key: item.link.replace(/(-cn$)/g, ''),
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
result.push({
|
2022-12-01 12:21:42 +08:00
|
|
|
label: group?.title,
|
|
|
|
key: group?.title,
|
2022-11-09 12:28:04 +08:00
|
|
|
children: childItems,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
result.push({
|
|
|
|
type: 'group',
|
2022-12-01 12:21:42 +08:00
|
|
|
label: group?.title,
|
|
|
|
key: group?.title,
|
2022-11-19 13:47:33 +08:00
|
|
|
children: group.children?.map((item) => ({
|
2022-11-09 12:28:04 +08:00
|
|
|
label: (
|
2022-11-23 13:48:28 +08:00
|
|
|
<Link to={`${item.link}${search}`}>
|
2022-11-09 12:28:04 +08:00
|
|
|
{before}
|
2022-12-01 12:21:42 +08:00
|
|
|
<span key="english">{item?.title}</span>
|
2022-11-09 12:28:04 +08:00
|
|
|
<span className="chinese" key="chinese">
|
|
|
|
{(item.frontmatter as any).subtitle}
|
|
|
|
</span>
|
|
|
|
{after}
|
|
|
|
</Link>
|
|
|
|
),
|
|
|
|
key: item.link.replace(/(-cn$)/g, ''),
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2022-12-08 16:46:48 +08:00
|
|
|
const list = group.children || [];
|
|
|
|
// 如果有 date 字段,我们就对其进行排序
|
|
|
|
if (list.every((info) => info?.frontmatter?.date)) {
|
|
|
|
list.sort((a, b) => (a.frontmatter.date > b.frontmatter.date ? -1 : 1));
|
|
|
|
}
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
result.push(
|
2022-12-08 16:46:48 +08:00
|
|
|
...list.map((item) => ({
|
2022-11-09 12:28:04 +08:00
|
|
|
label: (
|
2022-11-23 13:48:28 +08:00
|
|
|
<Link to={`${item.link}${search}`}>
|
2022-11-09 12:28:04 +08:00
|
|
|
{before}
|
2022-12-01 12:21:42 +08:00
|
|
|
{item?.title}
|
2022-11-09 12:28:04 +08:00
|
|
|
{after}
|
|
|
|
</Link>
|
|
|
|
),
|
|
|
|
key: item.link.replace(/(-cn$)/g, ''),
|
2022-12-08 16:46:48 +08:00
|
|
|
})),
|
2022-11-09 12:28:04 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, []) ?? []
|
|
|
|
);
|
2022-11-23 13:48:28 +08:00
|
|
|
}, [sidebarData, fullData, pathname, search]);
|
2022-11-09 12:28:04 +08:00
|
|
|
|
|
|
|
return [menuItems, pathname];
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useMenu;
|