import type {FC} from 'react'; import React, {useContext, useEffect, useState} from 'react'; import {context, Link} from 'dumi/theme'; import {history} from 'dumi'; import LocaleSelect from './LocaleSelect'; import SlugList from './SlugList'; import {Menu} from '@gyzn/linglong-ui'; import './SideMenu.less'; const {SubMenu} = Menu; interface INavbarProps { mobileMenuCollapsed: boolean; location: any; darkPrefix?: React.ReactNode; } const SideMenu: FC = ({mobileMenuCollapsed, location, darkPrefix}) => { const { config: {logo, title, description, mode}, menu, nav: navItems, base, meta } = useContext(context); const [openKeys, setOpenKeys] = useState([]); const [selectedKeys, setSelectedKeys] = useState([location.pathname]); useEffect(() => setSelectedKeys([location.pathname]), [location]); useEffect(() => setOpenKeys(() => menu.map(item => item.path).filter(item => item) as string[]), [ menu ]); const isHiddenMenus = Boolean((meta.hero || meta.features || meta.gapless) && mode === 'site') || meta.sidemenu === false || undefined; // 导航目录格式 const singleLayerMenu = menu.map(item => { // 标题 const title = item.title .split('_') .map((v, i) => { if (v.toLocaleLowerCase() === 'll' && i === 0) { return 'LL'; } return v.charAt(0).toLocaleUpperCase() + v.slice(1); }) .join(''); // children const children = item.children?.filter(i => i.title !== title); return {...item, title, children}; }); const onOpenChange = (keys: React.Key[]) => { setOpenKeys(keys as string[]); }; const onClickMenu = (path: string | undefined) => { path && history.push(path); }; return (

{title}

{description}

{/* mobile nav list */} {navItems.length ? (
    {navItems.map(nav => { const child = Boolean(nav.children?.length) && (
      {nav.children.map(item => (
    • {item.title}
    • ))}
    ); return (
  • {nav.path ? ( {nav.title} ) : ( nav.title )} {child}
  • ); })}
{/* site mode locale select */} {darkPrefix}
) : (
{darkPrefix} {/* doc mode locale select */}
)} {/* menu list */} {!isHiddenMenus && singleLayerMenu.map(item => { // always use meta from routes to reduce menu data size const hasSlugs = Boolean(meta.slugs?.length); const hasChildren = item.children && Boolean(item.children.length); const show1LevelSlugs = meta.toc === 'menu' && !hasChildren && hasSlugs && item.path === location.pathname.replace(/([^^])\/$/, '$1'); const isLeaf = !Boolean(item.children && item.children.length); if (isLeaf) { return ( {item.title} ); } return ( {item.children && item.children.map(child => ( {child.title} {/* group children slugs */} {Boolean( meta.toc === 'menu' && typeof window !== 'undefined' && child.path === location.pathname && hasSlugs ) && } ))} {/*group slugs*/} {show1LevelSlugs && } ); })}
); }; export default SideMenu;