import React, { useMemo } from 'react'; import yaml from 'js-yaml'; import { Check } from 'lucide-react'; function classNames(...classes) { return classes.filter(Boolean).join(' '); } export default function PermissionTable({ data }) { const { roles, permissions, matrix } = useMemo(() => { if (!data) return { roles: [], permissions: [], matrix: {} }; let parsedData = data; if (typeof data === 'string') { try { parsedData = yaml.load(data); } catch (e) { console.error('Failed to parse YAML data:', e); return { roles: [], permissions: [], matrix: {} }; } } // 1. Identify all sources. const sources = [ parsedData?.InternalAuthZ?.RolePermissionMappings, parsedData?.SystemAuthZ?.RolePermissionMappings, ].filter(Boolean).flat(); // 2. Aggregate Data const roleMap = new Map(); const allPermissions = new Set(); sources.forEach((mapping) => { const roleName = mapping.Role; const rolePerms = mapping.Permissions || []; if (!roleMap.has(roleName)) { roleMap.set(roleName, new Set()); } rolePerms.forEach((perm) => { roleMap.get(roleName).add(perm); allPermissions.add(perm); }); }); // 3. Sort for display const sortedRoles = Array.from(roleMap.keys()).sort(); const sortedPermissions = Array.from(allPermissions).sort(); return { roles: sortedRoles, permissions: sortedPermissions, matrix: roleMap, }; }, [data]); if (!roles.length) { return
| Permission | {roles.map((role) => ({role.replace(/_/g, ' ')} | ))}
|---|---|
| {perm} | {roles.map((role) => { const hasPermission = matrix.get(role).has(perm); return (
{hasPermission ? (
|
);
})}