import { useEffect, useMemo, useState } from 'react'; import { useAuth } from '../context/AuthContext'; import { useEvent } from '../lib/useEvent'; import { checkinAttendee, createConnectionRequest, lanyardLookup, welcomeCheckin } from '../lib/api'; import { capWords } from '../lib/time'; import type { CheckinResult, PublicContact, WelcomeCheckinResult } from '../lib/types'; import { Avatar, Scanner, Sheet, Spinner } from '../components/ui'; import { useToast } from '../context/ToastContext'; export function Scan() { const { contact } = useAuth(); const { slots, loading } = useEvent(); const isStaff = !!contact?.is_admin; const [mode, setMode] = useState<'attendee' | 'staff'>(isStaff ? 'staff' : 'attendee'); const [sessionId, setSessionId] = useState(''); const [welcomeMode, setWelcomeMode] = useState(false); const [connectTarget, setConnectTarget] = useState<{ contact: PublicContact; qr: string } | null>(null); const [checkin, setCheckin] = useState(null); const [welcome, setWelcome] = useState(null); const [busy, setBusy] = useState(false); const { toast } = useToast(); // The first checkable (non-service) session, computed synchronously so a fast // scan never races the async default-selection effect below. const defaultSessionId = useMemo( () => slots.flatMap((s) => s.sessions).find((s) => s.slot_type !== 'service_session')?.id ?? '', [slots], ); useEffect(() => { if (defaultSessionId && !sessionId) setSessionId(defaultSessionId); }, [defaultSessionId, sessionId]); if (loading || !contact) { return (
); } const meEmail = contact.email; const activeStaff = mode === 'staff' && isStaff; async function handleScan(token: string) { setBusy(true); try { if (activeStaff) { if (welcomeMode) { const res = await welcomeCheckin(token); setWelcome(res); } else { // Fall back to the first checkable session so a fast scan (before the // async default-selection effect runs) never fails with "pick a session". const targetSession = sessionId || defaultSessionId; if (!targetSession) { toast('Pick a session first', 'err'); return; } const res = await checkinAttendee(targetSession, token); setCheckin(res); } } else { const found = await lanyardLookup(token); if (!found) { toast('No attendee found for that code', 'err'); return; } if (found.email === meEmail) { toast('That’s your own lanyard 🙂', 'err'); return; } setConnectTarget({ contact: found, qr: token }); } } catch (e) { toast(e instanceof Error ? e.message : 'Something went wrong', 'err'); } finally { setBusy(false); } } async function confirmConnect() { if (!connectTarget) return; setBusy(true); try { const conn = await createConnectionRequest(connectTarget.qr); if (conn.status === 'accepted') toast('You’re connected! 🤝', 'ok'); else if (conn.status === 'pending') toast('Connection request sent', 'ok'); else toast('Request already resolved'); window.dispatchEvent(new Event('connections-changed')); setConnectTarget(null); } catch (e) { toast(e instanceof Error ? e.message : 'Could not send request', 'err'); } finally { setBusy(false); } } const selSession = slots.flatMap((s) => s.sessions).find((s) => s.id === sessionId); return (
{activeStaff ? 'Check-in' : 'Scan'}
{activeStaff ? 'Scan attendees to check them in' : 'Scan a peer’s lanyard to connect'}
{isStaff && (
)}
{activeStaff && (
Check-in type
{welcomeMode ? (
🏷 Welcome check-in — marks the attendee present at the event.
) : ( <>
Checking in to session
)}
)} {busy &&
} {activeStaff && !welcomeMode && selSession && (
{selSession.room} · {selSession.speakers}
)} {/* Connect sheet */} setConnectTarget(null)}> {connectTarget && (

{connectTarget.contact.full_name}

{connectTarget.contact.registration_type && ( {capWords(connectTarget.contact.registration_type)} )} {connectTarget.contact.linkedin_url && ( )}
)}
{/* Check-in result sheet */} setCheckin(null)}> {checkin && (
{checkin.already ? '⚠️' : '✅'}

{checkin.attendee_name}

{checkin.already ? 'Already checked in' : 'Checked in'}
{checkin.already ? ( No extra points ) : ( +10 pts · now {checkin.points} )}
)}
{/* Welcome check-in result sheet */} setWelcome(null)}> {welcome && (
{welcome.already ? '⚠️' : '👋'}

{welcome.attendee_name}

{welcome.already ? 'Already checked in to the event' : 'Welcome to the event!'}
✓ Checked in
)}
); }