Files
gruperly/apps/web/src/features/attendee-detail/ConfirmRemoveAttendeeDialog.tsx
Jose Selesan 8a5768e0c0 feat(i18n): traducir la ficha del alumno
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 12:30:11 -03:00

172 lines
5.5 KiB
TypeScript

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<HTMLButtonElement | null>
}) {
const {
attendee,
groupName,
firstWaitlistEntry,
waitlistTotal,
waitlistPending,
isSubmittingRemoval,
isRemoveDialogOpen,
closeRemoveDialog,
confirmRemoveAttendee,
} = useAttendeeDetail()
const wasOpen = useRef(false)
const { t } = useTranslation(['attendeeDetail', 'common'])
const contentRef = useRef<HTMLDivElement>(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 (
<Modal
isOpen={isRemoveDialogOpen}
onClose={closeRemoveDialog}
title={t('remove.title')}
description={`${attendee.fullName}${groupName ? ` · ${groupName}` : ''}`}
maxWidth="sm"
>
<div ref={contentRef} className="space-y-4">
<ul className="space-y-2.5">
{consequences.map(({ icon: Icon, key, text }) => (
<li key={key} className="flex items-start gap-2.5 text-sm text-foreground">
<Icon className="mt-0.5 size-4 shrink-0" aria-hidden="true" />
<span>{text}</span>
</li>
))}
</ul>
<p className="text-xs text-foreground">
{t('remove.paymentsNote')}
</p>
{canPromote && firstWaitlistEntry ? (
<div className="rounded-xl bg-accent-soft p-3.5">
<p className="text-sm text-foreground">
<Trans
t={t}
i18nKey="remove.promote"
values={{ name: firstWaitlistEntry.fullName }}
components={{ strong: <strong className="text-primary" /> }}
/>
</p>
<Button
variant="primary"
onClick={() => confirmRemoveAttendee(true)}
disabled={isSubmittingRemoval}
className="mt-2.5 w-full gap-2"
>
{isSubmittingRemoval ? (
<Loader2 className="size-4 animate-spin" />
) : (
<UserPlus className="size-4" />
)}
<span>{t('remove.promoteAction', { name: firstWaitlistEntry.fullName })}</span>
</Button>
</div>
) : null}
<div className="flex flex-col-reverse gap-2.5 border-t border-border pt-3 sm:flex-row sm:justify-end">
<Button variant="ghost" onClick={closeRemoveDialog} autoFocus>
{t('common:actions.cancel')}
</Button>
{/* 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. */}
<Button
onClick={() => confirmRemoveAttendee(false)}
disabled={isSubmittingRemoval}
className="gap-2 bg-danger text-black hover:bg-danger/90"
>
{isSubmittingRemoval ? (
<Loader2 className="size-4 animate-spin" />
) : (
<Trash2 className="size-4" />
)}
<span>{t('remove.confirm')}</span>
</Button>
</div>
</div>
</Modal>
)
}
/** 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<HTMLDivElement | null>) {
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<HTMLElement>('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)
}