Files
gruperly/apps/web/src/features/attendee-detail/AttendeeActionsMenu.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

171 lines
5.7 KiB
TypeScript

import { useEffect, useId, useRef, useState } from 'react'
import type { KeyboardEvent, RefObject } from 'react'
import { Ellipsis, MoveRight, Pause, Pencil, UserMinus, UserPlus } from 'lucide-react'
import { useToast } from '../../components/ui'
import { useAttendeeDetail } from './AttendeeDetailProvider'
import { useTranslation } from 'react-i18next'
const ITEM_CLASS =
'flex min-h-11 w-full items-center gap-2.5 rounded-lg px-3 text-left text-sm font-medium text-primary transition-colors hover:bg-surface focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent'
/** Accessible "⋯" menu for member-level actions. */
export function AttendeeActionsMenu({
triggerRef,
}: {
/** The screen passes it in so the confirmation dialog can give focus back
* when it closes. */
triggerRef: RefObject<HTMLButtonElement | null>
}) {
const { beginContactEdit, openRemoveDialog, openChangeGroupDialog, openAddToGroupDialog } = useAttendeeDetail()
const toast = useToast()
const { t } = useTranslation(['attendeeDetail', 'common'])
const [isOpen, setIsOpen] = useState(false)
const menuRef = useRef<HTMLDivElement>(null)
const itemRef = useRef<HTMLButtonElement>(null)
const menuId = useId()
useEffect(() => {
if (isOpen) itemRef.current?.focus()
}, [isOpen])
useEffect(() => {
if (!isOpen) return
const handlePointerDown = (event: PointerEvent) => {
const target = event.target as Node
if (menuRef.current?.contains(target) || triggerRef.current?.contains(target)) return
setIsOpen(false)
}
document.addEventListener('pointerdown', handlePointerDown)
return () => document.removeEventListener('pointerdown', handlePointerDown)
}, [isOpen, triggerRef])
const close = (restoreFocus = false) => {
setIsOpen(false)
if (restoreFocus) triggerRef.current?.focus()
}
const handleKeyDown = (event: KeyboardEvent<HTMLDivElement>) => {
if (event.key === 'Escape') {
event.stopPropagation()
close(true)
return
}
if (event.key === 'Tab') {
close()
return
}
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) return
event.preventDefault()
const items = Array.from(
menuRef.current?.querySelectorAll<HTMLElement>('[role="menuitem"]') ?? [],
)
if (items.length === 0) return
const current = items.indexOf(document.activeElement as HTMLElement)
const next =
event.key === 'Home'
? 0
: event.key === 'End'
? items.length - 1
: (current + (event.key === 'ArrowDown' ? 1 : -1) + items.length) % items.length
items[next]?.focus()
}
const showPendingPause = () => {
close()
// TODO(miembros): conectar la pausa cuando se defina su efecto sobre cupos y cobros.
toast.info(t('menu.pausePending'))
}
return (
<div className="relative shrink-0">
<button
ref={triggerRef}
type="button"
onClick={() => setIsOpen((value) => !value)}
aria-haspopup="menu"
aria-expanded={isOpen}
aria-controls={isOpen ? menuId : undefined}
aria-label={t('menu.more')}
className="inline-flex size-11 items-center justify-center rounded-xl text-primary transition-colors hover:bg-primary-soft active:scale-[0.96] motion-reduce:active:scale-100 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-accent"
>
<Ellipsis className="size-5" aria-hidden="true" />
</button>
{isOpen ? (
<div
ref={menuRef}
id={menuId}
role="menu"
aria-label={t('menu.label')}
onKeyDown={handleKeyDown}
className="absolute right-0 top-full z-40 mt-1 w-60 rounded-xl bg-primary-soft p-1.5 shadow-lg"
>
<button
ref={itemRef}
type="button"
role="menuitem"
className={ITEM_CLASS}
onClick={() => {
close()
openChangeGroupDialog()
}}
>
<MoveRight className="size-4 shrink-0 text-secondary" aria-hidden="true" />
{t('menu.changeGroup')}
</button>
<button
type="button"
role="menuitem"
className={ITEM_CLASS}
onClick={() => {
close()
openAddToGroupDialog()
}}
>
<UserPlus className="size-4 shrink-0 text-secondary" aria-hidden="true" />
{t('menu.addToGroup')}
</button>
<button
type="button"
role="menuitem"
className={ITEM_CLASS}
onClick={() => {
close()
beginContactEdit()
requestAnimationFrame(() => {
document.getElementById('attendee-contact')?.scrollIntoView({ behavior: 'smooth' })
})
}}
>
<Pencil className="size-4 shrink-0 text-secondary" aria-hidden="true" />
{t('menu.editContact')}
</button>
<button
type="button"
role="menuitem"
className={ITEM_CLASS}
onClick={showPendingPause}
>
<Pause className="size-4 shrink-0 text-secondary" aria-hidden="true" />
{t('menu.pause')}
</button>
<div className="my-1 border-t border-border/70" aria-hidden="true" />
<button
type="button"
role="menuitem"
className={`${ITEM_CLASS} text-danger-fg hover:bg-danger-soft`}
onClick={() => {
close()
openRemoveDialog()
}}
>
<UserMinus className="size-4 shrink-0" aria-hidden="true" />
{t('menu.remove')}
</button>
</div>
) : null}
</div>
)
}