refactor: remove unused attendee detail components and provider

This commit is contained in:
Jose Selesan
2026-09-30 13:37:42 -03:00
parent 27bff32e76
commit 4e86096653
30 changed files with 1710 additions and 475 deletions

View File

@@ -0,0 +1,113 @@
import { createContext, useContext, useState } from 'react'
import type { ReactNode } from 'react'
import { useQuery } from '@tanstack/react-query'
import { useNavigate } from '@tanstack/react-router'
import type { AttendeeDto, AttendeeHistory, GroupWaitlistEntryDto } from '@gruperly/shared'
import { getAttendeeHistory, getGroup, getGroupAttendee, getGroupWaitlist } from '../../lib/api'
import { useRemoveAttendee } from '../groups/detail/useRemoveAttendee'
type AttendeeDetailContextValue = {
groupId: string
groupName: string | undefined
attendee: AttendeeDto | undefined
attendeePending: boolean
attendeeFailed: boolean
history: AttendeeHistory | undefined
historyPending: boolean
historyFailed: boolean
firstWaitlistEntry: GroupWaitlistEntryDto | null
waitlistTotal: number
waitlistPending: boolean
isRemoveDialogOpen: boolean
openRemoveDialog: () => void
closeRemoveDialog: () => void
confirmRemoveAttendee: (promoteFromWaitlist: boolean) => void
isSubmittingRemoval: boolean
}
const AttendeeDetailContext = createContext<AttendeeDetailContextValue | null>(null)
export function AttendeeDetailProvider({
groupId,
attendeeId,
children,
}: {
groupId: string
attendeeId: string
children: ReactNode
}) {
const navigate = useNavigate()
const [isRemoveDialogOpen, setIsRemoveDialogOpen] = useState(false)
const groupQuery = useQuery({
queryKey: ['group', groupId],
queryFn: () => getGroup(groupId),
enabled: Boolean(groupId),
})
const attendeeQuery = useQuery({
queryKey: ['group-attendee', groupId, attendeeId],
queryFn: () => getGroupAttendee(groupId, attendeeId),
enabled: Boolean(groupId && attendeeId),
})
// Misma clave que usa la pantalla de estadísticas: las dos leen el mismo
// historial y no se pisan la caché.
const historyQuery = useQuery({
queryKey: ['attendee-history', groupId, attendeeId],
queryFn: () => getAttendeeHistory(groupId, attendeeId),
enabled: Boolean(groupId && attendeeId),
})
// La lista de espera solo hace falta para el diálogo de quitar, así que no se
// pide hasta que el usuario lo abre.
const waitlistQuery = useQuery({
queryKey: ['group-waitlist', groupId],
queryFn: () => getGroupWaitlist(groupId, 1, 100),
enabled: Boolean(groupId && isRemoveDialogOpen),
})
const waitlistEntries = waitlistQuery.data?.data ?? []
const firstWaitlistEntry = waitlistEntries[0] ?? null
const waitlistTotal = waitlistQuery.data?.pagination?.total ?? waitlistEntries.length
const { removeAttendee, isRemovingAttendee } = useRemoveAttendee({
groupId,
onRemoved: () => {
setIsRemoveDialogOpen(false)
void navigate({ to: '/groups/$groupId', params: { groupId } })
},
})
const value: AttendeeDetailContextValue = {
groupId,
groupName: groupQuery.data?.name,
attendee: attendeeQuery.data,
attendeePending: attendeeQuery.isPending,
attendeeFailed: attendeeQuery.isError,
history: historyQuery.data,
historyPending: historyQuery.isPending,
historyFailed: historyQuery.isError,
firstWaitlistEntry,
waitlistTotal,
waitlistPending: waitlistQuery.isPending,
isRemoveDialogOpen,
openRemoveDialog: () => setIsRemoveDialogOpen(true),
closeRemoveDialog: () => setIsRemoveDialogOpen(false),
confirmRemoveAttendee: (promoteFromWaitlist) => {
if (!attendeeQuery.data) return
removeAttendee({ attendee: attendeeQuery.data, promoteFromWaitlist })
},
isSubmittingRemoval: isRemovingAttendee,
}
return <AttendeeDetailContext.Provider value={value}>{children}</AttendeeDetailContext.Provider>
}
export function useAttendeeDetail() {
const context = useContext(AttendeeDetailContext)
if (!context) {
throw new Error('useAttendeeDetail debe usarse dentro de <AttendeeDetailProvider>')
}
return context
}