176 lines
6.4 KiB
React
176 lines
6.4 KiB
React
/*
|
|
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 <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import React, { useCallback, useMemo } from 'react';
|
|
import { Modal, Toast } from '@/components/ui';
|
|
import { Download, ExternalLink, X, FileText } from 'lucide-react';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
// 体验中心消息气泡里"点击附件 chip 弹出预览"的统一入口。
|
|
// - PDF:用 <iframe> 嵌入,浏览器原生 viewer 性能足够;
|
|
// - 其它(DOC/XLSX/PPT/TXT/CSV 等):仅给"在新窗口打开 + 下载"两个按钮,不引入额外解析器。
|
|
// - 图片:交给独立的 ImagePreviewModal,本组件不处理(不要重复造 zoom/旋转控件)。
|
|
const PDF_EXT = ['pdf'];
|
|
|
|
const FilePreviewModal = ({ visible, file, onClose }) => {
|
|
const { t } = useTranslation();
|
|
|
|
const fileName =
|
|
file?.filename || file?.file_name || file?.name || t('未知文件');
|
|
const fileUrl = file?.file_data || file?.url;
|
|
const ext = useMemo(
|
|
() => (fileName || '').split('.').pop().toLowerCase(),
|
|
[fileName],
|
|
);
|
|
const isPdf = PDF_EXT.includes(ext);
|
|
|
|
const handleDownload = useCallback(async () => {
|
|
if (!fileUrl) return;
|
|
try {
|
|
const response = await fetch(fileUrl);
|
|
const blob = await response.blob();
|
|
const url = window.URL.createObjectURL(blob);
|
|
const link = document.createElement('a');
|
|
link.href = url;
|
|
link.download = fileName;
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
requestAnimationFrame(() => {
|
|
document.body.removeChild(link);
|
|
window.URL.revokeObjectURL(url);
|
|
});
|
|
} catch {
|
|
// CORS / 签名过期等场景:直接新开窗口让浏览器自己处理
|
|
Toast.info({ content: t('在新窗口打开'), duration: 2 });
|
|
window.open(fileUrl, '_blank');
|
|
}
|
|
}, [fileUrl, fileName, t]);
|
|
|
|
const handleOpenInNewTab = useCallback(() => {
|
|
if (!fileUrl) return;
|
|
window.open(fileUrl, '_blank', 'noopener,noreferrer');
|
|
}, [fileUrl]);
|
|
|
|
if (!file) return null;
|
|
|
|
const btnClass =
|
|
'inline-flex items-center gap-1.5 px-3 py-1.5 rounded-lg bg-white/10 hover:bg-white/20 text-white/90 hover:text-white text-sm transition-all';
|
|
|
|
return (
|
|
<Modal
|
|
visible={visible}
|
|
onCancel={onClose}
|
|
footer={null}
|
|
closable={false}
|
|
maskClosable
|
|
width='100vw'
|
|
className='viewport-preview-modal'
|
|
style={{ maxWidth: '100vw', top: 0, padding: 0, margin: 0 }}
|
|
bodyStyle={{
|
|
padding: 0,
|
|
display: 'flex',
|
|
flexDirection: 'column',
|
|
// 移动端 URL bar 导致 vh 变化 → 用 dvh 取实际视口;老浏览器走 fallback 不崩
|
|
height: '100dvh',
|
|
background: 'rgba(0,0,0,0.92)',
|
|
}}
|
|
>
|
|
{/* Toolbar — 顶部 & 左右安全区,无刘海设备 env() 返回 0 行为一致 */}
|
|
<div
|
|
className='flex items-center justify-between px-4 py-3 bg-black/40 flex-shrink-0'
|
|
style={{
|
|
paddingTop: 'max(env(safe-area-inset-top), 0.75rem)',
|
|
paddingLeft: 'max(env(safe-area-inset-left), 1rem)',
|
|
paddingRight: 'max(env(safe-area-inset-right), 1rem)',
|
|
}}
|
|
>
|
|
<div className='flex items-center gap-2 min-w-0'>
|
|
<FileText size={18} className='text-white/70 flex-shrink-0' />
|
|
<span
|
|
className='text-white/90 text-sm truncate max-w-[40vw]'
|
|
title={fileName}
|
|
>
|
|
{fileName}
|
|
</span>
|
|
</div>
|
|
<div className='flex items-center gap-1.5'>
|
|
<button onClick={handleOpenInNewTab} className={btnClass}>
|
|
<ExternalLink size={14} />
|
|
{t('新窗口打开')}
|
|
</button>
|
|
<button onClick={handleDownload} className={btnClass}>
|
|
<Download size={14} />
|
|
{t('下载')}
|
|
</button>
|
|
<button
|
|
onClick={onClose}
|
|
className='w-9 h-9 flex items-center justify-center rounded-lg bg-white/10 hover:bg-white/20 text-white/80 hover:text-white transition-all'
|
|
title={t('关闭')}
|
|
>
|
|
<X size={20} />
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Preview body — 底部 home indicator + 左右安全区一并避开,PDF iframe 才不会被截一截 */}
|
|
<div
|
|
className='flex-1 flex items-center justify-center overflow-hidden p-4'
|
|
style={{
|
|
paddingBottom: 'max(env(safe-area-inset-bottom), 1rem)',
|
|
paddingLeft: 'max(env(safe-area-inset-left), 1rem)',
|
|
paddingRight: 'max(env(safe-area-inset-right), 1rem)',
|
|
}}
|
|
>
|
|
{isPdf ? (
|
|
<iframe
|
|
src={fileUrl}
|
|
title={fileName}
|
|
className='w-full h-full rounded-lg bg-white'
|
|
style={{ minHeight: '70dvh' }}
|
|
/>
|
|
) : (
|
|
<div className='flex flex-col items-center justify-center gap-4 px-6 py-10 rounded-2xl bg-white/5 border border-white/10 max-w-lg text-center'>
|
|
<FileText size={64} className='text-white/40' />
|
|
<div className='text-white/90 text-base font-medium break-all'>
|
|
{fileName}
|
|
</div>
|
|
<div className='text-white/50 text-sm'>
|
|
{t(
|
|
'此文件类型暂不支持在线预览,可下载到本地查看,或在新窗口打开。',
|
|
)}
|
|
</div>
|
|
<div className='flex items-center gap-2 mt-2'>
|
|
<button onClick={handleOpenInNewTab} className={btnClass}>
|
|
<ExternalLink size={14} />
|
|
{t('新窗口打开')}
|
|
</button>
|
|
<button onClick={handleDownload} className={btnClass}>
|
|
<Download size={14} />
|
|
{t('下载')}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</Modal>
|
|
);
|
|
};
|
|
|
|
export default FilePreviewModal;
|