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/PermissionHOC/PermissionHOC.tsx
Christopher Kolstad 5a3bb1ffc3
Biome1.5.1 (#5867)
Lots of work here, mostly because I didn't want to turn off the
`noImplicitAnyLet` lint. This PR tries its best to type all the untyped
lets biome complained about (Don't ask me how many hours that took or
how many lints that was >200...), which in the future will force test
authors to actually type their global variables setup in `beforeAll`.

---------

Co-authored-by: Gastón Fournier <gaston@getunleash.io>
2024-01-12 09:25:59 +00:00

46 lines
1.2 KiB
TypeScript

import { useContext, FC, ReactElement } from 'react';
import AccessContext from 'contexts/AccessContext';
import {
ITooltipResolverProps,
TooltipResolver,
} from 'component/common/TooltipResolver/TooltipResolver';
import { formatAccessText } from 'utils/formatAccessText';
type IPermissionHOCProps = {
permission: string;
projectId?: string;
environmentId?: string;
tooltip?: string;
tooltipProps?: Omit<ITooltipResolverProps, 'children' | 'title'>;
children: ({ hasAccess }: { hasAccess?: boolean }) => ReactElement;
};
export const PermissionHOC: FC<IPermissionHOCProps> = ({
permission,
projectId,
children,
environmentId,
tooltip,
tooltipProps,
}) => {
const { hasAccess } = useContext(AccessContext);
let access: boolean;
if (projectId && environmentId) {
access = hasAccess(permission, projectId, environmentId);
} else if (projectId) {
access = hasAccess(permission, projectId);
} else {
access = hasAccess(permission);
}
return (
<TooltipResolver
{...tooltipProps}
title={formatAccessText(access, tooltip)}
>
{children({ hasAccess: access })}
</TooltipResolver>
);
};