import { useEffect, useMemo, useState } from 'react'; import { createRegistration, getAllContacts, getActiveEvent, getRegistrations, resetQrToken, setCheckedIn, setRegistrationType, } from '../lib/api'; import type { Contact, Event, Registration, RegistrationType } from '../lib/types'; import { useToast } from '../context/ToastContext'; import { Avatar, Empty, Modal, QrImage, Spinner, Toggle, TypeBadge } from '../components/ui'; const TYPES: RegistrationType[] = ['attendee', 'speaker', 'staff', 'sponsor']; export function Registrations() { const { push } = useToast(); const [event, setEvent] = useState(null); const [loaded, setLoaded] = useState(false); const [contacts, setContacts] = useState([]); const [registrations, setRegistrations] = useState([]); const [error, setError] = useState(null); const [query, setQuery] = useState(''); const [adding, setAdding] = useState(false); const [qrFor, setQrFor] = useState(null); const [newToken, setNewToken] = useState(null); const [busy, setBusy] = useState(false); async function load() { try { const ev = await getActiveEvent(); setEvent(ev); if (ev) { const [c, r] = await Promise.all([getAllContacts(), getRegistrations(ev.id)]); setContacts(c); setRegistrations(r); } setLoaded(true); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to load'); } } useEffect(() => { load(); }, []); const byEmail = useMemo(() => new Map(contacts.map((c) => [c.email, c])), [contacts]); const filtered = useMemo(() => { if (!registrations) return []; const q = query.trim().toLowerCase(); if (!q) return registrations; return registrations.filter((r) => { const c = byEmail.get(r.contact_email); return ( (c?.full_name ?? '').toLowerCase().includes(q) || r.contact_email.toLowerCase().includes(q) || r.type.toLowerCase().includes(q) ); }); }, [registrations, query, byEmail]); async function changeType(r: Registration, type: RegistrationType) { setBusy(true); try { await setRegistrationType(r.contact_email, type); await load(); push('success', `${byEmail.get(r.contact_email)?.full_name ?? r.contact_email} → ${type}`); } catch (e) { push('error', e instanceof Error ? e.message : 'Update failed'); } finally { setBusy(false); } } async function toggleCheckedIn(r: Registration, v: boolean) { setBusy(true); try { await setCheckedIn(r.id, v); await load(); } catch (e) { push('error', e instanceof Error ? e.message : 'Update failed'); } finally { setBusy(false); } } async function doResetQr(r: Registration) { setBusy(true); try { const res = await resetQrToken(r.contact_email); setNewToken(res.qr_token); setQrFor(r); await load(); push('success', `New QR token issued for ${byEmail.get(r.contact_email)?.full_name ?? r.contact_email}`); } catch (e) { push('error', e instanceof Error ? e.message : 'Reset failed'); } finally { setBusy(false); } } if (error) return ; if (!loaded) return (
); if (!event) { return (

Registrations

Ticket types and check-ins for the active event.
); } return (

Registrations

{event.name} · {registrations.length} registrations · {registrations.filter((r) => r.checked_in).length} checked in.
setQuery(e.target.value)} />
{filtered.map((r) => { const c = byEmail.get(r.contact_email); return ( ); })}
Contact Type QR token Checked in Points
{c?.full_name ?? r.contact_email}
{r.contact_email}
{r.qr_token ?? '—'}
toggleCheckedIn(r, v)} /> {r.points}
{filtered.length === 0 &&
}
{adding && ( r.contact_email))} onClose={() => setAdding(false)} onSaved={async (msg) => { setAdding(false); await load(); push('success', msg); }} /> )} {qrFor && ( setQrFor(null)} footer={ <> } >
{newToken ? ( <>
{newToken}

New token issued. The old token no longer works — the person must re-scan.

) : ( <>
{qrFor.qr_token ?? '—'}

Current token. Reset to issue a new one.

)}
)}
); } function AddRegistrationModal({ contacts, existing, onClose, onSaved, }: { contacts: Contact[]; existing: Set; onClose: () => void; onSaved: (msg: string) => void; }) { const available = contacts.filter((c) => !existing.has(c.email)); const [email, setEmail] = useState(available[0]?.email ?? ''); const [type, setType] = useState('attendee'); const [busy, setBusy] = useState(false); async function save() { if (!email) return; setBusy(true); try { await createRegistration(email, type); onSaved(`Registered ${email} as ${type}`); } catch (e) { onSaved(e instanceof Error ? e.message : 'Registration failed'); } finally { setBusy(false); } } return ( } >
{type === 'sponsor' && (

A sponsor registration requires a sponsor lookup — add the sponsor in the Sponsors view first.

)}
); }