import { useState, useEffect, useCallback } from 'react' import { Plus, Pencil, UserX, Shield, User as UserIcon } from 'lucide-react' import { admin, type AdminUser } from '../../services/api' import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select' import { Alert, AlertDescription } from '@/components/ui/alert' import { ConfirmDialog } from '../dialogs/ConfirmDialog' export function UserManagement() { const [users, setUsers] = useState([]) const [loading, setLoading] = useState(true) const [showCreate, setShowCreate] = useState(false) const [editingUser, setEditingUser] = useState(null) const [deactivatingUser, setDeactivatingUser] = useState(null) const [error, setError] = useState(null) const fetchUsers = useCallback(async () => { try { const data = await admin.listUsers() setUsers(data.users) } catch { setError('Failed to load users.') } finally { setLoading(false) } }, []) useEffect(() => { fetchUsers() }, [fetchUsers]) const handleDeactivate = async () => { if (!deactivatingUser) return try { await admin.deleteUser(deactivatingUser.id) setDeactivatingUser(null) fetchUsers() } catch (err: any) { setError(err.response?.data?.detail ?? 'Failed to deactivate user.') setDeactivatingUser(null) } } if (loading) { return
Loading users…
} return (

Users

{error && ( {error} )} {users.map((u) => ( ))}
Username Role Photos Status Actions
{u.role === 'admin' ? ( ) : ( )} {u.username}
{u.role} {u.photo_count.toLocaleString()} {u.is_active ? 'Active' : 'Inactive'}
{u.is_active && ( )}
setShowCreate(false)} onCreated={() => { setShowCreate(false) fetchUsers() }} /> setEditingUser(null)} onSaved={() => { setEditingUser(null) fetchUsers() }} /> setDeactivatingUser(null)} />
) } // ── Create User Modal ────────────────────────────────────────────────── function CreateUserModal({ open, onClose, onCreated, }: { open: boolean onClose: () => void onCreated: () => void }) { const [username, setUsername] = useState('') const [password, setPassword] = useState('') const [role, setRole] = useState<'user' | 'admin'>('user') const [error, setError] = useState(null) const [loading, setLoading] = useState(false) useEffect(() => { if (open) { setUsername('') setPassword('') setRole('user') setError(null) setLoading(false) } }, [open]) const handleSubmit = async () => { setError(null) setLoading(true) try { await admin.createUser({ username: username.trim(), password, role }) onCreated() } catch (err: any) { setError(err.response?.data?.detail ?? 'Failed to create user.') } finally { setLoading(false) } } return ( !o && onClose()}> Add User {error && ( {error} )}
setUsername(e.target.value)} autoFocus />
setPassword(e.target.value)} />
) } // ── Edit User Modal ──────────────────────────────────────────────────── function EditUserModal({ user, onClose, onSaved, }: { user: AdminUser | null onClose: () => void onSaved: () => void }) { const [role, setRole] = useState<'user' | 'admin'>('user') const [newPassword, setNewPassword] = useState('') const [error, setError] = useState(null) const [loading, setLoading] = useState(false) useEffect(() => { if (user) { setRole(user.role as 'user' | 'admin') setNewPassword('') setError(null) setLoading(false) } }, [user]) const handleSubmit = async () => { if (!user) return setError(null) setLoading(true) try { const data: { role?: string; new_password?: string } = {} if (role !== user.role) data.role = role if (newPassword) data.new_password = newPassword if (Object.keys(data).length > 0) { await admin.updateUser(user.id, data) } onSaved() } catch (err: any) { setError(err.response?.data?.detail ?? 'Failed to update user.') } finally { setLoading(false) } } return ( !o && onClose()}> Edit: {user?.username} {error && ( {error} )}
setNewPassword(e.target.value)} placeholder="Unchanged" />
) }