import * as React from 'react'; import { useState, useEffect, Children, ComponentType } from 'react'; import { Navigate, Route, Routes } from 'react-router-dom'; import { WithPermissions, useCheckAuth } from '../auth'; import { useTimeout } from '../util'; import { useScrollToTop, useCreatePath } from '../routing'; import { AdminChildren, CatchAllComponent, LayoutComponent, LoadingComponent, CoreLayoutProps, } from '../types'; import { useConfigureAdminRouterFromChildren } from './useConfigureAdminRouterFromChildren'; export const CoreAdminRoutes = (props: CoreAdminRoutesProps) => { const oneSecondHasPassed = useTimeout(1000); useScrollToTop(); const createPath = useCreatePath(); const { customRoutesWithLayout, customRoutesWithoutLayout, status, resources, } = useConfigureAdminRouterFromChildren(props.children); const { layout: Layout, catchAll: CatchAll, dashboard, loading: LoadingPage, menu, requireAuth, ready: Ready, title, } = props; const [canRender, setCanRender] = useState(!requireAuth); const checkAuth = useCheckAuth(); useEffect(() => { if (requireAuth) { checkAuth() .then(() => { setCanRender(true); }) .catch(() => {}); } }, [checkAuth, requireAuth]); if (status === 'empty') { return ; } if (status === 'loading' || !canRender) { return ( {customRoutesWithoutLayout} {oneSecondHasPassed ? ( } /> ) : ( )} ); } return ( {/* Render the custom routes that were outside the child function. */} {customRoutesWithoutLayout} {customRoutesWithLayout} {Children.map(resources, resource => ( ))} ) : resources.length > 0 ? ( ) : null } /> } /> } /> ); }; CoreAdminRoutes.defaultProps = { customRoutes: [], }; export interface CoreAdminRoutesProps extends CoreLayoutProps { layout: LayoutComponent; catchAll: CatchAllComponent; children?: AdminChildren; loading: LoadingComponent; requireAuth?: boolean; ready?: ComponentType; } const defaultAuthParams = { params: { route: 'dashboard' } };