import { useEffect, useState } from 'react'; import type { ReactElement } from 'react'; import { Loader2 } from 'lucide-react'; import { SecondaryButton, StatusBadge, Modal } from '@/components/shared'; import type { Role } from '@/types/role'; interface ViewRoleModalProps { isOpen: boolean; onClose: () => void; roleId: string | null; onLoadRole: (id: string) => Promise; } // Helper function to get scope badge variant const getScopeVariant = (scope: string): 'success' | 'failure' | 'process' => { switch (scope.toLowerCase()) { case 'platform': return 'success'; case 'tenant': return 'process'; case 'module': return 'failure'; default: return 'success'; } }; // Helper function to format date const formatDate = (dateString: string): string => { const date = new Date(dateString); return date.toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit', }); }; export const ViewRoleModal = ({ isOpen, onClose, roleId, onLoadRole, }: ViewRoleModalProps): ReactElement | null => { const [role, setRole] = useState(null); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); // Load role data when modal opens useEffect(() => { if (isOpen && roleId) { const loadRole = async (): Promise => { try { setIsLoading(true); setError(null); const data = await onLoadRole(roleId); setRole(data); } catch (err: any) { setError(err?.response?.data?.error?.message || 'Failed to load role details'); } finally { setIsLoading(false); } }; loadRole(); } else { setRole(null); setError(null); } }, [isOpen, roleId, onLoadRole]); return ( Close } > {isLoading && (
)} {error && (

{error}

)} {!isLoading && !error && role && (
{/* Basic Information */}

Basic Information

{role.name}

{role.code}

{role.scope}

{role.id}

{/* Description */} {role.description && (

Description

{role.description}

)} {/* Timestamps */}

Timestamps

{formatDate(role.created_at)}

{formatDate(role.updated_at)}

)}
); };