2022-04-21 08:26:49 +02:00
|
|
|
import { Button, ButtonProps } from '@material-ui/core';
|
2021-10-08 11:23:29 +02:00
|
|
|
import { Lock } from '@material-ui/icons';
|
2022-02-24 09:23:07 +01:00
|
|
|
import AccessContext from 'contexts/AccessContext';
|
2022-02-25 10:55:39 +01:00
|
|
|
import React, { useContext } from 'react';
|
2021-10-08 11:23:29 +02:00
|
|
|
import ConditionallyRender from '../ConditionallyRender';
|
2022-04-21 08:26:49 +02:00
|
|
|
import { TooltipResolver } from 'component/common/TooltipResolver/TooltipResolver';
|
|
|
|
import { formatAccessText } from 'utils/formatAccessText';
|
|
|
|
import { useId } from 'hooks/useId';
|
2021-10-08 11:23:29 +02:00
|
|
|
|
2022-04-21 08:26:49 +02:00
|
|
|
export interface IPermissionButtonProps extends Omit<ButtonProps, 'title'> {
|
2022-01-14 15:50:02 +01:00
|
|
|
permission: string | string[];
|
2021-10-08 11:23:29 +02:00
|
|
|
onClick?: (e: any) => void;
|
|
|
|
disabled?: boolean;
|
2021-10-20 12:05:44 +02:00
|
|
|
projectId?: string;
|
2022-01-14 15:50:02 +01:00
|
|
|
environmentId?: string;
|
2022-04-21 08:26:49 +02:00
|
|
|
tooltip?: string;
|
2021-10-08 11:23:29 +02:00
|
|
|
}
|
|
|
|
|
2022-02-24 09:23:07 +01:00
|
|
|
const PermissionButton: React.FC<IPermissionButtonProps> = ({
|
2021-10-08 11:23:29 +02:00
|
|
|
permission,
|
2022-03-09 14:59:24 +01:00
|
|
|
variant = 'contained',
|
|
|
|
color = 'primary',
|
2021-10-08 11:23:29 +02:00
|
|
|
onClick,
|
|
|
|
children,
|
|
|
|
disabled,
|
2021-10-20 12:05:44 +02:00
|
|
|
projectId,
|
2022-01-14 15:50:02 +01:00
|
|
|
environmentId,
|
2022-04-21 08:26:49 +02:00
|
|
|
tooltip,
|
2021-10-08 11:23:29 +02:00
|
|
|
...rest
|
|
|
|
}) => {
|
|
|
|
const { hasAccess } = useContext(AccessContext);
|
2022-04-21 08:26:49 +02:00
|
|
|
const id = useId();
|
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
|
|
|
const handleAccess = () => {
|
|
|
|
let access;
|
|
|
|
if (Array.isArray(permission)) {
|
|
|
|
access = permission.some(permission => {
|
|
|
|
if (projectId && environmentId) {
|
|
|
|
return hasAccess(permission, projectId, environmentId);
|
|
|
|
} else if (projectId) {
|
|
|
|
return hasAccess(permission, projectId);
|
|
|
|
} else {
|
|
|
|
return hasAccess(permission);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (projectId && environmentId) {
|
|
|
|
access = hasAccess(permission, projectId, environmentId);
|
|
|
|
} else if (projectId) {
|
|
|
|
access = hasAccess(permission, projectId);
|
|
|
|
} else {
|
|
|
|
access = hasAccess(permission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return access;
|
|
|
|
};
|
|
|
|
|
|
|
|
access = handleAccess();
|
2021-10-20 12:05:44 +02:00
|
|
|
|
2021-10-08 11:23:29 +02:00
|
|
|
return (
|
2022-04-21 08:26:49 +02:00
|
|
|
<TooltipResolver title={formatAccessText(access, tooltip)} arrow>
|
|
|
|
<span id={id}>
|
2021-10-08 11:23:29 +02:00
|
|
|
<Button
|
|
|
|
onClick={onClick}
|
|
|
|
disabled={disabled || !access}
|
2022-04-21 08:26:49 +02:00
|
|
|
aria-describedby={id}
|
2022-03-09 14:59:24 +01:00
|
|
|
variant={variant}
|
|
|
|
color={color}
|
2021-10-08 11:23:29 +02:00
|
|
|
{...rest}
|
|
|
|
endIcon={
|
|
|
|
<ConditionallyRender
|
|
|
|
condition={!access}
|
2022-04-21 08:26:49 +02:00
|
|
|
show={<Lock titleAccess="Locked" />}
|
2021-10-08 11:23:29 +02:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Button>
|
|
|
|
</span>
|
2022-04-21 08:26:49 +02:00
|
|
|
</TooltipResolver>
|
2021-10-08 11:23:29 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PermissionButton;
|