import { IconLoader2 as Loader2, IconAlertTriangle as TriangleAlert } from '@tabler/icons-react'; import { useMutation } from '@tanstack/react-query'; import { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { useNavigate } from 'react-router'; import { toast } from 'sonner'; import { useAuth } from '@/components/auth/AuthProvider'; import { ContentPanel } from '@/components/ContentPanel'; import { AvatarUpload } from '@/components/FileUpload'; import { PageHeader } from '@/components/PageHeader'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogMedia, AlertDialogTitle, } from '@/components/ui/alert-dialog'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { apiJson } from '@/lib/api'; import { authErrorKey } from '@/lib/auth-errors'; async function deleteAccount(): Promise { await apiJson('/api/v1/me', { method: 'DELETE' }, 'Failed to delete account'); } export default function Profile() { const { t } = useTranslation(); const { user, isSuperAdmin, signOut, updateUser } = useAuth(); const navigate = useNavigate(); const displayName = user?.name || ''; const [name, setName] = useState(displayName); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const saveMutation = useMutation({ mutationFn: () => updateUser({ name: name.trim() }), onSuccess: () => { toast.success(t('profile.saved')); }, onError: (error: Error) => { toast.error(t(authErrorKey(error, 'profile.saveFailed'))); }, }); const deleteMutation = useMutation({ mutationFn: deleteAccount, onSuccess: async () => { await signOut(); navigate('/login'); }, onError: (error: Error) => { toast.error(error.message); }, }); // `url` is the app's own /api/files/avatars/... address, readable by anyone. async function handleAvatarUpload(url: string) { await updateUser({ image: url }); } return ( <> {t('profile.profileInfo')} {t('profile.profileInfoDescription')} {user && ( )}
setName(e.target.value)} placeholder={t('profile.namePlaceholder')} />

{t('profile.emailChangeHint')}

{t('profile.account')} {t('profile.accountDescription')}

{t('dashboard.userId')}

{user?.id}

{t('profile.deleteAccount')}

{t('profile.deleteAccountDescription')}

{t('profile.deleteAccountConfirmTitle')} {isSuperAdmin ? t('profile.superAdminCannotDelete') : t('profile.deleteAccountConfirmDescription')} {t('common.cancel')} {!isSuperAdmin && ( deleteMutation.mutate()} > {deleteMutation.isPending && } {t('profile.deleteAccountConfirmButton')} )} ); }