import { useRef, useState } from "react"; import { titleCase } from "text-case"; import Admin from "@/models/admin"; import EditUserModal from "./EditUserModal"; import showToast from "@/utils/toast"; import { useModal } from "@/hooks/useModal"; import ModalWrapper from "@/components/ModalWrapper"; const ModMap = { admin: ["admin", "manager", "default"], manager: ["manager", "default"], default: [], }; export default function UserRow({ currUser, user }) { const rowRef = useRef(null); const canModify = ModMap[currUser?.role || "default"].includes(user.role); const [suspended, setSuspended] = useState(user.suspended === 1); const { isOpen, openModal, closeModal } = useModal(); const handleSuspend = async () => { if ( !window.confirm( `Are you sure you want to suspend ${user.username}?\nAfter you do this they will be logged out and unable to log back into this instance of AnythingLLM until unsuspended by an admin.` ) ) return false; const { success, error } = await Admin.updateUser(user.id, { suspended: suspended ? 0 : 1, }); if (!success) showToast(error, "error", { clear: true }); if (success) { showToast( `User ${!suspended ? "has been suspended" : "is no longer suspended"}.`, "success", { clear: true } ); setSuspended(!suspended); } }; const handleDelete = async () => { if ( !window.confirm( `Are you sure you want to delete ${user.username}?\nAfter you do this they will be logged out and unable to use this instance of AnythingLLM.\n\nThis action is irreversible.` ) ) return false; const { success, error } = await Admin.deleteUser(user.id); if (!success) showToast(error, "error", { clear: true }); if (success) { rowRef?.current?.remove(); showToast("User deleted from system.", "success", { clear: true }); } }; return ( <> {user.username} {titleCase(user.role)} {user.createdAt} {canModify && ( )} {currUser?.id !== user.id && canModify && ( <> )} ); }