'use client' import { useEffect, useState } from 'react' import { RefreshCw, Plus, Trash2, KeyRound } from 'lucide-react' import { api } from '@/config/api' import { useAuth } from '@/hooks/useAuth' import { ensureErrorMessage } from '@/helpers/ensureErrorMessage' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { Alert, AlertDescription } from '@/components/ui/alert' import { Skeleton } from '@/components/ui/skeleton' import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from '@/components/ui/alert-dialog' type UserRecord = { id: string username: string displayName: string createdAt: string } export const UsersPage = () => { const { user: currentUser } = useAuth() const [users, setUsers] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [success, setSuccess] = useState(null) // Form state const [formUsername, setFormUsername] = useState('') const [formPassword, setFormPassword] = useState('') const [formDisplayName, setFormDisplayName] = useState('') // Change password state const [changingPasswordId, setChangingPasswordId] = useState(null) const [newPassword, setNewPassword] = useState('') const canSubmit = formUsername.trim().length >= 3 && formPassword.length >= 8 && formDisplayName.trim().length > 0 const load = async () => { setLoading(true) setError(null) try { const res = await api.get('/admin/users') setUsers(res.data) } catch (err) { setError(ensureErrorMessage(err)) } finally { setLoading(false) } } useEffect(() => { void load() }, []) const onCreate = async () => { if (!canSubmit) return setLoading(true) setError(null) setSuccess(null) try { await api.post('/admin/users', { username: formUsername.trim(), password: formPassword, displayName: formDisplayName.trim() }) setFormUsername('') setFormPassword('') setFormDisplayName('') setSuccess('Utilisateur créé') await load() } catch (err) { const msg = ensureErrorMessage(err) if (msg.includes('username_taken')) { setError('Ce nom d\'utilisateur est déjà pris.') } else { setError(msg) } } finally { setLoading(false) } } const onChangePassword = async (id: string) => { if (newPassword.length < 8) return setLoading(true) setError(null) setSuccess(null) try { await api.patch(`/admin/users/${id}/password`, { password: newPassword }) setChangingPasswordId(null) setNewPassword('') setSuccess('Mot de passe modifié') } catch (err) { setError(ensureErrorMessage(err)) } finally { setLoading(false) } } const onDelete = async (id: string) => { setLoading(true) setError(null) setSuccess(null) try { await api.delete(`/admin/users/${id}`) setSuccess('Utilisateur supprimé') await load() } catch (err) { const msg = ensureErrorMessage(err) if (msg.includes('cannot_delete_self')) { setError('Vous ne pouvez pas supprimer votre propre compte.') } else { setError(msg) } } finally { setLoading(false) } } return (

Utilisateurs

Gérer les comptes utilisateurs

{error ? ( {error} ) : null} {success ? ( {success} ) : null} {/* Formulaire d'ajout */} Créer un utilisateur
setFormUsername(e.target.value)} placeholder="min. 3 caractères" autoCapitalize="none" autoCorrect="off" className="h-12" />
setFormDisplayName(e.target.value)} placeholder="Prénom ou surnom" className="h-12" />
setFormPassword(e.target.value)} placeholder="min. 8 caractères" className="h-12" />
{/* Liste des utilisateurs */} {loading && users.length === 0 ? (
) : ( Comptes ({users.length}) {users.length === 0 ? (

Aucun utilisateur

) : (
{users.map((u) => { const isSelf = currentUser?.id === u.id return (

{u.displayName} {isSelf ? ( (vous) ) : null}

{u.username}

{changingPasswordId === u.id ? (
setNewPassword(e.target.value)} placeholder="Nouveau mot de passe (min. 8)" className="h-9 text-sm" autoFocus />
) : null}
{changingPasswordId !== u.id ? (
Supprimer cet utilisateur ? Le compte {u.displayName} ({u.username}) sera définitivement supprimé. Annuler void onDelete(u.id)}> Supprimer
) : null}
) })}
)}
)}
) }