"use client"; import React, { useEffect, useState } from "react"; import { Award, Download } from "lucide-react"; import { apiFetch, API_BASE } from "@/lib/api"; import { fetchApiFile, saveBlob } from "@/lib/export"; import { formatDate } from "@/lib/date"; import { errorMessage } from "@/lib/utils"; import type { MyCertificate } from "@/types"; // Completion certificates on My Events. Hidden until the person has one. export function MyCertificates({ token }: { token: string | null }) { const [certs, setCerts] = useState([]); const [error, setError] = useState(null); useEffect(() => { if (!token) return; apiFetch("/api/attendance/certificates/mine", { authToken: token }) .then(list => setCerts(Array.isArray(list) ? list : [])) .catch(() => setCerts([])); }, [token]); if (certs.length === 0) return null; const download = async (c: MyCertificate) => { if (!token) return; setError(null); try { saveBlob(await fetchApiFile(API_BASE, token, `/api/attendance/certificates/${c.id}/pdf`), `Certificate - ${c.event.title.replace(/[^a-z0-9 -]/gi, "")}.pdf`); } catch (e) { setError(errorMessage(e) || "Download failed"); } }; return (

My Certificates

{error &&
{error}
}
    {certs.map(c => (
  • {c.event.title}
    {c.name} · {c.totalSessions > 1 ? `${c.sessionsAttended} of ${c.totalSessions} sessions · ` : ""}issued {formatDate(new Date(c.issuedAt))}
  • ))}
); }