179 lines
7.2 KiB
TypeScript
179 lines
7.2 KiB
TypeScript
'use client';
|
|
|
|
import React, { useState } from 'react';
|
|
import { api } from '@/lib/api-client';
|
|
import { useAuth } from '@/context/auth-context';
|
|
import { UserRole } from '@fileshare/shared';
|
|
import { X, Trash2, AlertTriangle, Loader2, Building2, HardDrive, FileText } from 'lucide-react';
|
|
import { formatBytes } from '@/lib/utils';
|
|
|
|
interface DeleteUserModalProps {
|
|
isOpen: boolean;
|
|
onClose: () => void;
|
|
onSuccess: () => void;
|
|
user: any | null;
|
|
}
|
|
|
|
export const DeleteUserModal: React.FC<DeleteUserModalProps> = ({
|
|
isOpen,
|
|
onClose,
|
|
onSuccess,
|
|
user,
|
|
}) => {
|
|
const { user: currentUser } = useAuth();
|
|
const [isDeleting, setIsDeleting] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
if (!isOpen || !user) return null;
|
|
|
|
const isSuperAdmin = currentUser?.role === UserRole.SUPER_ADMIN;
|
|
const isSelf = currentUser?.id === user.id;
|
|
const isOtherCompany =
|
|
!isSuperAdmin &&
|
|
(user.companyName || 'Ecohub').trim().toLowerCase() !==
|
|
(currentUser?.companyName || 'Ecohub').trim().toLowerCase();
|
|
|
|
const handleDelete = async () => {
|
|
if (isSelf || isOtherCompany) return;
|
|
setError(null);
|
|
setIsDeleting(true);
|
|
|
|
try {
|
|
await api.deleteUser(user.id);
|
|
onSuccess();
|
|
onClose();
|
|
} catch (err: any) {
|
|
setError(err.message || 'Failed to delete user');
|
|
} finally {
|
|
setIsDeleting(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-900/60 backdrop-blur-sm animate-in fade-in duration-200">
|
|
<div className="w-full max-w-md bg-white border border-slate-200 rounded-2xl shadow-2xl overflow-hidden flex flex-col">
|
|
{/* Header */}
|
|
<div className="flex items-center justify-between px-6 py-4 border-b border-rose-100 bg-rose-50/50">
|
|
<div className="flex items-center gap-2.5">
|
|
<div className="w-9 h-9 rounded-xl bg-rose-100 border border-rose-200 flex items-center justify-center text-rose-600">
|
|
<Trash2 className="w-5 h-5" />
|
|
</div>
|
|
<div>
|
|
<h3 className="text-base font-bold text-slate-900">Delete User Account</h3>
|
|
<p className="text-xs text-rose-600 font-medium">Permanent System & Storage Cleanup</p>
|
|
</div>
|
|
</div>
|
|
<button
|
|
onClick={onClose}
|
|
className="p-1.5 rounded-lg text-slate-400 hover:text-slate-600 hover:bg-slate-100 transition-colors"
|
|
>
|
|
<X className="w-5 h-5" />
|
|
</button>
|
|
</div>
|
|
|
|
{/* Content */}
|
|
<div className="p-6 space-y-4 text-xs">
|
|
{error && (
|
|
<div className="p-3 rounded-xl bg-rose-50 border border-rose-200 text-rose-700 flex items-start gap-2">
|
|
<AlertTriangle className="w-4 h-4 text-rose-600 shrink-0 mt-0.5" />
|
|
<span>{error}</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* User Info Card */}
|
|
<div className="p-3.5 rounded-xl bg-slate-50 border border-slate-200 space-y-2">
|
|
<div className="flex items-center justify-between">
|
|
<span className="font-bold text-slate-900 text-sm">{user.fullName}</span>
|
|
<span
|
|
className={`px-2 py-0.5 rounded text-[10px] font-mono font-bold uppercase ${
|
|
user.role === UserRole.SUPER_ADMIN
|
|
? 'bg-rose-100 text-rose-700'
|
|
: user.role === UserRole.ADMIN
|
|
? 'bg-amber-100 text-amber-700'
|
|
: 'bg-slate-200 text-slate-700'
|
|
}`}
|
|
>
|
|
{user.role}
|
|
</span>
|
|
</div>
|
|
<div className="text-slate-500 font-mono text-[11px]">
|
|
@{user.username} • {user.email}
|
|
</div>
|
|
|
|
<div className="grid grid-cols-2 gap-2 pt-2 border-t border-slate-200/80 text-[11px]">
|
|
<div className="flex items-center gap-1.5 text-slate-600">
|
|
<Building2 className="w-3.5 h-3.5 text-emerald-600" />
|
|
<span className="font-medium truncate">{user.companyName || 'Ecohub'}</span>
|
|
</div>
|
|
<div className="flex items-center gap-1.5 text-slate-600">
|
|
<FileText className="w-3.5 h-3.5 text-sky-600" />
|
|
<span>{user.fileCount || 0} Files</span>
|
|
</div>
|
|
<div className="flex items-center gap-1.5 text-slate-600 col-span-2">
|
|
<HardDrive className="w-3.5 h-3.5 text-amber-600" />
|
|
<span>Storage: {formatBytes(Number(user.storageUsedBytes || 0))} used</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Warning Banner */}
|
|
<div className="p-3 rounded-xl bg-amber-50 border border-amber-200/80 text-amber-800 space-y-1">
|
|
<div className="flex items-center gap-1.5 font-bold text-amber-900">
|
|
<AlertTriangle className="w-4 h-4 text-amber-600 shrink-0" />
|
|
<span>Are you absolutely sure?</span>
|
|
</div>
|
|
<p className="text-[11px] text-amber-700 leading-relaxed">
|
|
This action <strong>cannot be undone</strong>. Deleting this account will permanently erase the user profile, all their personal folders, and purge all their uploaded physical files from storage.
|
|
</p>
|
|
</div>
|
|
|
|
{isSelf && (
|
|
<div className="p-3 rounded-xl bg-rose-50 border border-rose-200 text-rose-700 font-medium">
|
|
You are currently logged into this account. You cannot delete yourself.
|
|
</div>
|
|
)}
|
|
|
|
{isOtherCompany && (
|
|
<div className="p-3 rounded-xl bg-rose-50 border border-rose-200 text-rose-700 font-medium flex items-start gap-2">
|
|
<AlertTriangle className="w-4 h-4 text-rose-600 shrink-0 mt-0.5" />
|
|
<span>
|
|
Admins cannot delete users belonging to other companies ({user.companyName || 'Other Company'}). Only Super Admin has permission to delete external company accounts.
|
|
</span>
|
|
</div>
|
|
)}
|
|
|
|
{/* Actions */}
|
|
<div className="pt-2 flex justify-end gap-2.5 border-t border-slate-100">
|
|
<button
|
|
type="button"
|
|
onClick={onClose}
|
|
disabled={isDeleting}
|
|
className="px-4 py-2 rounded-xl bg-slate-100 hover:bg-slate-200 text-slate-700 font-semibold transition-colors"
|
|
>
|
|
Cancel
|
|
</button>
|
|
<button
|
|
type="button"
|
|
onClick={handleDelete}
|
|
disabled={isDeleting || isSelf || isOtherCompany}
|
|
className="px-4 py-2 rounded-xl bg-rose-600 hover:bg-rose-700 active:scale-95 text-white font-semibold flex items-center gap-1.5 shadow-sm shadow-rose-600/30 transition-all disabled:opacity-50 disabled:pointer-events-none"
|
|
>
|
|
{isDeleting ? (
|
|
<>
|
|
<Loader2 className="w-4 h-4 animate-spin" />
|
|
<span>Deleting User...</span>
|
|
</>
|
|
) : (
|
|
<>
|
|
<Trash2 className="w-4 h-4" />
|
|
<span>Delete User Permanently</span>
|
|
</>
|
|
)}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|