"use client"; import React, { useEffect, useState } from "react"; import { useParams } from "next/navigation"; import { Navbar } from "@/components/layout/Navbar"; import { Footer } from "@/components/layout/Footer"; import { apiFetch } from "@/lib/api"; import { formatDate } from "@/lib/date"; import { Ticket, CheckCircle2, XCircle } from "lucide-react"; import { errorMessage } from "@/lib/utils"; type Offer = { status: "pending" | "accepted" | "declined" | "cancelled" | "expired"; toName: string; fromName: string; expiresAt: string; option: string; event: { id: string; title: string; startDate: string; location: string | null }; }; const STATUS_TEXT: Record = { accepted: "This ticket has already been accepted.", declined: "This ticket offer was declined.", cancelled: "This ticket offer was withdrawn.", expired: "This ticket offer has expired.", }; /** Public page the person a ticket is offered to opens from their email or WhatsApp. */ export default function TransferOfferPage() { const params = useParams<{ token: string }>(); const token = params?.token; const [offer, setOffer] = useState(null); const [loadError, setLoadError] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const [done, setDone] = useState<{ ok: boolean; message: string } | null>(null); useEffect(() => { if (!token) return; apiFetch(`/api/tickets/transfer-offer/${encodeURIComponent(token)}`) .then(setOffer) .catch(e => setLoadError(e?.message || "This transfer link isn't valid.")); }, [token]); const answer = async (action: "accept" | "decline") => { setBusy(true); setError(null); try { const r = await apiFetch<{ message: string }>(`/api/tickets/transfer-offer/${encodeURIComponent(token!)}/${action}`, { method: "POST" }); setDone({ ok: action === "accept", message: r.message }); } catch (e) { setError(errorMessage(e) || "Something went wrong"); } finally { setBusy(false); } }; return (

A ticket for you

{loadError &&
{loadError}
} {done && (
{done.ok ? : }

{done.message}

)} {offer && !done && (

Hi {offer.toName.split(" ")[0]}, {offer.fromName || "someone"} would like to give you their ticket.

{offer.event.title}
{formatDate(new Date(offer.event.startDate))}{offer.event.location ? ` · ${offer.event.location}` : ""}
{offer.option}
{offer.status === "pending" ? ( <>

Accepting sends you your own ticket and QR code. If you don't have an account with us yet, we'll create one and send you a link to activate it. This offer is open until {new Date(offer.expiresAt).toLocaleString()}.

{error &&
{error}
}
) : (

{STATUS_TEXT[offer.status]}

)}
)}
); }