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