148 lines
4.2 KiB
TypeScript
148 lines
4.2 KiB
TypeScript
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<string | null>(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 (
|
|
<Section
|
|
id="attendee-contact"
|
|
title={t('contact.title')}
|
|
trailing={
|
|
!isEditingContact ? (
|
|
<button
|
|
type="button"
|
|
onClick={beginContactEdit}
|
|
className="min-h-11 rounded-lg px-2 text-sm font-medium text-accent-fg transition-colors hover:bg-primary-soft hover:text-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
|
|
>
|
|
{t('contact.edit')}
|
|
</button>
|
|
) : null
|
|
}
|
|
>
|
|
{isEditingContact ? (
|
|
<AttendeeContactForm
|
|
phone={phone}
|
|
email={email}
|
|
error={error}
|
|
sharedNote={
|
|
attendee.otherGroups.length > 0
|
|
? t('contact.shared')
|
|
: null
|
|
}
|
|
isSaving={isUpdatingAttendee}
|
|
onPhoneChange={(value) => {
|
|
setPhone(value)
|
|
setError(null)
|
|
}}
|
|
onEmailChange={(value) => {
|
|
setEmail(value)
|
|
setError(null)
|
|
}}
|
|
onCancel={cancel}
|
|
onSave={() => void save()}
|
|
/>
|
|
) : (
|
|
<div className="divide-y divide-border/60">
|
|
<ContactRow
|
|
icon={Phone}
|
|
label={t('contact.phone')}
|
|
value={formatPhoneDisplay(attendee.phone) ?? t('contact.addPhone')}
|
|
href={attendee.phone ? `tel:${attendee.phone.replace(/\D/g, '')}` : undefined}
|
|
placeholder={!attendee.phone}
|
|
/>
|
|
<ContactRow
|
|
icon={Mail}
|
|
label={t('contact.email')}
|
|
value={attendee.email ?? t('contact.addEmail')}
|
|
href={attendee.email ? `mailto:${attendee.email}` : undefined}
|
|
placeholder={!attendee.email}
|
|
/>
|
|
</div>
|
|
)}
|
|
</Section>
|
|
)
|
|
}
|
|
|
|
type ContactRowProps = {
|
|
icon: typeof Phone
|
|
label: string
|
|
value: string
|
|
href?: string
|
|
placeholder: boolean
|
|
}
|
|
|
|
function ContactRow({ icon: Icon, label, value, href, placeholder }: ContactRowProps) {
|
|
const content = (
|
|
<>
|
|
<Icon className="size-4 shrink-0 text-secondary" aria-hidden="true" />
|
|
<span className="min-w-0">
|
|
<span className="block text-xs font-medium text-secondary">{label}</span>
|
|
<span className={cn('block truncate text-sm text-primary', placeholder && 'text-secondary')}>
|
|
{value}
|
|
</span>
|
|
</span>
|
|
</>
|
|
)
|
|
|
|
const className = 'flex min-h-14 items-center gap-3 rounded-lg px-3 py-2'
|
|
return href ? (
|
|
<a href={href} className={cn(className, 'transition-colors hover:bg-primary-soft')}>
|
|
{content}
|
|
</a>
|
|
) : (
|
|
<div className={className}>{content}</div>
|
|
)
|
|
}
|