// 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 } from "../../../utils/SertificateGenerator"; import { resolveFileUrl } from "../../../utils/resolveFileFromUrl"; import { customToast } from "../../../utils/showToast"; interface CertificateAvailableProps { project: ProjectDetail; certificate: Certificate; templateUrl: string; } function formatDateIndo(date: string): string { return new Intl.DateTimeFormat("id-ID", { day: "numeric", month: "long", year: "numeric", }).format(new Date(date)); } function formatDateRange(startDate: string, endDate: string): string { return `${formatDateIndo(startDate)} - ${formatDateIndo(endDate)}`; } export default function CertificateAvailable({ project, certificate, templateUrl }: CertificateAvailableProps) { const [downloadingImg, setDownloadingImg] = useState(false); const [downloadingPdf, setDownloadingPdf] = useState(false); const [previewUrl, setPreviewUrl] = useState(null); const [loadingPreview, setLoadingPreview] = useState(true); const duration = formatDateRange(project?.start_date, project?.end_date) useEffect(() => { if (!templateUrl) return; const verifyUrl = `${window.location.origin}/verify-certificate/${certificate.uuid}`; generateCertificate( resolveFileUrl(templateUrl), certificate.user.full_name, certificate.project.project_name, certificate.certificate_no, duration, verifyUrl, ) .then((blob) => { const url = URL.createObjectURL(blob); setPreviewUrl(url); }) .finally(() => setLoadingPreview(false)); return () => { if (previewUrl) URL.revokeObjectURL(previewUrl); }; }, [templateUrl, certificate.user.full_name]); const handleDownloadImage = async () => { setDownloadingImg(true); try { await downloadCertificateImage(certificate, templateUrl, "png"); } catch { customToast.error("Gagal mengunduh sertifikat!", "Terjadi kendala saat mengunduh sertifikat!"); } finally { setDownloadingImg(false); } }; const handleDownloadPdf = async () => { setDownloadingPdf(true); try { await downloadCertificatePdf(certificate, templateUrl); } catch { customToast.error("Gagal mengunduh sertifikat!", "Terjadi kendala saat mengunduh sertifikat!"); } finally { setDownloadingPdf(false); } }; const handleShareLinkedIn = () => { const verifyUrl = `${window.location.origin}/verify-certificate/${certificate.uuid}`; const params = new URLSearchParams({ startTask: "CERTIFICATION_NAME", name: certificate.project.project_name, organizationName: "Internify", certUrl: verifyUrl, certId: certificate.certificate_no, }); window.open( `https://www.linkedin.com/profile/add?${params.toString()}`, "_blank", "width=600,height=600,noopener,noreferrer" ); }; 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 */}
); }