import React, { useContext, useState, useEffect } from 'react'; import type { IRouteComponentProps } from '@umijs/types'; import { context, Link } from 'dumi/theme'; import Navbar from './components/Navbar'; import SideMenu from './components/SideMenu'; import SlugList from './components/SlugList'; import SearchBar from './components/SearchBar'; import './style/layout.less'; const Hero = hero => ( <>
{hero.image && }

{hero.title}

{hero.actions && hero.actions.map(action => ( ))}
); const Features = features => (
{features.map(feat => (
{feat.link ? (
{feat.title}
) : (
{feat.title}
)}
))}
); const Layout: React.FC = ({ children, location }) => { const { config: { mode, repository }, meta, locale, } = useContext(context); const { url: repoUrl, branch, platform } = repository; const [menuCollapsed, setMenuCollapsed] = useState(true); const isSiteMode = mode === 'site'; const showHero = isSiteMode && meta.hero; const showFeatures = isSiteMode && meta.features; const showSideMenu = meta.sidemenu !== false && !showHero && !showFeatures && !meta.gapless; const showSlugs = !showHero && !showFeatures && Boolean(meta.slugs?.length) && (meta.toc === 'content' || meta.toc === undefined) && !meta.gapless; const isCN = /^zh|cn$/i.test(locale); const updatedTimeIns = new Date(meta.updatedTime); const updatedTime: any = `${updatedTimeIns.toLocaleDateString([], { hour12: false })} ${updatedTimeIns.toLocaleTimeString([], { hour12: false })}`; const repoPlatform = { github: 'GitHub', gitlab: 'GitLab' }[ (repoUrl || '').match(/(github|gitlab)/)?.[1] || 'nothing' ] || platform; return (
{ if (menuCollapsed) return; setMenuCollapsed(true); }} >
{isCN ? '文档还在建设中,与实际代码存在差异' : 'The documentation is still under construction and differs somewhat from the actual code.'}
} onMobileMenuClick={ev => { setMenuCollapsed(val => !val); ev.stopPropagation(); }} /> {showSlugs && } {showHero && Hero(meta.hero)} {showFeatures && Features(meta.features)}
{children} {!showHero && !showFeatures && meta.filePath && !meta.gapless && (
{repoPlatform && ( {isCN ? `在 ${repoPlatform} 上编辑此页` : `Edit this doc on ${repoPlatform}`} )} {/* {updatedTime} */}
)} {(showHero || showFeatures) && meta.footer && (
)}
); }; export default Layout;