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 { supabase } from '@/lib/supabase'; 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 } = useAuth(); const navigate = useNavigate(); const displayName = user?.user_metadata?.full_name || ''; const [name, setName] = useState(displayName); const [deleteDialogOpen, setDeleteDialogOpen] = useState(false); const saveMutation = useMutation({ mutationFn: async () => { const { error } = await supabase.auth.updateUser({ data: { full_name: name } }); if (error) throw error; }, onSuccess: () => { toast.success(t('profile.saved')); }, onError: (error: Error) => { toast.error(error.message); }, }); const deleteMutation = useMutation({ mutationFn: deleteAccount, onSuccess: async () => { await signOut(); navigate('/login'); }, onError: (error: Error) => { toast.error(error.message); }, }); async function handleAvatarUpload(url: string) { await supabase.auth.updateUser({ data: { avatar_url: 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')} )} ); }