import React from "react"; import { useRefineContext, LayoutWrapper, ErrorComponent, useResource, LoginPage as DefaultLoginPage, CanAccess, useRouterContext, } from "@pankod/refine-core"; import type { ResourceRouterParams } from "@pankod/refine-core"; export function NextRouteComponent( this: { initialRoute?: string }, props: any, ): React.ReactNode { const { useHistory, useLocation, useParams } = useRouterContext(); const { resources } = useResource(); const { push } = useHistory(); const { resource: routeResourceName, action, id, } = useParams(); const { pathname } = useLocation(); const { DashboardPage, catchAll, LoginPage } = useRefineContext(); const resource = resources.find( (res) => res.name === routeResourceName || res.route === routeResourceName, ); const isServer = typeof window !== "undefined"; const renderLoginRouteElement = (): JSX.Element => { if (LoginPage) return ; return ; }; if (routeResourceName === "login") { return renderLoginRouteElement(); } if (pathname === "/") { if (DashboardPage) { return ( } params={{ resource, }} > ); } else { if (isServer) { if (typeof this !== "undefined" && this.initialRoute) { push(this.initialRoute); } else { // push(`${resources.find((el) => el.list).route}`); /* * the above line is a better solution for the initial route * but in next.js, users can have custom pages through file system * which makes `list` component of the resource redundant. * for these cases, we need to redirect to the first resource * in the resources array, no matter if it has a list component or not. */ push(`/${resources[0].route}`); } } return null; } } if (resource) { const { list, create, edit, show, name, canCreate, canEdit, canShow, canDelete, options, } = resource; const List = list ?? (() => null); const Create = create ?? (() => null); const Edit = edit ?? (() => null); const Show = show ?? (() => null); const renderCrud = () => { switch (action) { case undefined: { return ( } params={{ resource, }} > ); } case "create": case "clone": { return ( } params={{ resource, }} > ); } case "edit": { return ( } > ); } case "show": { return ( } > ); } } }; return {renderCrud()}; } return catchAll ? ( <>{catchAll} ) : ( ); }