openpanel/packages/react-router-v6/src/navigate-to-resource.tsx
Stefan Pejcic 8595a9f4e5 back
2024-05-08 19:58:53 +02:00

37 lines
969 B
TypeScript

import { useResource, useGetToPath } from "@refinedev/core";
import React, { PropsWithChildren } from "react";
import { Navigate } from "react-router-dom";
type NavigateToResourceProps = PropsWithChildren<{
resource?: string;
meta?: Record<string, unknown>;
}>;
export const NavigateToResource: React.FC<NavigateToResourceProps> = ({
resource: resourceProp,
meta,
}) => {
const getToPath = useGetToPath();
const { resource, resources } = useResource(resourceProp);
const toResource = resource || resources.find((r) => r.list);
if (toResource) {
const path = getToPath({
resource: toResource,
action: "list",
meta,
});
if (path) {
return <Navigate to={path} />;
}
console.warn("No resource is found to navigate to.");
return null;
} else {
console.warn("No resource is found to navigate to.");
return null;
}
};