import React, { useMemo } from "react"; import { Route, Routes, Navigate, Outlet } from "react-router-dom"; import { LoginPage as DefaultLoginPage, ErrorComponent, LayoutWrapper, useResource, useRefineContext, useRouterContext, CanAccess, type ResourceRouterParams, useActiveAuthProvider, useIsAuthenticated, } from "@refinedev/core"; import type { RefineRouteProps } from "./index"; const ResourceComponent: React.FC<{ route: string }> = ({ route }) => { const { catchAll } = useRefineContext(); const { useParams } = useRouterContext(); const { resources } = useResource(); const { action, id } = useParams(); const resource = resources.find((res) => res.route === route); if (resource) { const { list, create, edit, show, name, canCreate, canEdit, canShow, canDelete, options, } = resource; const stringError = () => console.error( "`string` resource routes are not supported in legacy router. Please switch to the new router or assign Components to the resource routes.", ); const definitionError = () => console.error( "`Object` resource routes are not supported in legacy router. Please switch to the new router or assign Components to the resource routes.", ); const List = (typeof list === "string" ? stringError() : typeof list === "object" ? definitionError() : list) ?? (() => null); const Create = (typeof create === "string" ? stringError() : typeof create === "object" ? definitionError() : create) ?? (() => null); const Edit = (typeof edit === "string" ? stringError() : typeof edit === "object" ? definitionError() : edit) ?? (() => null); const Show = (typeof show === "string" ? stringError() : typeof show === "object" ? definitionError() : show) ?? (() => null); const renderCrud = () => { switch (action) { case undefined: return ( } params={{ resource, }} > {!list ? ( catchAll ?? ) : ( )} ); case "create": case "clone": return ( } params={{ id: id ? decodeURIComponent(id) : undefined, resource, }} > {!create ? ( catchAll ?? ) : ( )} ); case "edit": return ( } > {!edit ? ( catchAll ?? ) : ( )} ); case "show": return ( } > {!show ? ( catchAll ?? ) : ( )} ); default: return <>{catchAll ?? }; } }; return renderCrud(); } return <>{catchAll ?? }; }; type RouteProviderProps = { initialRoute?: string; }; export const RouteProvider = ({ initialRoute, }: RouteProviderProps): JSX.Element => { const { resources } = useResource(); const { catchAll, DashboardPage, LoginPage } = useRefineContext(); const { routes: customRoutes } = useRouterContext(); const authProvider = useActiveAuthProvider(); const { isFetching, isError, data: authData, } = useIsAuthenticated({ v3LegacyAuthProviderCompatible: Boolean(authProvider?.isLegacy), params: { type: "routeProvider", }, }); const isAuthenticated = useMemo(() => { if (authProvider?.isProvided) { if (authProvider?.isLegacy) { const hasAuthError = isError || authData?.error; return !hasAuthError; } return authData?.authenticated; } return true; }, [authData, isError, authProvider?.isLegacy]); if (isFetching) { return ( ); } const CustomPathAfterLogin: React.FC = (): JSX.Element | null => { const { pathname, search } = location; const toURL = `${pathname}${search}`; return ; }; const resourceRoutes: JSX.Element[] = []; resources.map((resource) => { const route = ( } > } > } /> ); resourceRoutes.push(route); }); const renderAuthorized = () => ( {[...(customRoutes || [])] .filter((p: RefineRouteProps) => !p.layout) .map((route, i) => ( ))} } > {[...(customRoutes || [])] .filter((p: RefineRouteProps) => p.layout) .map((route, i) => ( ))} {DashboardPage ? ( } > } /> ) : ( p.list)?.route?.replace(/^\//, "")}` } /> } /> )} {...[...(resourceRoutes || [])]} } /> ); const renderLoginRouteElement = (): JSX.Element => { if (LoginPage) return ; return ; }; const renderUnauthorized = () => ( {[...(customRoutes || [])].map((route, i) => ( ))} } /> ); return isAuthenticated ? renderAuthorized() : renderUnauthorized(); };