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, search } = useLocation(); const sidebarData = useSidebarData(); const { before, after } = options; const menuItems = useMemo(() => { 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>((result, group) => { if (group.title) { // 设计文档特殊处理二级分组 if (pathname.startsWith('/docs/spec')) { const childrenGroup = group.children.reduce< Record[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: ( {before} {item.title} {after} ), 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: ( {before} {item.title} {after} ), 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: ( {before} {item.title} {(item.frontmatter as any).subtitle} {after} ), key: item.link.replace(/(-cn$)/g, ''), })), }); } } else { result.push( ...group.children?.map((item) => ({ label: ( {before} {item.title} {after} ), key: item.link.replace(/(-cn$)/g, ''), })), ); } return result; }, []) ?? [] ); }, [sidebarData, fullData, pathname, search]); return [menuItems, pathname]; }; export default useMenu;