diff --git a/src/components/CenterPanel/DownloadButton.tsx b/src/components/CenterPanel/DownloadButton.tsx new file mode 100644 index 0000000..cbd5c0b --- /dev/null +++ b/src/components/CenterPanel/DownloadButton.tsx @@ -0,0 +1,109 @@ +import { useState } from 'react'; +import { useQR } from '../../context/QRContext'; +import { useTheme } from '../../context/ThemeContext'; +import { useToast } from '../../context/ToastContext'; +import { useHistory } from '../../context/HistoryContext'; +import { encodeQR } from '../../utils/encodeQR'; +import { downloadQR, copyQR } from './QRPreview'; + +export function DownloadButton() { + const { state } = useQR(); + const { isDark } = useTheme(); + const { show } = useToast(); + const { push } = useHistory(); + const [showFmts, setShowFmts] = useState(false); + const isActive = !!encodeQR(state); + const canShare = typeof navigator !== 'undefined' && 'share' in navigator; + + const handleDownload = (fmt: 'png' | 'svg') => { + if (!isActive) return; + downloadQR(state, isDark, fmt); + show(`Downloaded as ${fmt.toUpperCase()}`, 'success'); + push(state, encodeQR(state) ?? ''); + setShowFmts(false); + }; + + const handleCopy = async () => { + if (!isActive) return; + try { + await copyQR(state, isDark); + show('Copied to clipboard', 'success'); + } catch { + show('Copy not supported in this browser', 'error'); + } + }; + + const handleShare = async () => { + if (!isActive || !canShare) return; + try { + await navigator.share({ title: 'CQRC Platform QR Code', text: encodeQR(state) ?? '', url: 'https://qrcode.asabbagh.com' }); + push(state, encodeQR(state) ?? ''); + } catch { /* user cancelled */ } + }; + + const btnStyle = (active: boolean) => active + ? { background: 'var(--bg-elevated)', border: '1px solid var(--border-sub)', color: 'var(--text-secondary)', cursor: 'pointer' } + : { background: 'var(--bg-card)', border: '1px solid var(--border)', color: 'var(--text-dim)', cursor: 'not-allowed' }; + + return ( +