import { FunctionComponent, ReactNode } from "react"; import { BoundCanProps, Can } from "@casl/react"; import { TOrgPermission, useOrgPermission } from "@app/context/OrgPermissionContext"; import { Tooltip } from "../v2"; type Props = { label?: ReactNode; // this prop is used when there exist already a tooltip as helper text for users // so when permission is allowed same tooltip will be reused to show helpertext renderTooltip?: boolean; allowedLabel?: string; } & BoundCanProps; export const OrgPermissionCan: FunctionComponent = ({ label = "Access restricted", children, passThrough = true, renderTooltip, allowedLabel, ...props }) => { const { permission } = useOrgPermission(); return ( {(isAllowed, ability) => { // akhilmhdh: This is set as type due to error in casl react type. const finalChild = typeof children === "function" ? children(isAllowed, ability as TOrgPermission) : children; if (!isAllowed && passThrough) { return {finalChild}; } if (isAllowed && renderTooltip) { return {finalChild}; } if (!isAllowed) return null; return finalChild; }} ); };