/* 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 { copyText } from '../../../../helpers/clipboard'; import React, { useState, useEffect } from 'react'; import { Modal, Image, Button, Space, Typography, Tag } from '@/components/ui'; import { Download, Copy, ImagePlus, Sparkles } from 'lucide-react'; import { useTranslation } from 'react-i18next'; import { showSuccess, showError } from '../../../../helpers'; const { Text } = Typography; const ImageDetailModal = ({ visible, task, onClose, onReuseParams, onUseAsReference, }) => { const { t, i18n } = useTranslation(); // 跟随视口宽度,避免 900px 固定值在窄笔记本/平板上溢出 const [viewportWidth, setViewportWidth] = useState( typeof window !== 'undefined' ? window.innerWidth : 900, ); useEffect(() => { if (typeof window === 'undefined') return; const onResize = () => setViewportWidth(window.innerWidth); window.addEventListener('resize', onResize); return () => window.removeEventListener('resize', onResize); }, []); const modalWidth = Math.min(900, Math.max(320, viewportWidth - 48)); if (!task) return null; const handleDownload = async () => { if (!task.imageUrl) { showError(t('图片不存在')); return; } try { const response = await fetch(task.imageUrl); const blob = await response.blob(); const url = window.URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = `image_${task.id}.png`; link.style.display = 'none'; document.body.appendChild(link); link.click(); requestAnimationFrame(() => { document.body.removeChild(link); window.URL.revokeObjectURL(url); }); showSuccess(t('下载成功')); } catch (error) { console.error('下载失败:', error); window.open(task.imageUrl, '_blank'); showSuccess(t('已在新窗口打开图片')); } }; // 复制(带 execCommand 兜底,兼容 http / 老浏览器) const copyToClipboard = copyText; const handleCopyPrompt = async () => { if (!task.prompt) return; const ok = await copyToClipboard(task.prompt); ok ? showSuccess(t('已复制到剪贴板')) : showError(t('复制失败')); }; const formatDateTime = (isoString) => { if (!isoString) return '-'; const date = new Date(isoString); return date.toLocaleString(i18n.language, { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }); }; const isImageToImage = task.mode === 'image-to-image'; // 参数行:值为空就不显示 const params = [ { label: t('模型'), value: task.model, color: 'grey', brand: true }, { label: t('尺寸'), value: task.width && task.height ? `${task.width}×${task.height}` : null, color: 'grey', }, { label: t('模式'), value: isImageToImage ? t('图生图') : t('文生图'), color: 'grey', }, { label: t('质量'), value: task.quality, color: 'grey' }, { label: t('数量'), value: task.n && task.n > 1 ? t('{{n}} 张', { n: task.n }) : null, color: 'grey', }, ].filter((p) => p.value); const handleReuseParamsClick = () => { if (onReuseParams) { onReuseParams(task); onClose(); } }; const handleUseAsReferenceClick = () => { if (onUseAsReference) { onUseAsReference(task); onClose(); } }; return ( {onUseAsReference && task.imageUrl && ( )} {onReuseParams && ( )} } width={modalWidth} bodyStyle={{ maxHeight: '75vh', overflowX: 'hidden', overflowY: 'auto', padding: '20px 24px', }} > {/* 图片展示 */}
{isImageToImage && task.sourceImageUrl ? (
{t('原图')} Source
{t('生成图')} Generated
) : task.imageUrl ? ( Generated ) : (
{t('暂无图片')}
)}
{/* 参数 Tag 行 */} {params.length > 0 && (
{params.map((p) => ( {p.label}: {p.value} ))} {t('创建')}: {formatDateTime(task.createdAt)}
)} {/* 提示词 */}
{t('提示词')}
{task.prompt || {t('(无提示词)')}}
{/* 负向提示词(如果有) */} {task.negativePrompt && (
{t('负向提示词')}
{task.negativePrompt}
)} {/* 失败信息 */} {task.status === 'failed' && task.errorMessage && (
{t('失败原因')}
{task.errorMessage}
)}
); }; export default ImageDetailModal;