2023-04-14 17:29:08 +08:00
import classNames from 'classnames' ;
import dayjs from 'dayjs' ;
2024-02-04 17:08:39 +08:00
2023-04-14 17:29:08 +08:00
import 'dayjs/locale/zh-cn' ;
2024-02-04 17:08:39 +08:00
import React , { useContext , useEffect , useLayoutEffect , useRef } from 'react' ;
2023-07-28 16:17:43 +08:00
import ConfigProvider from 'antd/es/config-provider' ;
2024-02-04 17:08:39 +08:00
import zhCN from 'antd/es/locale/zh_CN' ;
import { Helmet , useOutlet , useSiteData } from 'dumi' ;
2022-11-30 11:05:41 +08:00
import useLocale from '../../../hooks/useLocale' ;
import useLocation from '../../../hooks/useLocation' ;
2022-11-09 12:28:04 +08:00
import GlobalStyles from '../../common/GlobalStyles' ;
2023-04-14 17:29:08 +08:00
import Header from '../../slots/Header' ;
import SiteContext from '../../slots/SiteContext' ;
2024-02-04 17:08:39 +08:00
2023-04-14 17:29:08 +08:00
import '../../static/style' ;
2024-02-04 17:08:39 +08:00
2023-10-07 14:23:52 +08:00
import IndexLayout from '../IndexLayout' ;
2023-04-14 17:29:08 +08:00
import ResourceLayout from '../ResourceLayout' ;
2022-11-09 12:28:04 +08:00
import SidebarLayout from '../SidebarLayout' ;
const locales = {
cn : {
title : 'Ant Design - 一套企业级 UI 设计语言和 React 组件库' ,
description : '基于 Ant Design 设计体系的 React UI 组件库,用于研发企业级中后台产品。' ,
} ,
en : {
title : "Ant Design - The world's second most popular React UI framework" ,
description :
'An enterprise-class UI design language and React UI library with a set of high-quality React components, one of best React UI library for enterprises' ,
} ,
} ;
2022-11-24 20:11:50 +08:00
const DocLayout : React.FC = ( ) = > {
2022-11-09 12:28:04 +08:00
const outlet = useOutlet ( ) ;
const location = useLocation ( ) ;
2023-10-10 17:30:46 +08:00
const { pathname , search , hash } = location ;
2022-11-09 12:28:04 +08:00
const [ locale , lang ] = useLocale ( locales ) ;
2023-08-14 13:32:57 +08:00
const timerRef = useRef < ReturnType < typeof setTimeout > | null > ( null ) ;
2022-12-19 11:42:41 +08:00
const { direction } = useContext ( SiteContext ) ;
2023-10-10 17:30:46 +08:00
const { loading } = useSiteData ( ) ;
2022-11-09 12:28:04 +08:00
2022-11-24 15:41:45 +08:00
useLayoutEffect ( ( ) = > {
if ( lang === 'cn' ) {
dayjs . locale ( 'zh-cn' ) ;
} else {
dayjs . locale ( 'en' ) ;
}
} , [ ] ) ;
2022-11-09 12:28:04 +08:00
useEffect ( ( ) = > {
const nprogressHiddenStyle = document . getElementById ( 'nprogress-style' ) ;
if ( nprogressHiddenStyle ) {
timerRef . current = setTimeout ( ( ) = > {
nprogressHiddenStyle . parentNode ? . removeChild ( nprogressHiddenStyle ) ;
} , 0 ) ;
}
} , [ ] ) ;
2023-10-10 17:30:46 +08:00
// handle hash change or visit page hash from Link component, and jump after async chunk loaded
useEffect ( ( ) = > {
const id = hash . replace ( '#' , '' ) ;
2024-02-04 17:08:39 +08:00
if ( id ) {
document . getElementById ( decodeURIComponent ( id ) ) ? . scrollIntoView ( ) ;
}
2023-10-10 17:30:46 +08:00
} , [ loading , hash ] ) ;
2022-12-22 14:12:26 +08:00
useEffect ( ( ) = > {
2022-11-09 12:28:04 +08:00
if ( typeof ( window as any ) . ga !== 'undefined' ) {
( window as any ) . ga ( 'send' , 'pageview' , pathname + search ) ;
}
} , [ location ] ) ;
2024-02-04 17:08:39 +08:00
const content = React . useMemo < React.ReactNode > ( ( ) = > {
2022-11-09 12:28:04 +08:00
if (
2022-11-19 13:47:33 +08:00
[ '' , '/' ] . some ( ( path ) = > path === pathname ) ||
[ '/index' ] . some ( ( path ) = > pathname . startsWith ( path ) )
2022-11-09 12:28:04 +08:00
) {
return (
2023-10-07 14:23:52 +08:00
< IndexLayout title = { locale . title } desc = { locale . description } >
{ outlet }
< / IndexLayout >
2022-11-09 12:28:04 +08:00
) ;
2022-11-30 11:05:41 +08:00
}
if ( pathname . startsWith ( '/docs/resource' ) ) {
2022-11-09 12:28:04 +08:00
return < ResourceLayout > { outlet } < / ResourceLayout > ;
2022-11-30 11:05:41 +08:00
}
if ( pathname . startsWith ( '/theme-editor' ) ) {
return outlet ;
2022-11-09 12:28:04 +08:00
}
return < SidebarLayout > { outlet } < / SidebarLayout > ;
} , [ pathname , outlet ] ) ;
return (
2022-12-19 11:42:41 +08:00
< >
2022-12-01 16:10:53 +08:00
< Helmet encodeSpecialCharacters = { false } >
< html
2023-07-24 15:38:49 +08:00
lang = { lang === 'cn' ? 'zh-CN' : lang }
2022-12-01 16:10:53 +08:00
data - direction = { direction }
2023-04-14 17:29:08 +08:00
className = { classNames ( { rtl : direction === 'rtl' } ) }
2022-12-01 16:10:53 +08:00
/ >
< link
sizes = "144x144"
href = "https://gw.alipayobjects.com/zos/antfincdn/UmVnt3t4T0/antd.png"
/ >
2023-05-11 20:00:28 +08:00
< meta property = "og:description" content = { locale . description } / >
2022-12-01 16:10:53 +08:00
< meta property = "og:type" content = "website" / >
< meta
property = "og:image"
content = "https://gw.alipayobjects.com/zos/rmsportal/rlpTLlbMzTNYuZGGCVYM.png"
/ >
< / Helmet >
2023-01-08 23:06:45 +08:00
< ConfigProvider direction = { direction } locale = { lang === 'cn' ? zhCN : undefined } >
2022-12-01 16:10:53 +08:00
< GlobalStyles / >
2022-12-19 11:42:41 +08:00
< Header / >
2022-12-01 16:10:53 +08:00
{ content }
< / ConfigProvider >
2022-12-19 11:42:41 +08:00
< / >
2022-11-09 12:28:04 +08:00
) ;
} ;
export default DocLayout ;