import React, { useEffect, useRef, useState } from 'react'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'; import { Button } from '@/components/ui/button'; import { useTranslation } from 'react-i18next'; import QRCode from 'qrcode'; interface QRCodeModalProps { isOpen: boolean; onClose: () => void; url: string; title: string; } export function QRCodeModal({ isOpen, onClose, url, title }: QRCodeModalProps) { const { t } = useTranslation(); const canvasRef = useRef(null); const [qrUrl, setQrUrl] = useState(''); useEffect(() => { if (isOpen) { // Generate QR code as data URL QRCode.toDataURL(url, { width: 300, margin: 2, color: { dark: '#000000', light: '#ffffff' } }) .then(dataUrl => { setQrUrl(dataUrl); }) .catch(err => { console.error('Error generating QR code:', err); }); } }, [isOpen, url]); const handleDownload = () => { if (qrUrl) { const link = document.createElement('a'); link.download = `${title.replace(/\s+/g, '-').toLowerCase()}-qrcode.png`; link.href = qrUrl; link.click(); } }; return ( {t('QR Code for')} {title}
{qrUrl ? ( QR Code ) : (

Loading QR code...

)}

{t('Scan this QR code to access the vCard')}

); }