import { useState, useEffect } from 'react' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Alert, AlertDescription } from '@/components/ui/alert' import { User, ApiResponse } from 'shared/types' import { UserForm } from './user-form' import { makeAuthenticatedRequest, authStorage } from '@/lib/auth' import { Plus, Edit, Trash2, Calendar, AlertCircle, Loader2, Shield, User as UserIcon } from 'lucide-react' export function UserList() { const [users, setUsers] = useState([]) const [loading, setLoading] = useState(false) const [showForm, setShowForm] = useState(false) const [editingUser, setEditingUser] = useState(null) const [error, setError] = useState('') const currentUser = authStorage.getUser() const fetchUsers = async () => { setLoading(true) setError('') try { const response = await makeAuthenticatedRequest('/api/users') const data: ApiResponse = await response.json() if (data.success && data.data) { setUsers(data.data) } else { setError('Failed to load users') } } catch (error) { console.error('Failed to fetch users:', error) setError('Failed to connect to server') } finally { setLoading(false) } } const handleDelete = async (id: string, email: string) => { if (!confirm(`Are you sure you want to delete user "${email}"? This action cannot be undone.`)) return try { const response = await makeAuthenticatedRequest(`/api/users/${id}`, { method: 'DELETE', }) if (response.ok) { fetchUsers() } else if (response.status === 403) { setError('You cannot delete this user') } else { setError('Failed to delete user') } } catch (error) { console.error('Failed to delete user:', error) setError('Failed to delete user') } } const handleEdit = (user: User) => { setEditingUser(user) setShowForm(true) } const handleFormSuccess = () => { setShowForm(false) setEditingUser(null) fetchUsers() } useEffect(() => { fetchUsers() }, []) return (

Users

Manage user accounts and permissions

{currentUser?.is_admin && ( )}
{error && ( {error} )} {loading ? (
Loading users...
) : users.length === 0 ? (

No users found

Get started by creating the first user account.

{currentUser?.is_admin && ( )}
) : (
{users.map((user) => (
{user.is_admin ? ( ) : ( )} {user.email} {user.is_admin ? "Admin" : "User"}
Joined {new Date(user.created_at).toLocaleDateString()}
{currentUser?.is_admin && currentUser.id !== user.id && ( )}
))}
)} { setShowForm(false) setEditingUser(null) }} onSuccess={handleFormSuccess} user={editingUser} />
) }