2022-11-25 20:25:03 +00:00
|
|
|
import type { ReactNode } from 'react';
|
|
|
|
|
2022-06-12 15:36:55 +00:00
|
|
|
import { ServerError } from '@/components/elements/ScreenBlock';
|
2022-11-25 20:25:03 +00:00
|
|
|
import { usePermissions } from '@/plugins/usePermissions';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
children?: ReactNode;
|
|
|
|
|
|
|
|
permission?: string | string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
function PermissionRoute({ children, permission }: Props): JSX.Element {
|
|
|
|
if (permission === undefined) {
|
|
|
|
return <>{children}</>;
|
|
|
|
}
|
|
|
|
|
|
|
|
const can = usePermissions(permission);
|
|
|
|
|
|
|
|
if (can.filter(p => p).length > 0) {
|
|
|
|
return <>{children}</>;
|
|
|
|
}
|
2022-06-12 15:36:55 +00:00
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
return <ServerError title="Access Denied" message="You do not have permission to access this page." />;
|
2022-06-12 15:36:55 +00:00
|
|
|
}
|
|
|
|
|
2022-11-25 20:25:03 +00:00
|
|
|
export default PermissionRoute;
|