import { useState, type RefObject } from 'react' import html2canvas from 'html2canvas' import { jsPDF } from 'jspdf' type Props = { targetRef: RefObject } export function ExportDashboardButton({ targetRef }: Props) { const [busy, setBusy] = useState<'png' | 'pdf' | null>(null) const runExport = async (mode: 'png' | 'pdf') => { const el = targetRef.current if (!el) return setBusy(mode) try { const canvas = await html2canvas(el, { scale: 2, useCORS: true, logging: false, backgroundColor: '#020617', }) const stamp = new Date().toISOString().slice(0, 19).replace(/[:T]/g, '-') if (mode === 'png') { canvas.toBlob((blob) => { if (!blob) return const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = `dashboard-copil-${stamp}.png` a.click() URL.revokeObjectURL(url) }, 'image/png') } else { const imgData = canvas.toDataURL('image/png') const pdf = new jsPDF({ orientation: 'landscape', unit: 'pt', format: 'a4' }) const pageW = pdf.internal.pageSize.getWidth() const pageH = pdf.internal.pageSize.getHeight() const img = new Image() await new Promise((resolve, reject) => { img.onload = () => resolve() img.onerror = () => reject(new Error('image')) img.src = imgData }) const ratio = Math.min(pageW / img.width, pageH / img.height) const w = img.width * ratio const h = img.height * ratio const x = (pageW - w) / 2 const y = (pageH - h) / 2 pdf.addImage(imgData, 'PNG', x, y, w, h) pdf.save(`dashboard-copil-${stamp}.pdf`) } } catch { alert('Export impossible (vérifiez les bloqueurs ou réessayez).') } finally { setBusy(null) } } return (
) }