UNPKG

ra-core

Version:

Core components of react-admin, a frontend Framework for building admin applications on top of REST services, using ES6, React

49 lines 3.53 kB
import * as React from 'react'; import { Children } from 'react'; import { WithPermissions, LogoutOnMount, useAuthState } from "../auth/index.js"; import { useScrollToTop, useRouterProvider } from "../routing/index.js"; import { useConfigureAdminRouterFromChildren } from "./useConfigureAdminRouterFromChildren.js"; import { HasDashboardContextProvider } from "./HasDashboardContext.js"; import { NavigateToFirstResource } from "./NavigateToFirstResource.js"; export const CoreAdminRoutes = (props) => { useScrollToTop(); const { Route, Routes } = useRouterProvider(); const { customRoutesWithLayout, customRoutesWithoutLayout, status, resources, } = useConfigureAdminRouterFromChildren(props.children); const { layout: Layout, catchAll: CatchAll, dashboard, loading: LoadingPage, requireAuth, ready: Ready, authenticationError: AuthenticationError = Noop, accessDenied: AccessDenied = Noop, } = props; const { authenticated, isPending: isPendingAuthenticated } = useAuthState(undefined, // do not log the user out on failure to allow access to custom routes with no layout false, { enabled: requireAuth }); if (status === 'empty') { if (!Ready) { throw new Error('The admin is empty. Please provide an empty component, or pass Resource or CustomRoutes as children.'); } return React.createElement(Ready, null); } // Note: custom routes with no layout are always rendered, regardless of the auth status if (status === 'loading' || (requireAuth && isPendingAuthenticated)) { return (React.createElement(Routes, null, customRoutesWithoutLayout, React.createElement(Route, { path: "*", element: React.createElement("div", { style: { height: '100vh' } }, React.createElement(LoadingPage, null)) }))); } if (requireAuth && (isPendingAuthenticated || !authenticated)) { return (React.createElement(Routes, null, customRoutesWithoutLayout, React.createElement(Route, { path: "*", element: React.createElement(LogoutOnMount, null) }))); } return (React.createElement(Routes, null, customRoutesWithoutLayout, React.createElement(Route, { path: "/*", element: React.createElement(HasDashboardContextProvider, { value: !!dashboard }, React.createElement(Layout, null, React.createElement(Routes, null, customRoutesWithLayout, Children.map(resources, resource => (React.createElement(Route, { key: resource.props.name, path: `${resource.props.name}/*`, element: resource }))), React.createElement(Route, { path: "/", element: dashboard ? (React.createElement(WithPermissions, { authParams: defaultAuthParams, component: dashboard, loading: LoadingPage })) : (React.createElement(NavigateToFirstResource, { loading: LoadingPage })) }), React.createElement(Route, { path: "/authentication-error", element: React.createElement(AuthenticationError, null) }), React.createElement(Route, { path: "/access-denied", element: React.createElement(AccessDenied, null) }), React.createElement(Route, { path: "*", element: React.createElement(CatchAll, null) })))) }))); }; // FIXME in v6: make dashboard anonymous by default to remove this hack const defaultAuthParams = { params: { route: 'dashboard' } }; const Noop = () => null; //# sourceMappingURL=CoreAdminRoutes.js.map