'use client'; import React, { useState } from 'react'; import { QRCodeSVG } from 'qrcode.react'; import { Card } from '@/components/ui/Card'; import { Input } from '@/components/ui/Input'; import { Button } from '@/components/ui/Button'; import { Badge } from '@/components/ui/Badge'; import { calculateContrast } from '@/lib/utils'; interface InstantGeneratorProps { t: any; // i18n translation function } export const InstantGenerator: React.FC = ({ t }) => { const [url, setUrl] = useState('https://example.com'); const [foregroundColor, setForegroundColor] = useState('#000000'); const [backgroundColor, setBackgroundColor] = useState('#FFFFFF'); const [cornerStyle, setCornerStyle] = useState('square'); const [size, setSize] = useState(200); const contrast = calculateContrast(foregroundColor, backgroundColor); const hasGoodContrast = contrast >= 4.5; const downloadQR = (format: 'svg' | 'png') => { const svg = document.querySelector('#instant-qr-preview svg'); if (!svg || !url) return; if (format === 'svg') { const svgData = new XMLSerializer().serializeToString(svg); const blob = new Blob([svgData], { type: 'image/svg+xml' }); const downloadUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = downloadUrl; a.download = 'qrcode.svg'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(downloadUrl); } else { // Convert SVG to PNG using Canvas const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d'); const img = new Image(); const svgData = new XMLSerializer().serializeToString(svg); const blob = new Blob([svgData], { type: 'image/svg+xml' }); const url = URL.createObjectURL(blob); img.onload = () => { canvas.width = size; canvas.height = size; if (ctx) { ctx.fillStyle = backgroundColor; ctx.fillRect(0, 0, size, size); ctx.drawImage(img, 0, 0, size, size); } canvas.toBlob((blob) => { if (blob) { const downloadUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = downloadUrl; a.download = 'qrcode.png'; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(downloadUrl); } }); URL.revokeObjectURL(url); }; img.src = url; } }; return (

{t.generator.title}

{/* Left Form */} setUrl(e.target.value)} placeholder={t.generator.url_placeholder} />
setForegroundColor(e.target.value)} className="w-12 h-10 rounded border border-gray-300" /> setForegroundColor(e.target.value)} className="flex-1" />
setBackgroundColor(e.target.value)} className="w-12 h-10 rounded border border-gray-300" /> setBackgroundColor(e.target.value)} className="flex-1" />
setSize(Number(e.target.value))} className="w-full" />
{size}px
{hasGoodContrast ? t.generator.contrast_good : 'Low contrast'}
Contrast: {contrast.toFixed(1)}:1
{/* Right Preview */}

{t.generator.live_preview}

{url ? (
) : (
Enter URL
)}
URL
{t.generator.demo_note}
); };