2023-02-16 22:39:57 +00:00
|
|
|
import { UserPermissionRole } from "@prisma/client";
|
2022-09-08 15:35:31 +00:00
|
|
|
import { useSession } from "next-auth/react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { FC } from "react";
|
|
|
|
import { Fragment } from "react";
|
2022-09-08 15:35:31 +00:00
|
|
|
|
|
|
|
type AdminRequiredProps = {
|
|
|
|
as?: keyof JSX.IntrinsicElements;
|
|
|
|
children?: React.ReactNode;
|
|
|
|
/**Not needed right now but will be useful if we ever expand our permission roles */
|
|
|
|
roleRequired?: UserPermissionRole;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const PermissionContainer: FC<AdminRequiredProps> = ({
|
|
|
|
children,
|
|
|
|
as,
|
|
|
|
roleRequired = "ADMIN",
|
|
|
|
...rest
|
|
|
|
}) => {
|
|
|
|
const session = useSession();
|
|
|
|
|
|
|
|
// Admin can do everything
|
2023-04-19 14:15:08 +00:00
|
|
|
if (session.data?.user.role !== roleRequired && session.data?.user.role != UserPermissionRole.ADMIN)
|
|
|
|
return null;
|
2022-09-08 15:35:31 +00:00
|
|
|
const Component = as ?? Fragment;
|
|
|
|
return <Component {...rest}>{children}</Component>;
|
|
|
|
};
|