mirror of
https://github.com/Unleash/unleash.git
synced 2025-09-24 17:51:14 +02:00
This PR changes the environment permission switch to use the HTML tooltip instead of the default MUI tooltip. This aligns with how we've been doing tooltips recently. The main driver behind this change was that the project flag table used two different tooltips. This makes it so that they all look the same, but it also impacts other places that use the same switch. In feature flag table:  On flag page:  In project env table: 
107 lines
2.8 KiB
TypeScript
107 lines
2.8 KiB
TypeScript
import { Switch, type 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 | 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
|
|
variant='custom'
|
|
>
|
|
<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;
|