2022-05-02 15:52:41 +02:00
|
|
|
import { Button, ButtonProps } from '@mui/material';
|
|
|
|
import { Lock } from '@mui/icons-material';
|
2022-12-14 10:00:14 +01:00
|
|
|
import React from 'react';
|
2022-05-09 15:17:20 +02:00
|
|
|
import {
|
|
|
|
TooltipResolver,
|
|
|
|
ITooltipResolverProps,
|
|
|
|
} from 'component/common/TooltipResolver/TooltipResolver';
|
2022-04-21 08:26:49 +02:00
|
|
|
import { formatAccessText } from 'utils/formatAccessText';
|
|
|
|
import { useId } from 'hooks/useId';
|
2022-12-14 10:00:14 +01:00
|
|
|
import {
|
|
|
|
useHasRootAccess,
|
|
|
|
useHasProjectEnvironmentAccess,
|
|
|
|
} from 'hooks/useHasAccess';
|
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-05-09 15:17:20 +02:00
|
|
|
tooltipProps?: Omit<ITooltipResolverProps, 'children'>;
|
2023-06-21 15:26:07 +02:00
|
|
|
hideLockIcon?: boolean;
|
2021-10-08 11:23:29 +02:00
|
|
|
}
|
|
|
|
|
2022-12-14 10:00:14 +01:00
|
|
|
interface IPermissionBaseButtonProps extends IPermissionButtonProps {
|
|
|
|
access: boolean;
|
|
|
|
}
|
2022-11-11 11:04:59 +01:00
|
|
|
|
2022-12-14 10:00:14 +01:00
|
|
|
export interface IProjectPermissionButtonProps extends IPermissionButtonProps {
|
|
|
|
projectId: string;
|
|
|
|
environmentId: string;
|
|
|
|
}
|
2022-01-14 15:50:02 +01:00
|
|
|
|
2023-06-29 19:01:27 +02:00
|
|
|
const getEndIcon = (
|
|
|
|
access: boolean,
|
|
|
|
fallBackIcon?: React.ReactNode,
|
2023-10-02 14:25:46 +02:00
|
|
|
hideLockIcon?: boolean,
|
2023-06-29 19:01:27 +02:00
|
|
|
): React.ReactNode => {
|
|
|
|
if (!access && !hideLockIcon) {
|
2023-10-02 14:25:46 +02:00
|
|
|
return <Lock titleAccess='Locked' />;
|
2023-06-29 19:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fallBackIcon) {
|
|
|
|
return fallBackIcon;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-12-14 10:00:14 +01:00
|
|
|
const ProjectEnvironmentPermissionButton: React.FC<IProjectPermissionButtonProps> =
|
|
|
|
React.forwardRef((props, ref) => {
|
|
|
|
const access = useHasProjectEnvironmentAccess(
|
|
|
|
props.permission,
|
2022-12-14 13:00:51 +01:00
|
|
|
props.projectId,
|
2023-10-02 14:25:46 +02:00
|
|
|
props.environmentId,
|
2022-12-14 10:00:14 +01:00
|
|
|
);
|
2022-01-14 15:50:02 +01:00
|
|
|
|
2022-12-14 10:00:14 +01:00
|
|
|
return <BasePermissionButton {...props} access={access} ref={ref} />;
|
|
|
|
});
|
2021-10-20 12:05:44 +02:00
|
|
|
|
2022-12-14 10:00:14 +01:00
|
|
|
const RootPermissionButton: React.FC<IPermissionButtonProps> = React.forwardRef(
|
|
|
|
(props, ref) => {
|
|
|
|
const access = useHasRootAccess(
|
|
|
|
props.permission,
|
2022-12-14 13:00:51 +01:00
|
|
|
props.projectId,
|
2023-10-02 14:25:46 +02:00
|
|
|
props.environmentId,
|
2022-11-11 11:04:59 +01:00
|
|
|
);
|
2022-12-14 10:00:14 +01:00
|
|
|
|
|
|
|
return <BasePermissionButton {...props} access={access} ref={ref} />;
|
2023-10-02 14:25:46 +02:00
|
|
|
},
|
2022-12-14 10:00:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const BasePermissionButton: React.FC<IPermissionBaseButtonProps> =
|
|
|
|
React.forwardRef(
|
|
|
|
(
|
|
|
|
{
|
|
|
|
permission,
|
|
|
|
access,
|
|
|
|
variant = 'contained',
|
|
|
|
color = 'primary',
|
|
|
|
onClick,
|
|
|
|
children,
|
|
|
|
disabled,
|
|
|
|
projectId,
|
|
|
|
environmentId,
|
|
|
|
tooltipProps,
|
2023-06-21 15:26:07 +02:00
|
|
|
hideLockIcon,
|
2022-12-14 10:00:14 +01:00
|
|
|
...rest
|
|
|
|
},
|
2023-10-02 14:25:46 +02:00
|
|
|
ref,
|
2022-12-14 10:00:14 +01:00
|
|
|
) => {
|
|
|
|
const id = useId();
|
2023-06-29 19:01:27 +02:00
|
|
|
const endIcon = getEndIcon(access, rest.endIcon, hideLockIcon);
|
2022-12-14 10:00:14 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<TooltipResolver
|
|
|
|
{...tooltipProps}
|
|
|
|
title={formatAccessText(access, tooltipProps?.title)}
|
|
|
|
arrow
|
|
|
|
>
|
|
|
|
<span id={id}>
|
|
|
|
<Button
|
|
|
|
ref={ref}
|
|
|
|
onClick={onClick}
|
|
|
|
disabled={disabled || !access}
|
|
|
|
aria-labelledby={id}
|
|
|
|
variant={variant}
|
|
|
|
color={color}
|
|
|
|
{...rest}
|
2023-06-29 19:01:27 +02:00
|
|
|
endIcon={endIcon}
|
2022-12-14 10:00:14 +01:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Button>
|
|
|
|
</span>
|
|
|
|
</TooltipResolver>
|
|
|
|
);
|
2023-10-02 14:25:46 +02:00
|
|
|
},
|
2022-12-14 10:00:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const PermissionButton: React.FC<IPermissionButtonProps> = React.forwardRef(
|
|
|
|
(props, ref) => {
|
|
|
|
if (
|
|
|
|
typeof props.projectId !== 'undefined' &&
|
|
|
|
typeof props.environmentId !== 'undefined'
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<ProjectEnvironmentPermissionButton
|
|
|
|
{...props}
|
|
|
|
environmentId={props.environmentId}
|
|
|
|
projectId={props.projectId}
|
|
|
|
ref={ref}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return <RootPermissionButton {...props} ref={ref} />;
|
2023-10-02 14:25:46 +02:00
|
|
|
},
|
2022-11-11 11:04:59 +01:00
|
|
|
);
|
2021-10-08 11:23:29 +02:00
|
|
|
|
|
|
|
export default PermissionButton;
|