import React from 'react'; import { Routes, Route, Outlet, useLocation } from 'react-router-dom'; import { IRouter, IBrowserRouterProps, IMeta } from './types/router'; import routers from './app/routers'; import Loading from './app/components/Loading'; import useFeatures from '@project/hooks/useFeatures'; type TRenderRoutes = ( routes?: IRouter[], breadcrumbs?: string[] ) => React.ReactNode[] | null; /** * 渲染应用路由 * @param routes * @param breadcrumb */ const renderRoutes: TRenderRoutes = (routes = [], breadcrumb = []) => { if (routes.length === 0) return null; return routes.map((route, index: number) => { const { Component, children, namespace, meta, isFirst, customRouter } = route; let currentBreadcrumb = breadcrumb; let props = {}; if (isFirst) { props = { index: true, }; } else { props = { path: route.path, }; } return ( } > {renderRoutes(children, currentBreadcrumb)} ); }); }; const Guard = React.memo( (props: { Component?: React.FC | (() => any); namespace?: string; meta?: IMeta; breadcrumb: string[]; customRouter?: boolean; }) => { const { Component, namespace, meta, breadcrumb, customRouter } = props; const features = useFeatures(); const location = useLocation(); const { pathname } = location; const path = namespace ? pathname.slice(namespace.length) : pathname; const i18nNs = `taicang-p-${ path.startsWith('/') ? path.slice(1).replaceAll(/\//g, '-') : path.replaceAll(/\//g, '-') }`; if (i18nNs) { const windowTitle = features.locales.hasKey(`${i18nNs}.pageTitle`); if (windowTitle) { breadcrumb.push(windowTitle); window.document.title = windowTitle; } } return Component ? ( }> ) : ( ); } ); const AppRouter = React.memo(() => ( {renderRoutes(routers)} )); export default AppRouter;