// CertificateAvailable.tsx import { useState, useEffect } from "react"; import { Download, Link, Printer } from "lucide-react"; import type { ProjectDetail } from "../../../../types/project.types"; import type { Certificate } from "../../../../types/certificate.types"; import { downloadCertificatePdf, downloadCertificateImage } from "../../../utils/Certificates"; import { generateCertificate, resolveImageUrl } from "../../../utils/SertificateGenerator"; interface CertificateAvailableProps { project: ProjectDetail | null; certificate: Certificate; templateUrl: string; } export default function CertificateAvailable({ project, certificate, templateUrl }: CertificateAvailableProps) { // const [copied, setCopied] = useState(false); const [downloadingImg, setDownloadingImg] = useState(false); const [downloadingPdf, setDownloadingPdf] = useState(false); const [previewUrl, setPreviewUrl] = useState(null); const [loadingPreview, setLoadingPreview] = useState(true); useEffect(() => { if (!templateUrl) return; generateCertificate(resolveImageUrl(templateUrl), certificate.user.full_name, certificate.project.project_name, certificate.certificate_no, certificate.uuid) .then((blob) => { const url = URL.createObjectURL(blob); setPreviewUrl(url); }) .finally(() => setLoadingPreview(false)); // Cleanup supaya tidak memory leak return () => { if (previewUrl) URL.revokeObjectURL(previewUrl); }; }, [templateUrl, certificate.user.full_name]); const duration = project ? (() => { const start = new Date(project.start_date); const end = new Date(project.end_date); const diffMs = end.getTime() - start.getTime(); const diffDays = Math.ceil(diffMs / (1000 * 60 * 60 * 24)); const months = Math.floor(diffDays / 30); const days = diffDays % 30; if (months > 0 && days > 0) return `${months} Bulan ${days} Hari`; if (months > 0) return `${months} Bulan`; return `${diffDays} Hari`; })() : "-"; const handleDownloadImage = async () => { setDownloadingImg(true); try { await downloadCertificateImage(certificate, templateUrl, "png"); } finally { setDownloadingImg(false); } }; const handleDownloadPdf = async () => { setDownloadingPdf(true); try { await downloadCertificatePdf(certificate, templateUrl); } finally { setDownloadingPdf(false); } }; return (
{/* Certificate Card */}
{loadingPreview ? (
Memuat sertifikat...
) : previewUrl ? ( Certificate ) : (
Gagal memuat sertifikat.
)}
{/* Program Summary */}

Ringkasan Program

{[ { label: "Nama Program", value: project?.project_name ?? "-" }, { label: "Durasi", value: duration }, { label: "No. Sertifikat", value: certificate.certificate_no ?? "-" }, ].map((row) => (
{row.label} {row.value}
))}
{/* Unduh Sertifikat */} {/* Bagikan ke LinkedIn */} {/* Cetak & Salin Link */}
); }