refactor: remove unused attendee detail components and provider
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
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'
|
||||
|
||||
type Consequence = { key: string; icon: LucideIcon; text: string }
|
||||
|
||||
export function ConfirmRemoveAttendeeDialog({
|
||||
returnFocusRef,
|
||||
}: {
|
||||
/** Elemento al que vuelve el foco al cerrarse (el botón "⋯"). */
|
||||
returnFocusRef: RefObject<HTMLButtonElement | null>
|
||||
}) {
|
||||
const {
|
||||
attendee,
|
||||
groupName,
|
||||
firstWaitlistEntry,
|
||||
waitlistTotal,
|
||||
waitlistPending,
|
||||
isSubmittingRemoval,
|
||||
isRemoveDialogOpen,
|
||||
closeRemoveDialog,
|
||||
confirmRemoveAttendee,
|
||||
} = useAttendeeDetail()
|
||||
|
||||
const wasOpen = useRef(false)
|
||||
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: 'Se borra su historial: las clases registradas y las ausencias que avisó dejan de existir.',
|
||||
},
|
||||
]
|
||||
if (attendee.notifyToken) {
|
||||
consequences.push({ key: 'link', icon: Link2, text: 'El link de ausencias deja de funcionar.' })
|
||||
}
|
||||
consequences.push({
|
||||
key: 'lugar',
|
||||
icon: Users,
|
||||
text: waiting
|
||||
? `Se libera 1 lugar. Hay ${waitlistTotal} ${
|
||||
waitlistTotal === 1 ? 'persona' : 'personas'
|
||||
} en lista de espera.`
|
||||
: 'Se libera 1 lugar en el grupo.',
|
||||
})
|
||||
|
||||
return (
|
||||
<Modal
|
||||
isOpen={isRemoveDialogOpen}
|
||||
onClose={closeRemoveDialog}
|
||||
title="Quitar del grupo"
|
||||
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">
|
||||
Si tiene cobros registrados no se va a poder quitar: primero hay que gestionarlos.
|
||||
</p>
|
||||
|
||||
{canPromote && firstWaitlistEntry ? (
|
||||
<div className="rounded-xl bg-accent-soft p-3.5">
|
||||
<p className="text-sm text-foreground">
|
||||
Si lo quitás, podés darle el lugar a{' '}
|
||||
<strong className="text-primary">{firstWaitlistEntry.fullName}</strong>, el primero
|
||||
de la lista de espera.
|
||||
</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>Pasar a {firstWaitlistEntry.fullName} al grupo</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>
|
||||
Cancelar
|
||||
</Button>
|
||||
{/* Etiqueta en negro sobre el relleno `danger`: es la única forma de
|
||||
llegar a AA con el mismo rojo en los dos temas (4.9:1 claro,
|
||||
6.7:1 oscuro); en blanco quedan 3.7:1 y 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>Quitar del grupo</span>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
)
|
||||
}
|
||||
|
||||
/** El `Modal` de la app no atrapa el foco; este diálogo sí, para que el Tab no
|
||||
* se vaya detrás del overlay. Solo cicla entre los controles del contenido: si
|
||||
* el foco está en otro lado (la X del `Modal`, que se sigue cerrando con
|
||||
* Escape) lo deja seguir su curso normal. */
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user