import type { IRouteComponentProps } from '@umijs/types';
import { context, Link } from 'dumi/theme';
import React, { useContext, useState } from 'react';
import Dark from './components/Dark';
import Navbar from './components/Navbar';
import SearchBar from './components/SearchBar';
import SideMenu from './components/SideMenu';
import SlugList from './components/SlugList';
import './style/layout.less';
const Hero = hero => (
<>
{hero.image &&
}
{hero.title}
{hero.actions &&
hero.actions.map(action => (
{action.text}
))}
>
);
const Features = features => (
{features.map(feat => (
{feat.link ? (
{feat.title}
) : (
{feat.title}
)}
))}
);
const Layout: React.FC = ({ children, location }) => {
const {
config: { mode, repository },
nav: navItems,
meta,
locale,
} = useContext(context);
console.log(useContext(context));
const { url: repoUrl, branch, platform } = repository;
const [menuCollapsed, setMenuCollapsed] = useState(true);
const [darkSwitch, setDarkSwitch] = useState(false);
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 (
{
setDarkSwitch(false);
if (menuCollapsed) return;
setMenuCollapsed(true);
}}
>
}
darkPrefix={
{
setDarkSwitch(val => !val);
ev.stopPropagation();
}}
isSideMenu={false}
/>
}
onMobileMenuClick={ev => {
setMenuCollapsed(val => !val);
ev.stopPropagation();
}}
/>
}
mobileMenuCollapsed={menuCollapsed}
location={location}
/>
{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;