// CertificateAvailable.tsx import { useState, useRef } from "react"; import { Download, Link, Printer } from "lucide-react"; import type { ProjectDetail } from "../../../../types/project.types"; import type { Certificate } from "../../../../types/certificate.types"; import { downloadCertificateAsImage, downloadCertificateAsPdf } from "../../../utils/DownloadImagetoDevice"; interface CertificateAvailableProps { project: ProjectDetail | null; certificate: Certificate; } export default function CertificateAvailable({ project, certificate }: CertificateAvailableProps) { const [copied, setCopied] = useState(false); const [downloadingImg, setDownloadingImg] = useState(false); const [downloadingPdf, setDownloadingPdf] = useState(false); const certificateRef = useRef(null); const handleCopyLink = () => { navigator.clipboard.writeText(window.location.href); setCopied(true); setTimeout(() => setCopied(false), 2000); }; // Hitung durasi dari start_date - end_date 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); await downloadCertificateAsImage( certificateRef.current, `Sertifikat - ${certificate.certificate_no}`, "png", certificate.image_path ); setDownloadingImg(false); }; const handleDownloadPdf = async () => { setDownloadingPdf(true); await downloadCertificateAsPdf( certificateRef.current, `Sertifikat - ${certificate.certificate_no}` ); setDownloadingPdf(false); }; return (
{/* Certificate Card */}
Certificate
{/* Program Summary */}

Ringkasan Program

{[ { label: "Nama Program", value: project?.project_name ?? "-" }, { label: "Durasi", value: duration }, { label: "Total Tugas", value: project ? `${project.total_tasks} Tugas` : "-" }, { label: "Status", value: ( {certificate.certificate_status.toUpperCase()} ), }, ].map((row) => (
{row.label} {row.value}
))}
{/* Unduh Sertifikat */} {/* Bagikan ke LinkedIn */} {/* Cetak & Salin Link */}
); }