mirror of
https://github.com/SamyRai/turash.git
synced 2025-12-26 23:01:33 +00:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { usePermissions } from '@/hooks/usePermissions';
|
|
import { Permission } from '@/types/permissions';
|
|
import React from 'react';
|
|
|
|
export interface PermissionGateProps {
|
|
children: React.ReactNode;
|
|
permission: Permission | Permission[];
|
|
requireAll?: boolean;
|
|
fallback?: React.ReactNode;
|
|
showError?: boolean;
|
|
}
|
|
|
|
/**
|
|
* Component that conditionally renders children based on permissions
|
|
* Use this for hiding/showing UI elements based on permissions
|
|
*/
|
|
export const PermissionGate = ({
|
|
children,
|
|
permission,
|
|
requireAll = false,
|
|
fallback = null,
|
|
showError = false,
|
|
}: PermissionGateProps) => {
|
|
const { checkAnyPermission, checkAllPermissions } = usePermissions();
|
|
|
|
const permissions = Array.isArray(permission) ? permission : [permission];
|
|
const hasAccess = requireAll ? checkAllPermissions(permissions) : checkAnyPermission(permissions);
|
|
|
|
if (!hasAccess) {
|
|
if (showError) {
|
|
return (
|
|
<div className="text-sm text-destructive">
|
|
You don't have permission to view this content.
|
|
</div>
|
|
);
|
|
}
|
|
return <>{fallback}</>;
|
|
}
|
|
|
|
return <>{children}</>;
|
|
};
|