import { useEffect, useState } from 'react' import { Mail, Phone } from 'lucide-react' import { ApiError } from '../../lib/api' import { formatPhoneDisplay } from '../../lib/format' import { cn } from '../../lib/utils' import { AttendeeContactForm } from './AttendeeContactForm' import { contactEmailError } from './detail' import { Section } from './Section' import { useAttendeeDetail } from './AttendeeDetailProvider' import { useTranslation } from 'react-i18next' export function AttendeeContactSection() { const { attendee, isEditingContact, beginContactEdit, endContactEdit, saveAttendee, isUpdatingAttendee, } = useAttendeeDetail() const [phone, setPhone] = useState('') const { t } = useTranslation(['attendeeDetail', 'common']) const [email, setEmail] = useState('') const [error, setError] = useState(null) useEffect(() => { if (!isEditingContact || !attendee) return setPhone(attendee.phone ?? '') setEmail(attendee.email ?? '') setError(null) }, [attendee, isEditingContact]) if (!attendee) return null const cancel = () => { setError(null) endContactEdit() } const save = async () => { const emailError = contactEmailError(email) if (emailError) { setError(emailError) return } try { await saveAttendee({ phone, email }) endContactEdit() } catch (err) { setError( err instanceof ApiError && err.problem?.code === 'person_phone_taken' ? t('contact.phoneTaken') : t('contact.saveError'), ) } } return (
{t('contact.edit')} ) : null } > {isEditingContact ? ( 0 ? t('contact.shared') : null } isSaving={isUpdatingAttendee} onPhoneChange={(value) => { setPhone(value) setError(null) }} onEmailChange={(value) => { setEmail(value) setError(null) }} onCancel={cancel} onSave={() => void save()} /> ) : (
)}
) } type ContactRowProps = { icon: typeof Phone label: string value: string href?: string placeholder: boolean } function ContactRow({ icon: Icon, label, value, href, placeholder }: ContactRowProps) { const content = ( <>