feat: first commit
This commit is contained in:
40
admin/src/components/PermissionWrapper/index.tsx
Normal file
40
admin/src/components/PermissionWrapper/index.tsx
Normal file
@@ -0,0 +1,40 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import authentication, { AuthParams } from '@/utils/authentication';
|
||||
import {useStore} from '@/store';
|
||||
|
||||
type PermissionWrapperProps = AuthParams & {
|
||||
backup?: React.ReactNode;
|
||||
};
|
||||
|
||||
const PermissionWrapper = (
|
||||
props: React.PropsWithChildren<PermissionWrapperProps>
|
||||
) => {
|
||||
const { backup, requiredPermissions, oneOfPerm } = props;
|
||||
const [hasPermission, setHasPermission] = useState(false);
|
||||
const userInfo = useStore().userInfo
|
||||
|
||||
useEffect(() => {
|
||||
const hasPermission = authentication(
|
||||
{ requiredPermissions, oneOfPerm },
|
||||
userInfo.permissions
|
||||
);
|
||||
setHasPermission(hasPermission);
|
||||
}, [requiredPermissions, oneOfPerm, userInfo.permissions]);
|
||||
|
||||
if (hasPermission) {
|
||||
return <>{convertReactElement(props.children)}</>;
|
||||
}
|
||||
if (backup) {
|
||||
return <>{convertReactElement(backup)}</>;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
function convertReactElement(node: React.ReactNode): React.ReactElement {
|
||||
if (!React.isValidElement(node)) {
|
||||
return <>{node}</>;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
export default PermissionWrapper;
|
||||
Reference in New Issue
Block a user