import { useState } from 'react' import { apiFetch } from '../api' import { useAuth } from '../context/AuthContext' import { useHideCaretWhileTyping } from '../hooks/useHideCaretWhileTyping' type Props = { onClose: () => void } export function AccountModal({ onClose }: Props) { const { onFormTextInput, onTextareaBlur } = useHideCaretWhileTyping() const { user, refreshUser, signOut } = useAuth() const [firstName, setFirstName] = useState(user?.firstName || '') const [lastName, setLastName] = useState(user?.lastName || '') const [email, setEmail] = useState(user?.email || '') const [password, setPassword] = useState('') const [err, setErr] = useState('') const save = async () => { setErr('') const body: Record = { firstName, lastName, email } if (password.trim()) body.password = password const r = await apiFetch('/api/auth/me', { method: 'PATCH', body: JSON.stringify(body), }) if (!r.ok) { setErr((await r.json().catch(() => ({}))).detail || 'Update failed') return } await refreshUser() onClose() } const deleteAccount = async () => { if (!confirm('Delete your account and server-stored profile? This cannot be undone.')) return const r = await apiFetch('/api/auth/me', { method: 'DELETE' }) if (r.ok) { signOut() onClose() } } return (

Account

{err &&

{err}

}
setFirstName(e.target.value)} onInput={onFormTextInput} onBlur={onTextareaBlur} />
setLastName(e.target.value)} onInput={onFormTextInput} onBlur={onTextareaBlur} />
setEmail(e.target.value)} onInput={onFormTextInput} onBlur={onTextareaBlur} />
setPassword(e.target.value)} onInput={onFormTextInput} onBlur={onTextareaBlur} placeholder="Leave blank to keep current" />
) }