1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-24 17:51:14 +02:00
unleash.unleash/frontend/src/component/common/PermissionSwitch/PermissionSwitch.tsx
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

103 lines
2.7 KiB
TypeScript

import { Switch, SwitchProps } from '@mui/material';
import React from 'react';
import { formatAccessText } from 'utils/formatAccessText';
import { TooltipResolver } from 'component/common/TooltipResolver/TooltipResolver';
import {
useHasProjectEnvironmentAccess,
useHasRootAccess,
} from 'hooks/useHasAccess';
interface IPermissionSwitchProps extends SwitchProps {
permission: string;
tooltip?: string;
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => void;
disabled?: boolean;
projectId?: string;
environmentId?: string;
checked: boolean;
}
interface IBasePermissionSwitchProps extends IPermissionSwitchProps {
access: boolean;
}
const ProjectenvironmentPermissionSwitch = React.forwardRef<
HTMLButtonElement,
IPermissionSwitchProps & { projectId: string; environmentId: string }
>((props, ref) => {
const access = useHasProjectEnvironmentAccess(
props.permission,
props.projectId,
props.environmentId,
);
return <BasePermissionSwitch {...props} access={access} ref={ref} />;
});
const RootPermissionSwitch = React.forwardRef<
HTMLButtonElement,
IPermissionSwitchProps
>((props, ref) => {
const access = useHasRootAccess(
props.permission,
props.projectId,
props.environmentId,
);
return <BasePermissionSwitch {...props} access={access} ref={ref} />;
});
const BasePermissionSwitch = React.forwardRef<
HTMLButtonElement,
IBasePermissionSwitchProps
>((props, ref) => {
const {
access,
permission,
tooltip,
disabled,
projectId,
environmentId,
checked,
onChange,
...rest
} = props;
return (
<TooltipResolver title={formatAccessText(access, tooltip)} arrow>
<span data-loading>
<Switch
data-testid='toggle-switch'
onChange={onChange}
disabled={disabled || !access}
checked={checked}
ref={ref}
{...rest}
/>
</span>
</TooltipResolver>
);
});
const PermissionSwitch = React.forwardRef<
HTMLButtonElement,
IPermissionSwitchProps
>((props, ref) => {
if (
typeof props.projectId !== 'undefined' &&
typeof props.environmentId !== 'undefined'
) {
return (
<ProjectenvironmentPermissionSwitch
{...props}
projectId={props.projectId}
environmentId={props.environmentId}
ref={ref}
/>
);
}
return <RootPermissionSwitch {...props} ref={ref} />;
});
export default PermissionSwitch;