"use client"; import React, { useState } from "react"; import { ListPlus, X } from "lucide-react"; import { apiFetch, ApiError } from "@/lib/api"; import { useAuth } from "@/hooks/useAuth"; export type WaitlistLine = { eventOptionId: string; variantId?: string | null; label: string }; const inputCls = "w-full border rounded-lg px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-brand-400"; /** * "Join waitlist" for sold-out options. Opens a small form: which option, how many, * and (for guests on events that allow it) name and contact details. */ export function JoinWaitlistButton({ eventId, lines, guestsAllowed, initialLine, compact = false }: { eventId: string; lines: WaitlistLine[]; guestsAllowed: boolean; initialLine?: WaitlistLine; compact?: boolean; }) { const { token, user } = useAuth(); const [open, setOpen] = useState(false); const [lineKey, setLineKey] = useState(() => keyOf(initialLine || lines[0])); const [qty, setQty] = useState(1); const [guest, setGuest] = useState({ name: "", email: "", phone: "" }); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [done, setDone] = useState(null); if (lines.length === 0) return null; const loggedIn = !!(token && user); const line = lines.find(l => keyOf(l) === lineKey) || lines[0]; const submit = async (e: React.FormEvent) => { e.preventDefault(); setBusy(true); setError(null); try { const res = await apiFetch<{ message: string }>("/api/waitlist", { method: "POST", authToken: token || undefined, body: { eventId, eventOptionId: line.eventOptionId, ...(line.variantId ? { variantId: line.variantId } : {}), quantity: qty, ...(!loggedIn ? { guestName: guest.name.trim(), guestEmail: guest.email.trim(), guestPhone: guest.phone.trim() || undefined } : {}), }, }); setDone(res.message); } catch (err) { setError(err instanceof ApiError ? err.message : "Couldn't join the waitlist. Please try again."); } finally { setBusy(false); } }; return ( <> {open && (
setOpen(false)}>
e.stopPropagation()}>

Join the waitlist

{done ? ( <>

{done}

) : !loggedIn && !guestsAllowed ? (

Log in to join the waitlist for this event.

) : ( <>

If a place opens up, we'll email or WhatsApp you a link to claim it. We hold it for you for a limited time.

{lines.length > 1 && (
)} {lines.length === 1 &&

Ticket: {line.label}

}
setQty(Math.max(1, Math.min(20, parseInt(e.target.value, 10) || 1)))} />
{!loggedIn && (
setGuest({ ...guest, name: e.target.value })} required aria-label="Full name" /> setGuest({ ...guest, email: e.target.value })} required aria-label="Email" /> setGuest({ ...guest, phone: e.target.value })} aria-label="Phone" />
)} {error &&

{error}

} )}
)} ); } function keyOf(l?: WaitlistLine) { return l ? `${l.eventOptionId}::${l.variantId || ""}` : ""; } /** The sold-out lines of an event, for the waitlist button. */ export function soldOutLines(options: { id: string; name: string; stockLimit?: number; availableCount?: number | null; variants?: { id: string; name: string; stockLimit?: number; availableCount?: number | null }[] | null }[]): WaitlistLine[] { const out: WaitlistLine[] = []; for (const o of options || []) { const optOut = (o.stockLimit ?? 0) > 0 && o.availableCount !== undefined && o.availableCount !== null && o.availableCount <= 0; const variants = o.variants || []; if (variants.length > 0) { for (const v of variants) { const vOut = (v.stockLimit ?? 0) > 0 && v.availableCount !== undefined && v.availableCount !== null && v.availableCount <= 0; if (optOut || vOut) out.push({ eventOptionId: o.id, variantId: v.id, label: `${o.name} (${v.name})` }); } } else if (optOut) { out.push({ eventOptionId: o.id, label: o.name }); } } return out; }