"use client"; import React, { useEffect, useState } from "react"; import { Award, Download } from "lucide-react"; import { 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"; import { apiV2Data } from "@/lib/apiV2"; // 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; (apiV2Data("get", "/me/certificates", { authToken: token }) as Promise) .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/v2/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))}
  • ))}
); }