@refinedev/core
Version:
Refine is a React meta-framework for building enterprise-level, data-intensive applications rapidly with support for modern UI libraries and headless integrations.
27 lines (22 loc) • 810 B
text/typescript
import type { IResourceItem } from "../../../contexts/resource/types";
/**
* Returns the parent resource of the given resource.
* Uses the `resource.meta.parent` property.
*/
export const getParentResource = (
resource: IResourceItem,
resources: IResourceItem[],
): IResourceItem | undefined => {
const parentName = resource.meta?.parent;
if (!parentName) {
return undefined;
}
const parentResource = resources.find(
(resource) => (resource.identifier ?? resource.name) === parentName,
);
/**
* If the parent resource is not found, we return a resource object with the name of the parent resource.
* Because we still want to have nesting and prefixing for the resource even if the parent is not explicitly defined.
*/
return parentResource ?? { name: parentName };
};