'use client' import { useLHSession } from '@components/Contexts/LHSessionContext' import { useOrg } from '@components/Contexts/OrgContext' import AddRole from '@components/Objects/Modals/Dash/OrgRoles/AddRole' import EditRole from '@components/Objects/Modals/Dash/OrgRoles/EditRole' import ConfirmationModal from '@components/Objects/StyledElements/ConfirmationModal/ConfirmationModal' import Modal from '@components/Objects/StyledElements/Modal/Modal' import { getAPIUrl } from '@services/config/config' import { deleteRole } from '@services/roles/roles' import { swrFetcher } from '@services/utils/ts/requests' import { Pencil, Shield, Users, X, Globe } from 'lucide-react' import React from 'react' import toast from 'react-hot-toast' import useSWR, { mutate } from 'swr' function OrgRoles() { const org = useOrg() as any const session = useLHSession() as any const access_token = session?.data?.tokens?.access_token; const [createRoleModal, setCreateRoleModal] = React.useState(false) const [editRoleModal, setEditRoleModal] = React.useState(false) const [selectedRole, setSelectedRole] = React.useState(null) as any const { data: roles } = useSWR( org ? `${getAPIUrl()}roles/org/${org.id}` : null, (url) => swrFetcher(url, access_token) ) const deleteRoleUI = async (role_id: any) => { const toastId = toast.loading("Deleting..."); const res = await deleteRole(role_id, org.id, access_token) if (res.status === 200) { mutate(`${getAPIUrl()}roles/org/${org.id}`) toast.success("Deleted role", {id:toastId}) } else { toast.error('Error deleting role', {id:toastId}) } } const handleEditRoleModal = (role: any) => { setSelectedRole(role) setEditRoleModal(!editRoleModal) } const getRightsSummary = (rights: any) => { if (!rights) return 'No permissions' const totalPermissions = Object.keys(rights).reduce((acc, key) => { if (typeof rights[key] === 'object') { return acc + Object.keys(rights[key]).filter(k => rights[key][k] === true).length } return acc }, 0) return `${totalPermissions} permissions` } // Check if a role is system-wide (TYPE_GLOBAL or role_uuid starts with role_global_) const isSystemRole = (role: any) => { // Check for role_type field first if (role.role_type === 'TYPE_GLOBAL') { return true } // Check for role_uuid starting with role_global_ if (role.role_uuid && role.role_uuid.startsWith('role_global_')) { return true } // Check for common system role IDs (1-4 are typically system roles) if (role.id && [1, 2, 3, 4].includes(role.id)) { return true } // Check if the role name indicates it's a system role if (role.name && ['Admin', 'Maintainer', 'Instructor', 'User'].includes(role.name)) { return true } return false } return ( <>

Manage Roles & Permissions

{' '} Roles define what users can do within your organization. Create custom roles with specific permissions for different user types.{' '}

{/* Mobile view - Cards */}
{roles?.map((role: any) => { const isSystem = isSystemRole(role) return (
{role.name} {isSystem && ( System-wide )}
{getRightsSummary(role.rights)}

{role.description || 'No description'}

{!isSystem ? ( <> handleEditRoleModal(role) } minHeight="lg" minWidth='xl' customWidth="max-w-7xl" dialogContent={ } dialogTitle="Edit Role" dialogDescription={ 'Edit the role permissions and details' } dialogTrigger={ } /> Delete } functionToExecute={() => { deleteRoleUI(role.id) }} status="warning" /> ) : null}
) })}
{/* Desktop view - Table */}
<> {roles?.map((role: any) => { const isSystem = isSystemRole(role) return ( ) })}
Role Name Description Permissions Actions
{role.name} {isSystem && ( System-wide )}
{role.description || 'No description'} {getRightsSummary(role.rights)}
{!isSystem ? ( <> handleEditRoleModal(role) } minHeight="lg" minWidth='xl' customWidth="max-w-7xl" dialogContent={ } dialogTitle="Edit Role" dialogDescription={ 'Edit the role permissions and details' } dialogTrigger={ } /> Delete } functionToExecute={() => { deleteRoleUI(role.id) }} status="warning" /> ) : null}
setCreateRoleModal(!createRoleModal)} minHeight="no-min" minWidth='xl' customWidth="max-w-7xl" dialogContent={ } dialogTitle="Create a Role" dialogDescription={ 'Create a new role with specific permissions' } dialogTrigger={ } />
) } export default OrgRoles