import { useEffect, useState } from "react" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Checkbox } from "@/components/ui/checkbox" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table" import { CheckCircle, Trash2, RefreshCw, Users, Search, ChevronLeft, ChevronRight, Mail, AlertCircle, XCircle, } from "lucide-react" import { Input } from "@/components/ui/input" import { PendingUser, EmailVerificationSettings } from "../../config" import { useEmailVerification } from "../../hooks/use-email-verification" interface PendingUsersTabProps { verificationMode: EmailVerificationSettings['verificationMode'] } export function PendingUsersTab({ verificationMode }: PendingUsersTabProps) { const { pendingUsers, pendingTotal, pendingStats, loadingUsers, loadPendingUsers, verifyUser, deleteUser, resendVerification, bulkVerify, bulkDelete, approveUser, bulkApprove, rejectUser, bulkReject, } = useEmailVerification() const isApprovalMode = verificationMode === 'admin_approval' const [selectedUsers, setSelectedUsers] = useState([]) const [searchQuery, setSearchQuery] = useState("") const [currentPage, setCurrentPage] = useState(1) const [actionLoading, setActionLoading] = useState<{ [key: string]: boolean }>({}) const itemsPerPage = 10 useEffect(() => { const request = window.setTimeout(() => loadPendingUsers(currentPage, itemsPerPage, searchQuery), 250) return () => window.clearTimeout(request) }, [currentPage, searchQuery, loadPendingUsers]) const totalPages = Math.max(1, Math.ceil(pendingTotal / itemsPerPage)) const paginatedUsers = pendingUsers useEffect(() => { if (currentPage > totalPages) setCurrentPage(totalPages) }, [currentPage, totalPages]) const toggleSelectAll = () => { if (selectedUsers.length === paginatedUsers.length) { setSelectedUsers([]) } else { setSelectedUsers(paginatedUsers.map((u: PendingUser) => u.id)) } } const toggleSelectUser = (userId: number) => { if (selectedUsers.includes(userId)) { setSelectedUsers(selectedUsers.filter(id => id !== userId)) } else { setSelectedUsers([...selectedUsers, userId]) } } const handleVerifyUser = async (userId: number) => { setActionLoading({ ...actionLoading, [`verify-${userId}`]: true }) if (isApprovalMode) { await approveUser(userId) } else { await verifyUser(userId) } await loadPendingUsers(currentPage, itemsPerPage, searchQuery) setActionLoading({ ...actionLoading, [`verify-${userId}`]: false }) } const handleDeleteUser = async (userId: number) => { if (!confirm('Are you sure you want to delete this user?')) return setActionLoading({ ...actionLoading, [`delete-${userId}`]: true }) await deleteUser(userId) await loadPendingUsers(currentPage, itemsPerPage, searchQuery) setActionLoading({ ...actionLoading, [`delete-${userId}`]: false }) } const handleResendVerification = async (userId: number) => { setActionLoading({ ...actionLoading, [`resend-${userId}`]: true }) await resendVerification(userId) await loadPendingUsers(currentPage, itemsPerPage, searchQuery) setActionLoading({ ...actionLoading, [`resend-${userId}`]: false }) } const handleBulkVerify = async () => { if (selectedUsers.length === 0) return setActionLoading({ ...actionLoading, bulkVerify: true }) if (isApprovalMode) { await bulkApprove(selectedUsers) } else { await bulkVerify(selectedUsers) } await loadPendingUsers(currentPage, itemsPerPage, searchQuery) setSelectedUsers([]) setActionLoading({ ...actionLoading, bulkVerify: false }) } const handleBulkDelete = async () => { if (selectedUsers.length === 0) return if (!confirm(`Are you sure you want to delete ${selectedUsers.length} user(s)?`)) return setActionLoading({ ...actionLoading, bulkDelete: true }) await bulkDelete(selectedUsers) await loadPendingUsers(currentPage, itemsPerPage, searchQuery) setSelectedUsers([]) setActionLoading({ ...actionLoading, bulkDelete: false }) } const handleRejectUser = async (userId: number) => { if (!confirm('Are you sure you want to reject this user? They will be notified and their account will be deleted.')) return setActionLoading({ ...actionLoading, [`reject-${userId}`]: true }) await rejectUser(userId) await loadPendingUsers(currentPage, itemsPerPage, searchQuery) setActionLoading({ ...actionLoading, [`reject-${userId}`]: false }) } const handleBulkReject = async () => { if (selectedUsers.length === 0) return if (!confirm(`Are you sure you want to reject ${selectedUsers.length} user(s)? They will be notified and their accounts will be deleted.`)) return setActionLoading({ ...actionLoading, bulkReject: true }) await bulkReject(selectedUsers) await loadPendingUsers(currentPage, itemsPerPage, searchQuery) setSelectedUsers([]) setActionLoading({ ...actionLoading, bulkReject: false }) } const formatDate = (dateString: string) => { return new Date(dateString).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit' }) } const isExpired = (expiryDate: string) => { return new Date(expiryDate) < new Date() } return (
{/* Stats */}

{pendingStats.pending}

{verificationMode === 'admin_approval' ? 'Pending Approval' : 'Pending Verification'}

{pendingStats.expired}

Expired Links

{pendingStats.resends}

Emails Resent

{/* User Table */}
Pending Users {verificationMode === 'admin_approval' ? 'Users waiting for admin approval' : 'Users waiting for email verification' }
{ setSearchQuery(e.target.value) setCurrentPage(1) }} />
{/* Bulk Actions - PRO */} {selectedUsers.length > 0 && (
{selectedUsers.length} user(s) selected
{isApprovalMode && ( )}
)} {/* Table */} {loadingUsers ? (
) : paginatedUsers.length === 0 ? (

No pending users

{searchQuery ? "No users match your search criteria" : verificationMode === 'admin_approval' ? "All users have been reviewed" : "All users have verified their email addresses"}

) : (
0} onCheckedChange={toggleSelectAll} /> User Status Registered Expires Resent Actions {paginatedUsers.map((user: PendingUser) => ( toggleSelectUser(user.id)} />

{user.username}

{user.email}

{user.status === 'verified' ? (verificationMode === 'admin_approval' ? 'Approved' : 'Verified') : (verificationMode === 'admin_approval' ? 'Pending Approval' : 'Pending') } {formatDate(user.registeredAt)} {isExpired(user.expiresAt) ? 'Expired' : formatDate(user.expiresAt)} {user.resendCount}x
{verificationMode === 'email_verification' && ( )} {isApprovalMode && ( )}
))}
)} {/* Pagination */} {totalPages > 1 && (

Showing {((currentPage - 1) * itemsPerPage) + 1} to {Math.min(currentPage * itemsPerPage, pendingTotal)} of {pendingTotal} users

Page {currentPage} of {totalPages}
)}
) }