misc_pterodactyl-panel/resources/scripts/components/elements/Can.tsx

26 lines
665 B
TypeScript
Raw Normal View History

2022-11-25 20:25:03 +00:00
import type { ReactNode } from 'react';
import { memo } from 'react';
import isEqual from 'react-fast-compare';
2022-11-25 20:25:03 +00:00
import { usePermissions } from '@/plugins/usePermissions';
interface Props {
action: string | string[];
matchAny?: boolean;
2022-11-25 20:25:03 +00:00
renderOnError?: ReactNode | null;
children: ReactNode;
}
2022-11-25 20:25:03 +00:00
function Can({ action, matchAny = false, renderOnError, children }: Props) {
2020-03-29 21:19:17 +00:00
const can = usePermissions(action);
return (
<>
2022-11-25 20:25:03 +00:00
{(matchAny && can.filter(p => p).length > 0) || (!matchAny && can.every(p => p)) ? children : renderOnError}
</>
);
2022-11-25 20:25:03 +00:00
}
const MemoizedCan = memo(Can, isEqual);
2022-11-25 20:25:03 +00:00
export default MemoizedCan;