import { useEffect, useState } from 'react'; import type { FormEvent } from 'react'; import { Alert, LoadingBlock, errorMessage } from '../components/Feedback'; import { useAuth } from '../auth/AuthContext'; import { initials } from '../lib/format'; import { getSelfProfile, getSelfSmtpSettings, saveSelfSmtpSettings, testSelfSmtpSettings, updateSelfProfile, } from '../lib/myProfileApi'; import type { UserSmtpMode, UserSmtpSecurityMode, UserSmtpSettings, } from '../lib/myProfileApi'; import type { AdministrativeUserProfile } from '../lib/userProfileApi'; export function MyProfilePage() { const { user } = useAuth(); const [profile, setProfile] = useState(null); const [smtp, setSmtp] = useState(null); const [mode, setMode] = useState('SYSTEM'); const [host, setHost] = useState(''); const [port, setPort] = useState(587); const [securityMode, setSecurityMode] = useState('STARTTLS'); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [fromName, setFromName] = useState(''); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(''); const [error, setError] = useState(''); const [success, setSuccess] = useState(''); const applySmtp = (settings: UserSmtpSettings) => { setSmtp(settings); setMode(settings.mode); setHost(settings.custom?.host ?? ''); setPort(settings.custom?.port ?? 587); setSecurityMode(settings.custom?.securityMode ?? 'STARTTLS'); setUsername(settings.custom?.username ?? ''); setFromName(settings.custom?.fromName ?? ''); setPassword(''); }; const load = async () => { setLoading(true); try { const [loadedProfile, loadedSmtp] = await Promise.all([ getSelfProfile(), getSelfSmtpSettings(), ]); setProfile(loadedProfile); applySmtp(loadedSmtp); } catch (requestError) { setError(errorMessage(requestError)); } finally { setLoading(false); } }; useEffect(() => { void load(); }, []); const saveProfile = async (event: FormEvent) => { event.preventDefault(); if (!profile) return; setError(''); setSuccess(''); setSaving('profile'); try { const form = new FormData(event.currentTarget); const updated = await updateSelfProfile({ email: String(form.get('email') ?? '').trim(), phone: String(form.get('phone') ?? '').trim() || null, jobTitle: String(form.get('jobTitle') ?? '').trim() || null, }); setProfile(updated); setSuccess('Perfil actualizado correctamente.'); } catch (requestError) { setError(errorMessage(requestError)); } finally { setSaving(''); } }; const saveSmtp = async () => { setError(''); setSuccess(''); setSaving('smtp'); try { const updated = await saveSelfSmtpSettings(mode === 'SYSTEM' ? { mode: 'SYSTEM', } : { mode: 'CUSTOM', host: host.trim(), port, securityMode, username: username.trim() || null, password: password || undefined, fromName: fromName.trim() || null, enabled: true, }); applySmtp(updated); setSuccess(mode === 'SYSTEM' ? 'Usarás el SMTP general.' : 'SMTP propio guardado.'); } catch (requestError) { setError(errorMessage(requestError)); } finally { setSaving(''); } }; const testSmtp = async () => { setError(''); setSuccess(''); setSaving('test'); try { const result = await testSelfSmtpSettings(); setSuccess(`Correo de prueba enviado a ${result.recipient}.`); } catch (requestError) { setError(errorMessage(requestError)); } finally { setSaving(''); } }; if (loading) return ; if (!profile || !smtp) return {error || 'No se pudo cargar el perfil.'}; return
{initials(profile.firstName, profile.lastName)}
MI PERFIL

{profile.firstName} {profile.lastName}

@{profile.username} · {profile.jobTitle || 'Función sin informar'}

{error && {error}} {success && {success}}
CONTACTO

Datos del usuario

CORREO SALIENTE

SMTP personal

Por defecto, tus correos salen mediante el SMTP general. Podés cambiar a una cuenta SMTP propia con un clic.

{mode === 'CUSTOM' && <>

La contraseña se cifra en el servidor y nunca vuelve a mostrarse. El remitente será {profile.email}.

}
SEGURIDAD

Cuenta

Tu email es obligatorio para operar en DH Inspección. La contraseña SMTP nunca se muestra ni se incluye en auditorías.

Usuario autenticado: @{user?.username ?? profile.username}

; }