2021-10-08 11:23:29 +02:00
|
|
|
import { IconButton, Tooltip } from '@material-ui/core';
|
2022-02-25 10:55:39 +01:00
|
|
|
import React, { useContext } from 'react';
|
2021-10-08 11:23:29 +02:00
|
|
|
import AccessContext from '../../../contexts/AccessContext';
|
|
|
|
|
2022-01-14 15:50:02 +01:00
|
|
|
interface IPermissionIconButtonProps
|
2022-03-04 17:29:51 +01:00
|
|
|
extends React.HTMLProps<HTMLButtonElement> {
|
2021-10-08 11:23:29 +02:00
|
|
|
permission: string;
|
2021-11-26 16:07:05 +01:00
|
|
|
Icon?: React.ElementType;
|
2022-02-15 10:57:57 +01:00
|
|
|
tooltip?: string;
|
2021-10-08 11:23:29 +02:00
|
|
|
onClick?: (e: any) => void;
|
2021-10-20 12:05:44 +02:00
|
|
|
projectId?: string;
|
2022-01-14 15:50:02 +01:00
|
|
|
environmentId?: string;
|
2022-02-09 12:25:02 +01:00
|
|
|
edge?: string;
|
|
|
|
className?: string;
|
|
|
|
title?: string;
|
2021-10-08 11:23:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const PermissionIconButton: React.FC<IPermissionIconButtonProps> = ({
|
|
|
|
permission,
|
|
|
|
Icon,
|
|
|
|
tooltip,
|
|
|
|
onClick,
|
2021-10-20 12:05:44 +02:00
|
|
|
projectId,
|
2021-10-08 11:23:29 +02:00
|
|
|
children,
|
2022-01-14 15:50:02 +01:00
|
|
|
environmentId,
|
2021-10-08 11:23:29 +02:00
|
|
|
...rest
|
|
|
|
}) => {
|
|
|
|
const { hasAccess } = useContext(AccessContext);
|
2022-01-14 15:50:02 +01:00
|
|
|
let access;
|
2021-10-08 11:23:29 +02:00
|
|
|
|
2022-01-14 15:50:02 +01:00
|
|
|
if (projectId && environmentId) {
|
|
|
|
access = hasAccess(permission, projectId, environmentId);
|
|
|
|
} else if (projectId) {
|
|
|
|
access = hasAccess(permission, projectId);
|
|
|
|
} else {
|
|
|
|
access = hasAccess(permission);
|
|
|
|
}
|
2021-10-20 12:05:44 +02:00
|
|
|
|
2022-02-23 13:45:37 +01:00
|
|
|
const tooltipText = !access
|
|
|
|
? "You don't have access to perform this operation"
|
|
|
|
: '';
|
2021-10-08 11:23:29 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip title={tooltipText} arrow>
|
|
|
|
<span>
|
2022-03-03 10:01:04 +01:00
|
|
|
{/* @ts-expect-error */}
|
2021-10-08 11:23:29 +02:00
|
|
|
<IconButton onClick={onClick} disabled={!access} {...rest}>
|
|
|
|
{children}
|
|
|
|
</IconButton>
|
|
|
|
</span>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PermissionIconButton;
|