mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	Based on this article: https://mui.com/material-ui/guides/minimizing-bundle-size/ importing from `'@mui/icons-material'` instead of specifying the actual path to the icon like `import Delete from '@mui/icons-material/Delete';` can be up to six time slower. This change changes all named imports in Unleash referencing the `@mui/icons-material` to default imports. This reduced the amount of modules we had to process when building the frontend from 15206 to 4746 Before: <img width="1016" alt="Skjermbilde 2024-03-11 kl 14 19 58" src="https://github.com/Unleash/unleash/assets/16081982/f137d24a-6557-4183-a40f-f62a33524520"> After: <img width="1237" alt="Skjermbilde 2024-03-11 kl 14 20 32" src="https://github.com/Unleash/unleash/assets/16081982/05a27d6a-2c3f-4409-9862-7188ab4b9c72"> Build time locally decreased by around 50% Before: <img width="1504" alt="Skjermbilde 2024-03-11 kl 14 31 45" src="https://github.com/Unleash/unleash/assets/16081982/bc931559-b022-47ed-9f8f-c87401578518"> After: <img width="1219" alt="Skjermbilde 2024-03-11 kl 14 27 00" src="https://github.com/Unleash/unleash/assets/16081982/3c3a8d6b-576d-45c3-aa40-cc5f95d9df2b">
		
			
				
	
	
		
			140 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			140 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { Button, ButtonProps } from '@mui/material';
 | |
| import Lock from '@mui/icons-material/Lock';
 | |
| import React from 'react';
 | |
| import {
 | |
|     TooltipResolver,
 | |
|     ITooltipResolverProps,
 | |
| } from 'component/common/TooltipResolver/TooltipResolver';
 | |
| import { formatAccessText } from 'utils/formatAccessText';
 | |
| import { useId } from 'hooks/useId';
 | |
| import {
 | |
|     useHasRootAccess,
 | |
|     useHasProjectEnvironmentAccess,
 | |
| } from 'hooks/useHasAccess';
 | |
| 
 | |
| export interface IPermissionButtonProps extends Omit<ButtonProps, 'title'> {
 | |
|     permission: string | string[];
 | |
|     onClick?: (e: any) => void;
 | |
|     disabled?: boolean;
 | |
|     projectId?: string;
 | |
|     environmentId?: string;
 | |
|     tooltipProps?: Omit<ITooltipResolverProps, 'children'>;
 | |
|     hideLockIcon?: boolean;
 | |
| }
 | |
| 
 | |
| interface IPermissionBaseButtonProps extends IPermissionButtonProps {
 | |
|     access: boolean;
 | |
| }
 | |
| 
 | |
| export interface IProjectPermissionButtonProps extends IPermissionButtonProps {
 | |
|     projectId: string;
 | |
|     environmentId: string;
 | |
| }
 | |
| 
 | |
| const getEndIcon = (
 | |
|     access: boolean,
 | |
|     fallBackIcon?: React.ReactNode,
 | |
|     hideLockIcon?: boolean,
 | |
| ): React.ReactNode => {
 | |
|     if (!access && !hideLockIcon) {
 | |
|         return <Lock titleAccess='Locked' />;
 | |
|     }
 | |
| 
 | |
|     if (fallBackIcon) {
 | |
|         return fallBackIcon;
 | |
|     }
 | |
| 
 | |
|     return null;
 | |
| };
 | |
| 
 | |
| const ProjectEnvironmentPermissionButton: React.FC<IProjectPermissionButtonProps> =
 | |
|     React.forwardRef((props, ref) => {
 | |
|         const access = useHasProjectEnvironmentAccess(
 | |
|             props.permission,
 | |
|             props.projectId,
 | |
|             props.environmentId,
 | |
|         );
 | |
| 
 | |
|         return <BasePermissionButton {...props} access={access} ref={ref} />;
 | |
|     });
 | |
| 
 | |
| const RootPermissionButton: React.FC<IPermissionButtonProps> = React.forwardRef(
 | |
|     (props, ref) => {
 | |
|         const access = useHasRootAccess(
 | |
|             props.permission,
 | |
|             props.projectId,
 | |
|             props.environmentId,
 | |
|         );
 | |
| 
 | |
|         return <BasePermissionButton {...props} access={access} ref={ref} />;
 | |
|     },
 | |
| );
 | |
| 
 | |
| const BasePermissionButton: React.FC<IPermissionBaseButtonProps> =
 | |
|     React.forwardRef(
 | |
|         (
 | |
|             {
 | |
|                 permission,
 | |
|                 access,
 | |
|                 variant = 'contained',
 | |
|                 color = 'primary',
 | |
|                 onClick,
 | |
|                 children,
 | |
|                 disabled,
 | |
|                 projectId,
 | |
|                 environmentId,
 | |
|                 tooltipProps,
 | |
|                 hideLockIcon,
 | |
|                 ...rest
 | |
|             },
 | |
|             ref,
 | |
|         ) => {
 | |
|             const id = useId();
 | |
|             const endIcon = getEndIcon(access, rest.endIcon, hideLockIcon);
 | |
| 
 | |
|             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}
 | |
|                             endIcon={endIcon}
 | |
|                         >
 | |
|                             {children}
 | |
|                         </Button>
 | |
|                     </span>
 | |
|                 </TooltipResolver>
 | |
|             );
 | |
|         },
 | |
|     );
 | |
| 
 | |
| 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} />;
 | |
|     },
 | |
| );
 | |
| 
 | |
| export default PermissionButton;
 |