import React from 'react'; import { Switch, Redirect, Route, RouteProps } from 'react-router-dom'; import { CosmosRouterProps } from './type'; import { routeContext, flatRoutes, addFlag2Route, filterAuthedRoute } from './utils'; import { LazyComponent } from './components/LazyComponent'; import { LayoutWrapper } from './components/LayoutWrapper'; const { Provider } = routeContext; export function CosmosRouter = {}, Auth = number>(props: CosmosRouterProps) { const { routes = [], defaultConfig, lazyLoad, layout, checkAuth, catalogFromRoute, hasCheckResult } = props; const authFlagRoutes = addFlag2Route(routes, checkAuth); return ( { flatRoutes(authFlagRoutes) .filter(({ authFlag }) => !hasCheckResult || authFlag) .map(route => { const { path, redirectTo, auth, authFlag, useLayout, fallback, exist, children, noAuthDisplay, catalog, allCatalog, allPath, component, render, ...rest } = { exact: true, useLayout: true, ...defaultConfig, ...route }; // 是否使用布局 const hasLayout = !!(useLayout && layout); if (!authFlag) { // 旧版本这里使用的是 fallback, 这里做一下兼容 const noAuthNode = noAuthDisplay || fallback || null; return ( key={allPath} path={allPath} {...rest} > { hasLayout ? {noAuthNode} : noAuthNode } ); } // 重定向 if (redirectTo) { return ( ); } // 非自动懒加载组件的情况 if (component || render) { return hasLayout ? ( key={allPath} path={allPath} {...rest} > ) : ( ) } const lazyComp = typeof lazyLoad === 'function' ? ( lazyLoad(catalogFromRoute ? allCatalog : catalog!)} fallback={fallback} /> ) : null; return ( key={allPath} path={allPath} {...rest} > { hasLayout ? {lazyComp} : lazyComp } ) }) } ) };