Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update useMenu.tsx #47921

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
179 changes: 48 additions & 131 deletions .dumi/hooks/useMenu.tsx
@@ -1,13 +1,20 @@
import React, { useMemo } from 'react';
import type { MenuProps } from 'antd';
import { Tag, version } from 'antd';
import { MenuProps, Tag } from 'antd';
import { useFullSidebarData, useSidebarData } from 'dumi';

import Link from '../theme/common/Link';
import useLocation from './useLocation';

const ItemTag: React.FC<{ tag?: string; show?: boolean }> = (props) => {
const { tag, show = true } = props;
interface MenuItem {
label: React.ReactNode;
key: string;
}

interface UseMenuOptions {
before?: React.ReactNode;
after?: React.ReactNode;
}

const ItemTag: React.FC<{ tag?: string; show?: boolean }> = ({ tag, show = true }) => {

Check failure on line 17 in .dumi/hooks/useMenu.tsx

View workflow job for this annotation

GitHub Actions / lint

'ItemTag' is declared but its value is never read.
if (!show || !tag) {
return null;
}
Expand All @@ -17,153 +24,63 @@
color={tag === 'New' ? 'success' : 'processing'}
style={{ marginInlineStart: 'auto', marginInlineEnd: 0, marginTop: -2 }}
>
{tag.replace('VERSION', version)}
{tag.replace('VERSION', 'version')}
</Tag>
);
};

export interface UseMenuOptions {
before?: React.ReactNode;
after?: React.ReactNode;
}

const useMenu = (options: UseMenuOptions = {}): [MenuProps['items'], string] => {
const fullData = useFullSidebarData();
const { before, after } = options;
const { pathname, search } = useLocation();
const fullData = useFullSidebarData();
const sidebarData = useSidebarData();
const { before, after } = options;

const menuItems = useMemo<MenuProps['items']>(() => {
const sidebarItems = [...(sidebarData ?? [])];
const processMenuItem = (item: any): MenuItem => ({
label: (
<Link to={`${item.link}${search}`} style={{ display: 'flex', alignItems: 'center' }}>
{before}
{item?.title}
{after}
</Link>
),
key: item.link.replace(/(-cn$)/g, ''),
});

const processGroup = (group: any): MenuItem => ({
label: group?.title,
key: group?.title,
children: group.children?.map(processMenuItem),

Check failure on line 52 in .dumi/hooks/useMenu.tsx

View workflow job for this annotation

GitHub Actions / lint

Object literal may only specify known properties, and 'children' does not exist in type 'MenuItem'.
});

const menuItems = useMemo(() => {
let items: MenuItem[] = [];

// 将设计文档未分类的放在最后
if (pathname.startsWith('/docs/spec')) {
const notGrouped = sidebarItems.splice(0, 1);
sidebarItems.push(...notGrouped);
const notGrouped = sidebarData?.shift();
if (notGrouped) {
sidebarData?.push(notGrouped);
}
}

// 把 /changelog 拼到开发文档中
if (pathname.startsWith('/docs/react')) {
const changelogData = Object.entries(fullData).find(([key]) =>
key.startsWith('/changelog'),
)?.[1];
const changelogData = Object.entries(fullData).find(([key]) => key.startsWith('/changelog'))?.[1];
if (changelogData) {
sidebarItems.splice(1, 0, changelogData[0]);
sidebarData?.splice(1, 0, changelogData[0]);
}
}

if (pathname.startsWith('/changelog')) {
const reactDocData = Object.entries(fullData).find(([key]) =>
key.startsWith('/docs/react'),
)?.[1];
const reactDocData = Object.entries(fullData).find(([key]) => key.startsWith('/docs/react'))?.[1];
if (reactDocData) {
sidebarItems.unshift(reactDocData[0]);
sidebarItems.push(...reactDocData.slice(1));
sidebarData?.unshift(reactDocData[0]);
sidebarData?.push(...reactDocData.slice(1));
}
}

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?.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}${search}`}>
{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}${search}`}>
{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}${search}`}
style={{ display: 'flex', alignItems: 'center' }}
>
{before}
<span key="english">{item?.title}</span>
<span className="chinese" key="chinese">
{item.frontmatter?.subtitle}
</span>
<ItemTag tag={item.frontmatter?.tag} show={!before && !after} />
{after}
</Link>
),
key: item.link.replace(/(-cn$)/g, ''),
})),
});
}
} else {
const list = group.children || [];
// 如果有 date 字段,我们就对其进行排序
if (list.every((info) => info?.frontmatter?.date)) {
list.sort((a, b) => (a.frontmatter?.date > b.frontmatter?.date ? -1 : 1));
}
result.push(
...list.map((item) => ({
label: (
<Link
to={`${item.link}${search}`}
style={{ display: 'flex', alignItems: 'center' }}
>
{before}
{item?.title}
<ItemTag tag={item.frontmatter?.tag} show={!before && !after} />
{after}
</Link>
),
key: item.link.replace(/(-cn$)/g, ''),
})),
);
}
return result;
}, []) ?? []
);
}, [sidebarData, fullData, pathname, search, options]);
items = (sidebarData ?? []).map(processGroup);

return items;
}, [sidebarData, fullData, pathname, search, before, after]);

return [menuItems, pathname];
};
Expand Down