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
Thomas Heartman cfcf13980e
chore: use HTML (custom) tooltip for permission switches (#7355)
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:

![image](https://github.com/Unleash/unleash/assets/17786332/4d411285-c00e-41ec-95f9-9e6855d46661)

On flag page:

![image](https://github.com/Unleash/unleash/assets/17786332/11de1daf-7d0f-4214-8dc7-10b11631ce58)

In project env table:


![image](https://github.com/Unleash/unleash/assets/17786332/cc7a3a99-e48c-4989-9a14-2d5d4035a3cb)
2024-06-11 14:03:35 +02:00

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;