import { useEffect, useRef } from 'react' import type { RefObject } from 'react' import type { LucideIcon } from 'lucide-react' import { History, Link2, Loader2, Trash2, UserPlus, Users } from 'lucide-react' import { Button, Modal } from '../../components/ui' import { useAttendeeDetail } from './AttendeeDetailProvider' import { Trans, useTranslation } from 'react-i18next' type Consequence = { key: string; icon: LucideIcon; text: string } export function ConfirmRemoveAttendeeDialog({ returnFocusRef, }: { /** The element focus returns to when it closes (the "⋯" button). */ returnFocusRef: RefObject }) { const { attendee, groupName, firstWaitlistEntry, waitlistTotal, waitlistPending, isSubmittingRemoval, isRemoveDialogOpen, closeRemoveDialog, confirmRemoveAttendee, } = useAttendeeDetail() const wasOpen = useRef(false) const { t } = useTranslation(['attendeeDetail', 'common']) const contentRef = useRef(null) useEffect(() => { if (isRemoveDialogOpen) { wasOpen.current = true return } if (!wasOpen.current) return wasOpen.current = false returnFocusRef.current?.focus() }, [isRemoveDialogOpen, returnFocusRef]) useEffect(() => { if (!isRemoveDialogOpen) return return trapTabWithin(contentRef) }, [isRemoveDialogOpen]) if (!attendee) return null const waiting = waitlistTotal > 0 const canPromote = !waitlistPending && firstWaitlistEntry !== null const consequences: Consequence[] = [ { key: 'historial', icon: History, text: t('remove.history'), }, ] if (attendee.notifyToken) { consequences.push({ key: 'link', icon: Link2, text: t('remove.link') }) } consequences.push({ key: 'lugar', icon: Users, text: waiting ? t('remove.spotWaiting', { count: waitlistTotal }) : t('remove.spot'), }) return (
    {consequences.map(({ icon: Icon, key, text }) => (
  • ))}

{t('remove.paymentsNote')}

{canPromote && firstWaitlistEntry ? (

}} />

) : null}
{/* Label in black on the `danger` fill: it is the only way to reach AA with the same red in both themes (4.9:1 light, 6.7:1 dark); in white they are 3.7:1 and 2.7:1. */}
) } /** The app's `Modal` does not trap focus; this dialog does, so Tab does not walk * behind the overlay. It only cycles between the content's controls: if focus is * somewhere else (the `Modal`'s X, which still closes on Escape) it lets it * follow its normal course. */ function trapTabWithin(contentRef: RefObject) { const handleKeyDown = (event: KeyboardEvent) => { if (event.key !== 'Tab') return const container = contentRef.current if (!container?.contains(document.activeElement)) return const focusable = Array.from(container.querySelectorAll('button:not([disabled])')) if (focusable.length === 0) return const first = focusable[0] const last = focusable[focusable.length - 1] const active = document.activeElement if (event.shiftKey && active === first) { event.preventDefault() last.focus() } else if (!event.shiftKey && active === last) { event.preventDefault() first.focus() } } document.addEventListener('keydown', handleKeyDown) return () => document.removeEventListener('keydown', handleKeyDown) }