78 lines
2.8 KiB
TypeScript
78 lines
2.8 KiB
TypeScript
import { useId, useState } from 'react'
|
|
import { Check, Copy, MessageCircle } from 'lucide-react'
|
|
import { cn } from '../../lib/utils'
|
|
import { useAttendeeDetail } from './AttendeeDetailProvider'
|
|
import { ACTION_BUTTON_CLASS, PRIMARY_ACTION_CLASS } from './actionButton'
|
|
import { Section } from './Section'
|
|
import { useCopyToClipboard } from './useCopyToClipboard'
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
export function AbsenceLinkSection() {
|
|
const { attendee } = useAttendeeDetail()
|
|
const { copied, copy } = useCopyToClipboard()
|
|
const { t } = useTranslation(['attendeeDetail', 'common'])
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
const panelId = useId()
|
|
|
|
const token = attendee?.notifyToken
|
|
const url = token ? `${window.location.origin}/report-absence/${token}` : null
|
|
if (!attendee || !url) return null
|
|
|
|
const phoneDigits = attendee.phone?.replace(/\D/g, '') ?? ''
|
|
const shareUrl = phoneDigits
|
|
? `https://wa.me/${phoneDigits}?text=${encodeURIComponent(
|
|
t('absenceLink.message', { name: attendee.fullName.split(' ')[0], url }),
|
|
)}`
|
|
: null
|
|
|
|
return (
|
|
<Section
|
|
title={t('absenceLink.title')}
|
|
description={t('absenceLink.description')}
|
|
toggle={{ isOpen, panelId, onToggle: () => setIsOpen((value) => !value) }}
|
|
>
|
|
{/* The panel stays in the DOM but hidden: `aria-controls` always resolves and
|
|
the "Link copied" text is not lost on collapse/expand. */}
|
|
<div id={panelId} hidden={!isOpen}>
|
|
{/* Stacked and full width: at 50% the labels ("Enviar por WhatsApp")
|
|
wrapped onto two lines and fell below the minimum touch target.
|
|
Copy stays on top, glued to its confirmation, and the primary action
|
|
is told apart by the accent fill. */}
|
|
<div className="flex flex-col gap-2">
|
|
<button
|
|
type="button"
|
|
onClick={() => copy(url)}
|
|
className={cn(ACTION_BUTTON_CLASS, 'w-full')}
|
|
>
|
|
<Copy className="size-4" aria-hidden="true" />
|
|
{t('absenceLink.copy')}
|
|
</button>
|
|
{shareUrl ? (
|
|
<a
|
|
href={shareUrl}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
aria-label={t('absenceLink.sendLabel', { name: attendee.fullName })}
|
|
className={PRIMARY_ACTION_CLASS}
|
|
>
|
|
<MessageCircle className="size-4" aria-hidden="true" />
|
|
{t('absenceLink.send')}
|
|
</a>
|
|
) : null}
|
|
</div>
|
|
<p
|
|
aria-live="polite"
|
|
className="flex min-h-5 items-center gap-1.5 px-3 pt-2 text-xs font-medium text-success"
|
|
>
|
|
{copied ? (
|
|
<>
|
|
<Check className="size-3.5" aria-hidden="true" />
|
|
{t('absenceLink.copied')}
|
|
</>
|
|
) : null}
|
|
</p>
|
|
</div>
|
|
</Section>
|
|
)
|
|
}
|