Files
monkey-ai-web/src/pages/ExperienceCenter/components/AICreation/ImageDetailModal.jsx
T
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

292 lines
9.3 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
Download as ActionDownload,
Copy as ActionCopy,
} from '@/components/common/icons/ActionIcons';
import { copyText } from '../../../../helpers/clipboard';
import React, { useState, useEffect } from 'react';
import { Modal, Image, Button, Space, Typography, Tag } from '@/components/ui';
import { 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 (
<Modal
title={t('图片详情')}
visible={visible}
onCancel={onClose}
footer={
// wrap 必须开:三个中文按钮(用作参考图/在此基础上微调/下载图片)合计 ~370px,
// 而 modalWidth 是 viewportWidth-48,390px 屏上扣掉 content 的 padding 只剩 ~294px。
// Space 默认 wrap:false + .ant-modal-container{overflow:hidden},
// 溢出部分向左溢出并被裁掉 —— 「用作参考图」整个点不到。
<Space wrap style={{ justifyContent: 'flex-end', width: '100%' }}>
{onUseAsReference && task.imageUrl && (
<Button
icon={<ImagePlus size={16} />}
onClick={handleUseAsReferenceClick}
>
{t('用作参考图')}
</Button>
)}
{onReuseParams && (
<Button
icon={<Sparkles size={16} />}
theme='light'
type='primary'
onClick={handleReuseParamsClick}
>
{t('在此基础上微调')}
</Button>
)}
<Button
type='primary'
icon={<ActionDownload size={16} />}
onClick={handleDownload}
disabled={!task.imageUrl}
>
{t('下载图片')}
</Button>
</Space>
}
width={modalWidth}
bodyStyle={{
maxHeight: '75vh',
overflowX: 'hidden',
overflowY: 'auto',
padding: '20px 24px',
}}
>
{/* 图片展示 */}
<div className='text-center mb-5'>
{isImageToImage && task.sourceImageUrl ? (
<div className='flex gap-4 items-start justify-center'>
<div className='flex-1' style={{ maxWidth: '48%' }}>
<Text type='secondary' size='small' className='block mb-2'>
{t('原图')}
</Text>
<Image
src={task.sourceImageUrl}
alt='Source'
className='rounded-lg border border-gray-200 dark:border-neutral-700'
style={{ maxWidth: '100%', maxHeight: 360 }}
preview={{ src: task.sourceImageUrl }}
/>
</div>
<div className='flex-1' style={{ maxWidth: '48%' }}>
<Text type='secondary' size='small' className='block mb-2'>
{t('生成图')}
</Text>
<Image
src={task.imageUrl}
alt='Generated'
className='rounded-lg border border-gray-200 dark:border-neutral-700'
style={{ maxWidth: '100%', maxHeight: 360 }}
preview={{ src: task.imageUrl }}
/>
</div>
</div>
) : task.imageUrl ? (
<Image
src={task.imageUrl}
alt='Generated'
className='rounded-lg border border-gray-200 dark:border-neutral-700'
style={{ maxWidth: '100%', maxHeight: 520 }}
preview={{ src: task.imageUrl }}
/>
) : (
<div className='py-10 text-gray-400'>
<Text type='tertiary'>{t('暂无图片')}</Text>
</div>
)}
</div>
{/* 参数 Tag 行 */}
{params.length > 0 && (
<div className='flex items-center gap-1.5 flex-wrap mb-4'>
{params.map((p) => (
<Tag
key={p.label}
color={p.color}
size='small'
className={`!rounded ${
p.brand
? '!border-brand/25 !bg-brand/10 !text-brand-deep dark:!border-brand/25 dark:!bg-brand/15 dark:!text-brand-from'
: ''
}`}
>
<span className='text-gray-500 mr-1'>{p.label}:</span>
<strong>{p.value}</strong>
</Tag>
))}
<Tag size='small' color='grey' className='!rounded'>
<span className='text-gray-500 mr-1'>{t('创建')}:</span>
<strong>{formatDateTime(task.createdAt)}</strong>
</Tag>
</div>
)}
{/* 提示词 */}
<div className='mb-3'>
<div className='flex items-center justify-between mb-1.5'>
<Text strong>{t('提示词')}</Text>
<Button
size='small'
theme='borderless'
icon={<ActionCopy size={14} />}
onClick={handleCopyPrompt}
className='!text-gray-500 hover:!text-brand-deep dark:hover:!text-brand-from'
>
{t('复制')}
</Button>
</div>
<div
className='p-3 rounded-lg bg-gray-50 dark:bg-neutral-800/60 text-sm leading-relaxed whitespace-pre-wrap break-words'
style={{ minHeight: 60 }}
>
{task.prompt || <Text type='tertiary'>{t('(无提示词)')}</Text>}
</div>
</div>
{/* 负向提示词(如果有) */}
{task.negativePrompt && (
<div className='mb-3'>
<Text strong className='block mb-1.5'>
{t('负向提示词')}
</Text>
<div className='p-3 rounded-lg bg-rose-50 dark:bg-rose-900/10 text-sm leading-relaxed whitespace-pre-wrap break-words'>
{task.negativePrompt}
</div>
</div>
)}
{/* 失败信息 */}
{task.status === 'failed' && task.errorMessage && (
<div className='mb-3'>
<Text
strong
className='block mb-1.5 text-rose-600 dark:text-rose-400'
>
{t('失败原因')}
</Text>
<div className='p-3 rounded-lg bg-rose-50 dark:bg-rose-900/10 text-xs text-rose-700 dark:text-rose-300 whitespace-pre-wrap break-words'>
{task.errorMessage}
</div>
</div>
)}
</Modal>
);
};
export default ImageDetailModal;