import React, { useEffect } from 'react'; import { useStaticQuery, graphql, withPrefix, Link } from 'gatsby'; import { Spin } from 'antd'; import i18n from 'i18next'; import { initReactI18next } from 'react-i18next'; import { getCurrentLangKey } from 'ptz-i18n'; import { Helmet } from 'react-helmet'; import { FooterProps } from 'rc-footer'; import Seo from '../components/Seo'; import Header from '../components/Header'; import PageLoading from '../components/PageLoading'; import { OLD_SITE_DOMAIN } from '../components/Footer'; import styles from './layout.module.less'; i18n .use(initReactI18next) // passes i18n down to react-i18next .init({ initImmediate: false, fallbackLng: 'zh', keySeparator: false, react: { useSuspense: false, }, }); const lngs = ['zh', 'en']; interface LayoutProps { children: React.ReactElement; location: Location; pageContext: any; footerProps: FooterProps; } function parseNulltoUndefined(value: T) { if (value === null) { return undefined; } return value; } const Layout: React.FC = ({ children, location, footerProps }) => { // https://github.com/gatsbyjs/gatsby/issues/13867#issuecomment-489481343 if (location.pathname.includes('offline-plugin-app-shell-fallback')) { return ; } const query = graphql` query SiteTitleQuery { site { siteMetadata { title githubUrl siteUrl logoUrl showSearch showChinaMirror showLanguageSwitcher showAntVProductsCard showGithubCorner navs { slug title { zh en } target } redirects { from to } docsearchOptions { apiKey indexName } versions } } locales { internal { content } } } `; const { site, locales } = useStaticQuery(query); const { siteMetadata: { title, navs = [], githubUrl, siteUrl, logoUrl = '', showLanguageSwitcher, showSearch, showChinaMirror, showGithubCorner, showAntVProductsCard, redirects = [], docsearchOptions, versions, }, } = site; let resources = {}; try { resources = JSON.parse(locales.internal.content); } catch (e) { // empty } const pathPrefix = withPrefix('/').replace(/\/$/, ''); const path = location.pathname.replace(pathPrefix, ''); const currentLangKey = getCurrentLangKey(lngs, 'zh', path); const isHomePage = path === '/' || path === `/${currentLangKey}` || path === `/${currentLangKey}/`; if (!i18n.options.lng || process.env.NODE_ENV === 'production') { i18n.init({ lng: currentLangKey, }); } if (!i18n.options.resources) { i18n.init({ resources, }); } useEffect(() => { if (i18n.language !== currentLangKey) { i18n.changeLanguage(currentLangKey); } }, [currentLangKey]); if ( location.pathname === pathPrefix || (children && children.type && (children as any).type.noLayout) ) { return children; } const getRediectUrl = () => { const list = redirects || []; for (let i = 0; i < list.length; i += 1) { const { from = '', to, keepUrl } = list[i] as { from: string | RegExp; to: string; keepUrl?: boolean; }; // 支持字符串和正则表达式比较 if ( location.pathname !== from && !new RegExp(from).test(location.pathname) ) { return; } if (keepUrl && new RegExp(from).test(location.pathname)) { return location.pathname.replace(new RegExp(from), to); } // 如果没有指定 to,则直接用替换成老版本的域名 return to || `${OLD_SITE_DOMAIN}${location.pathname}`; } }; const rediectUrl = getRediectUrl(); const logoProps = logoUrl ? { logo: { img: logo, }, } : {}; return ( <> {rediectUrl && ( )}
{children}
); }; export default Layout;