import { useEffect, useState } from "react"; import { Trophy } from "lucide-react"; import { Link, useSearchParams } from "react-router-dom"; import { getVisitorEventRewardBalance, getVisitorRewardHistory } from "../lib/api"; import { getAttendeeToken } from "../lib/attendeeAuth"; import { HeaderMobile } from "../components/HeaderMobile"; import { MobileShell } from "../components/MobileShell"; import { readStoredVisitorContext } from "../types/visitorContext"; function historyLabel(source?: string, description?: string): string { if (description && String(description).trim()) return String(description).trim(); const m: Record = { registration: "Registration", question_ask: "Asked a question", question_vote: "Question vote", quiz_poll: "Quiz", poll: "Poll or survey", manual: "Adjustment", feedback: "Feedback", rating: "Rating", quiz: "Quiz", }; return m[source || ""] || source || "Points"; } export function RewardsPage() { const [searchParams] = useSearchParams(); const urlEventId = searchParams.get("event_id")?.trim() || ""; const ctx = readStoredVisitorContext(); const eventId = urlEventId || ctx?.eventId || ""; const [totalPoints, setTotalPoints] = useState(null); const [historyItems, setHistoryItems] = useState< Awaited>["items"] >([]); const [historyLoading, setHistoryLoading] = useState(false); useEffect(() => { if (!eventId || !getAttendeeToken()) { setTotalPoints(null); setHistoryItems([]); return; } let cancelled = false; getVisitorEventRewardBalance(eventId) .then((n) => { if (!cancelled) setTotalPoints(Math.max(0, n)); }) .catch(() => { if (!cancelled) setTotalPoints(0); }); setHistoryLoading(true); getVisitorRewardHistory(eventId, { page: 1, limit: 30 }) .then((r) => { if (!cancelled) setHistoryItems(r.items); }) .catch(() => { if (!cancelled) setHistoryItems([]); }) .finally(() => { if (!cancelled) setHistoryLoading(false); }); return () => { cancelled = true; }; }, [eventId]); const displayTotal = !getAttendeeToken() || !eventId ? "—" : totalPoints === null ? "…" : String(totalPoints); return (

Your Rewards

Leaderboard

Total Points

{displayTotal}

Points history

    {historyLoading && (
  • Loading…
  • )} {!historyLoading && historyItems.length === 0 && (
  • No transactions yet.
  • )} {!historyLoading && historyItems.map((row) => { const signed = row.type === "redeemed" ? "-" : "+"; const cls = row.type === "redeemed" ? "bg-[#fff0f0] text-[#c02424]" : row.type === "earned" ? "bg-[#ebffee] text-[#009951]" : "bg-[#f3f4f6] text-[#24282f]"; return (
  • {signed} {row.points} pts {historyLabel(row.source, row.description)}
  • ); })}
); }