|
| 1 | +import React, { useState, useCallback, useEffect } from 'react'; |
| 2 | +import usePageTitle from '../../utils/usePageTitle'; |
| 3 | +import { Link } from 'react-router-dom'; |
| 4 | +import { ArrowLeftIcon, QrCode, DownloadSimple } from '@phosphor-icons/react'; |
| 5 | +import colors from '../../config/colors'; |
| 6 | +import { useToast } from '../../hooks/useToast'; |
| 7 | +import { QRCodeCanvas } from 'qrcode.react'; // Import the QRCodeCanvas component |
| 8 | +import '../../styles/app-buttons.css'; |
| 9 | + |
| 10 | +const QrCodeGeneratorPage = () => { |
| 11 | + usePageTitle('QR Code Generator'); |
| 12 | + const { addToast } = useToast(); |
| 13 | + |
| 14 | + const [text, setText] = useState('https://fezcode.com'); |
| 15 | + const [version, setVersion] = useState(7); // Default QR code version |
| 16 | + const [errorCorrectionLevel, setErrorCorrectionLevel] = useState('M'); // L, M, Q, H |
| 17 | + const [qrCodeSize, setQrCodeSize] = useState(256); // Size of the QR code in pixels |
| 18 | + |
| 19 | + const cardStyle = { |
| 20 | + backgroundColor: colors['app-alpha-10'], |
| 21 | + borderColor: colors['app-alpha-50'], |
| 22 | + color: colors.app, |
| 23 | + }; |
| 24 | + |
| 25 | + const inputStyle = `mt-1 block w-full p-2 border rounded-md bg-gray-700 text-white focus:ring-blue-500 focus:border-blue-500 border-gray-600`; |
| 26 | + const selectStyle = `mt-1 block w-full p-2 border border-gray-600 rounded-md bg-gray-700 text-white focus:ring-blue-500 focus:border-blue-500`; |
| 27 | + const buttonStyle = `px-6 py-2 rounded-md text-lg font-arvo font-normal transition-colors duration-300 ease-in-out roll-button`; |
| 28 | + |
| 29 | + const downloadQrCode = useCallback(() => { |
| 30 | + const canvas = document.getElementById('qrCodeCanvas'); |
| 31 | + if (canvas) { |
| 32 | + const pngUrl = canvas.toDataURL('image/png'); |
| 33 | + const downloadLink = document.createElement('a'); |
| 34 | + downloadLink.href = pngUrl; |
| 35 | + downloadLink.download = 'qrcode.png'; |
| 36 | + document.body.appendChild(downloadLink); |
| 37 | + downloadLink.click(); |
| 38 | + document.body.removeChild(downloadLink); |
| 39 | + addToast({ title: 'Downloaded', message: 'QR Code downloaded successfully.', duration: 2000 }); |
| 40 | + } else { |
| 41 | + addToast({ title: 'Error', message: 'Could not find QR Code to download.', duration: 3000, type: 'error' }); |
| 42 | + } |
| 43 | + }, [addToast]); |
| 44 | + |
| 45 | + return ( |
| 46 | + <div className="py-16 sm:py-24"> |
| 47 | + <div className="mx-auto max-w-7xl px-6 lg:px-8 text-gray-300"> |
| 48 | + <Link |
| 49 | + to="/apps" |
| 50 | + className="text-article hover:underline flex items-center justify-center gap-2 text-lg mb-4" |
| 51 | + > |
| 52 | + <ArrowLeftIcon size={24} /> Back to Apps |
| 53 | + </Link> |
| 54 | + <h1 className="text-4xl font-bold tracking-tight sm:text-6xl mb-4 flex items-center"> |
| 55 | + <span className="codex-color">fc</span> |
| 56 | + <span className="separator-color">::</span> |
| 57 | + <span className="apps-color">apps</span> |
| 58 | + <span className="separator-color">::</span> |
| 59 | + <span className="single-app-color">qr</span> |
| 60 | + </h1> |
| 61 | + <hr className="border-gray-700" /> |
| 62 | + <div className="flex justify-center items-center mt-16"> |
| 63 | + <div |
| 64 | + className="group bg-transparent border rounded-lg shadow-2xl p-6 flex flex-col justify-between relative transform overflow-hidden h-full w-full max-w-4xl" |
| 65 | + style={cardStyle} |
| 66 | + > |
| 67 | + <div |
| 68 | + className="absolute top-0 left-0 w-full h-full opacity-10" |
| 69 | + style={{ |
| 70 | + backgroundImage: |
| 71 | + 'radial-gradient(circle, white 1px, transparent 1px)', |
| 72 | + backgroundSize: '10px 10px', |
| 73 | + }} |
| 74 | + ></div> |
| 75 | + <div className="relative z-10 p-1"> |
| 76 | + <h1 className="text-3xl font-arvo font-normal mb-4 text-app"> QR Code Generator </h1> |
| 77 | + <hr className="border-gray-700 mb-4" /> |
| 78 | + |
| 79 | + {/* Client-Side Notification */} |
| 80 | + <div className="bg-yellow-900 bg-opacity-30 border border-yellow-700 text-yellow-300 px-4 py-3 rounded relative mb-6" role="alert"> |
| 81 | + <strong className="font-bold">Client-Side Only:</strong> |
| 82 | + <span className="block sm:inline ml-2">This tool operates entirely within your browser. No data is sent to any server, ensuring maximum privacy.</span> |
| 83 | + </div> |
| 84 | + |
| 85 | + <div className="mb-6"> |
| 86 | + <label htmlFor="qrText" className="block text-sm font-medium text-gray-300 mb-2"> |
| 87 | + Text or URL to Encode |
| 88 | + </label> |
| 89 | + <textarea |
| 90 | + id="qrText" |
| 91 | + rows="3" |
| 92 | + className={inputStyle} |
| 93 | + value={text} |
| 94 | + onChange={(e) => setText(e.target.value)} |
| 95 | + placeholder="Enter text or URL here..." |
| 96 | + ></textarea> |
| 97 | + </div> |
| 98 | + |
| 99 | + <div className="grid grid-cols-1 sm:grid-cols-3 gap-6 mb-6"> |
| 100 | + <div> |
| 101 | + <label htmlFor="qrVersion" className="block text-sm font-medium text-gray-300 mb-2"> |
| 102 | + QR Version (1-40) |
| 103 | + </label> |
| 104 | + <select |
| 105 | + id="qrVersion" |
| 106 | + className={selectStyle} |
| 107 | + value={version} |
| 108 | + onChange={(e) => setVersion(Number(e.target.value))} |
| 109 | + > |
| 110 | + {Array.from({ length: 40 }, (_, i) => i + 1).map((v) => ( |
| 111 | + <option key={v} value={v}> |
| 112 | + Version {v} |
| 113 | + </option> |
| 114 | + ))} |
| 115 | + </select> |
| 116 | + </div> |
| 117 | + <div> |
| 118 | + <label htmlFor="errorCorrection" className="block text-sm font-medium text-gray-300 mb-2"> |
| 119 | + Error Correction |
| 120 | + </label> |
| 121 | + <select |
| 122 | + id="errorCorrection" |
| 123 | + className={selectStyle} |
| 124 | + value={errorCorrectionLevel} |
| 125 | + onChange={(e) => setErrorCorrectionLevel(e.target.value)} |
| 126 | + > |
| 127 | + <option value="L">L (Low ~7%)</option> |
| 128 | + <option value="M">M (Medium ~15%)</option> |
| 129 | + <option value="Q">Q (Quartile ~25%)</option> |
| 130 | + <option value="H">H (High ~30%)</option> |
| 131 | + </select> |
| 132 | + </div> |
| 133 | + <div> |
| 134 | + <label htmlFor="qrSize" className="block text-sm font-medium text-gray-300 mb-2"> |
| 135 | + Size (px) |
| 136 | + </label> |
| 137 | + <input |
| 138 | + type="number" |
| 139 | + id="qrSize" |
| 140 | + className={inputStyle} |
| 141 | + value={qrCodeSize} |
| 142 | + onChange={(e) => setQrCodeSize(Number(e.target.value))} |
| 143 | + min="64" |
| 144 | + max="768" |
| 145 | + step="16" |
| 146 | + /> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + |
| 150 | + {text && ( |
| 151 | + <div className="flex flex-col items-center justify-center mb-6"> |
| 152 | + <QRCodeCanvas |
| 153 | + id="qrCodeCanvas" // ID for canvas to download |
| 154 | + value={text} |
| 155 | + size={qrCodeSize} |
| 156 | + level={errorCorrectionLevel} |
| 157 | + version={version} |
| 158 | + bgColor="#FFFFFF" |
| 159 | + fgColor="#000000" |
| 160 | + includeMargin={true} |
| 161 | + // renderAs="canvas" is not needed for QRCodeCanvas as it defaults to canvas |
| 162 | + /> |
| 163 | + <button |
| 164 | + onClick={downloadQrCode} |
| 165 | + className={`${buttonStyle} mt-4`} |
| 166 | + style={{ |
| 167 | + backgroundColor: 'rgba(0, 0, 0, 0.2)', |
| 168 | + color: cardStyle.color, |
| 169 | + borderColor: cardStyle.borderColor, |
| 170 | + border: '1px solid', |
| 171 | + }} |
| 172 | + > |
| 173 | + <DownloadSimple size={20} className="inline-block mr-2" /> Download QR Code |
| 174 | + </button> |
| 175 | + </div> |
| 176 | + )} |
| 177 | + {!text && ( |
| 178 | + <div className="text-center text-gray-500 mb-6"> |
| 179 | + Enter text or URL to generate QR Code. |
| 180 | + </div> |
| 181 | + )} |
| 182 | + </div> |
| 183 | + </div> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + </div> |
| 187 | + ); |
| 188 | +}; |
| 189 | + |
| 190 | +export default QrCodeGeneratorPage; |
0 commit comments