import React, { useState } from 'react' import { Search, Shield, UserX, UserCheck } from 'lucide-react' import { useAuthStore } from '../../store/authStore' import { useNotificationStore } from '../../store/notificationStore' import { useUIStore } from '../../store/uiStore' import { User, UserRole } from '../../types' import { Avatar } from '../ui/Avatar' import { RoleBadge } from '../ui/Badge' import { Button } from '../ui/Button' import { Modal } from '../ui/Modal' export const UserTable: React.FC = () => { const { allUsers, updateUserRole, currentUser } = useAuthStore() const { addNotification } = useNotificationStore() const { addToast } = useUIStore() const [search, setSearch] = useState('') const [confirmUser, setConfirmUser] = useState(null) const [confirmAction, setConfirmAction] = useState<'promote' | 'demote' | null>(null) const filtered = allUsers.filter( u => u.username.toLowerCase().includes(search.toLowerCase()) || u.email.toLowerCase().includes(search.toLowerCase()) || u.name.toLowerCase().includes(search.toLowerCase()) ) const handlePromote = (user: User) => { const newRole: UserRole = user.role === 'regular' ? 'writer' : user.role === 'writer' ? 'admin' : 'admin' updateUserRole(user.user_id, newRole) addNotification({ user_id: user.user_id, type: 'system', title: 'Role Updated', message: `Your account has been promoted to ${newRole}.`, }) addToast(`${user.username} promoted to ${newRole}`) setConfirmUser(null) } const handleDemote = (user: User) => { const newRole: UserRole = user.role === 'admin' ? 'writer' : 'regular' updateUserRole(user.user_id, newRole) addToast(`${user.username} role changed to ${newRole}`, 'info') setConfirmUser(null) } const formatDate = (str: string) => new Date(str).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }) return (
{/* Search */}
setSearch(e.target.value)} placeholder="Search users..." className="w-full pl-9 pr-4 py-2.5 bg-slate-800 border border-slate-700 rounded-xl text-sm text-white placeholder-slate-500 focus:outline-none focus:border-indigo-500" />
{/* Table */}
{filtered.map(user => ( ))}
User Email Role Joined Actions

{user.name} {user.surname}

@{user.username}

{user.email} {formatDate(user.created_at)}
{user.user_id !== currentUser?.user_id && user.role !== 'admin' && ( )} {user.user_id !== currentUser?.user_id && user.role !== 'regular' && ( )}
{filtered.length === 0 && (
No users found.
)}
{/* Confirm modal */} { setConfirmUser(null); setConfirmAction(null) }} title={confirmAction === 'promote' ? 'Promote User' : 'Demote User'} size="sm" > {confirmUser && (

{confirmAction === 'promote' ? `Promote @${confirmUser.username} to the next role tier?` : `Demote @${confirmUser.username} to a lower role?`}

)}
) }