mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 16:40:30 +00:00
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
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<TOrgPermission>;
|
|
|
|
export const OrgPermissionCan: FunctionComponent<Props> = ({
|
|
label = "Access restricted",
|
|
children,
|
|
passThrough = true,
|
|
renderTooltip,
|
|
allowedLabel,
|
|
...props
|
|
}) => {
|
|
const { permission } = useOrgPermission();
|
|
|
|
return (
|
|
<Can {...props} passThrough={passThrough} ability={props?.ability || permission}>
|
|
{(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 <Tooltip content={label}>{finalChild}</Tooltip>;
|
|
}
|
|
|
|
if (isAllowed && renderTooltip) {
|
|
return <Tooltip content={allowedLabel}>{finalChild}</Tooltip>;
|
|
}
|
|
|
|
if (!isAllowed) return null;
|
|
|
|
return finalChild;
|
|
}}
|
|
</Can>
|
|
);
|
|
};
|