/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import React, { useEffect, useRef, useState } from 'react'; import { Button, Image, Typography } from '@/components/ui'; import { Upload } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { showError } from '../../helpers'; import { validateImageFile } from './model'; import { uploadWebsiteQrCode } from './settingsApi'; export default function QrCodeUpload({ value, onChange, busy, onBusyChange, resetVersion, invalid, }) { const { t } = useTranslation(); const inputRef = useRef(null); const requestVersion = useRef(0); const [uploading, setUploading] = useState(false); useEffect(() => { requestVersion.current += 1; setUploading(false); onBusyChange(false); if (inputRef.current) inputRef.current.value = ''; return () => { requestVersion.current += 1; onBusyChange(false); }; }, [resetVersion, onBusyChange]); const upload = async (event) => { const file = event.target.files?.[0]; event.target.value = ''; if (!file) return; const error = validateImageFile(file); if (error) { showError(t(error)); return; } const version = ++requestVersion.current; setUploading(true); onBusyChange(true); try { // Verify the content is decodable, rather than trusting the filename or MIME alone. try { const bitmap = await createImageBitmap(file); bitmap.close(); } catch { throw new Error( '无法读取二维码图片,请选择有效的 JPG、PNG 或 WebP 图片', ); } if (version !== requestVersion.current) return; const url = await uploadWebsiteQrCode(file); if (version === requestVersion.current) onChange(url); } catch (err) { if (version === requestVersion.current) showError(t(err.message || '二维码上传失败,请选择有效图片后重试')); } finally { if (version === requestVersion.current) { setUploading(false); onBusyChange(false); } } }; return (
{value ? (
{t('企业微信二维码')}
) : ( )}
{t('支持 JPG、PNG、WebP,单张不超过 2 MB')} {value && ( {t('点击图片可放大预览')} )}
); }