import React, { useState, useEffect, useRef } from 'react' import { generateQRCode, generateSVGQRCode, addImageToQRCode, addImageToSVG, generateCompleteSVGQRCode, downloadSVG, fileToDataURL, validateImageFile } from '../utils/qrCodeUtils' const TextAreaComponent = () => { const [text, setText] = useState('') const [qrCodeUrl, setQrCodeUrl] = useState('') const [customImage, setCustomImage] = useState(null) const [customImageUrl, setCustomImageUrl] = useState('') const [imageSize, setImageSize] = useState(25) // Size as percentage of QR code const [foregroundColor, setForegroundColor] = useState('#0000ff') // QR code foreground color const [backgroundColor, setBackgroundColor] = useState('#FFFFFF') // QR code background color const canvasRef = useRef(null) // Generate QR code when text, custom image, image size, or colors change useEffect(() => { if (text.trim()) { generateQRCodeLocal(text) } else { setQrCodeUrl('') } }, [text, customImage, imageSize, foregroundColor, backgroundColor]) const generateQRCodeLocal = async (inputText) => { try { // Generate QR code as data URL using utility function const url = await generateQRCode(inputText, foregroundColor, backgroundColor) if (customImage) { // Create QR code with custom image overlay const qrWithImage = await addImageToQRCode(url, customImageUrl, imageSize) setQrCodeUrl(qrWithImage) } else { setQrCodeUrl(url) } } catch (error) { console.error('Error generating QR code:', error) setQrCodeUrl('') } } const generateSVGQRCodeLocal = async (inputText) => { try { console.log('Base SVG generated, length:', inputText.length) if (customImage) { console.log('Custom image detected, adding to SVG...') // Add custom image to SVG using utility function const svgWithImage = await addImageToSVG(inputText, customImageUrl, imageSize) console.log('SVG with image generated, length:', svgWithImage.length) return svgWithImage } else { console.log('No custom image, returning base SVG') return inputText } } catch (error) { console.error('Error generating SVG QR code:', error) return null } } const exportAsSVG = async () => { if (!text.trim()) { alert('Please enter some text to generate a QR code') return } // Generate base SVG QR code using utility function const baseSvg = await generateSVGQRCode(text, foregroundColor, backgroundColor) if (!baseSvg) { alert('Error generating QR code') return } // Add custom image if present const svgContent = await generateSVGQRCodeLocal(baseSvg) if (svgContent) { // Debug: Log the SVG content to see what's being generated console.log('Generated SVG content:', svgContent) // Use utility function to download SVG downloadSVG(svgContent, 'qrcode.svg') } } const handleImageUpload = async (event) => { const file = event.target.files[0] if (file) { // Validate file using utility function const validation = validateImageFile(file) if (!validation.success) { alert(validation.error) return } try { // Convert file to data URL using utility function const dataUrl = await fileToDataURL(file) setCustomImage(file) setCustomImageUrl(dataUrl) } catch (error) { console.error('Error processing image file:', error) alert('Error processing image file') } } } const removeCustomImage = () => { setCustomImage(null) setCustomImageUrl('') } const handleTextChange = (e) => { setText(e.target.value) } const handleClear = () => { setText('') } return (
Scan this QR code with any QR code reader app