Files
monkey-ai-web/src/components/common/DocumentRenderer/AgreementModal.jsx
T

166 lines
6.0 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, { useState, useCallback } from 'react';
import { Modal } from '@/components/ui';
import { X, Shield, Scale, ScrollText, ExternalLink } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import DocumentRenderer from './index';
import { useIsMobile } from '../../../hooks/common/useIsMobile';
const getIconByType = (type) => {
if (type === 'privacy') return Shield;
if (type === 'agreement') return Scale;
return ScrollText;
};
const AGREEMENT_CONFIGS = {
agreement: {
apiEndpoint: '/api/user-agreement',
cacheKey: 'user_agreement',
fullPagePath: '/user-agreement',
titleKey: '服务协议',
emptyKey: '加载服务协议内容失败...',
},
privacy: {
apiEndpoint: '/api/privacy-policy',
cacheKey: 'privacy_policy',
fullPagePath: '/privacy-policy',
titleKey: '隐私政策',
emptyKey: '加载隐私政策内容失败...',
},
};
/**
* 协议预览弹窗(用于登录/注册页内嵌预览)
* @param {boolean} visible
* @param {'agreement'|'privacy'} type
* @param {function} onClose
*/
const AgreementModal = ({ visible, type, onClose }) => {
const { t } = useTranslation();
const isMobile = useIsMobile();
const [progress, setProgress] = useState(0);
const handleProgressChange = useCallback((p) => setProgress(p), []);
const config = type ? AGREEMENT_CONFIGS[type] : null;
if (!config) return null;
const IconComponent = getIconByType(type);
const title = t(config.titleKey);
return (
<Modal
title={null}
header={null}
footer={null}
visible={visible}
onCancel={onClose}
closeIcon={null}
maskClosable={true}
centered
width={isMobile ? '94%' : 720}
bodyStyle={{
padding: 0,
borderRadius: 16,
overflow: 'hidden',
}}
className='agreement-preview-modal'
maskStyle={{
backgroundColor: 'rgba(15, 18, 28, 0.55)',
backdropFilter: 'blur(8px)',
}}
>
<div
className='flex flex-col bg-white dark:bg-gray-900 ring-1 ring-gray-200/60 dark:ring-gray-800/60 rounded-2xl overflow-hidden'
style={{ height: isMobile ? '85vh' : '78vh' }}
>
{/* 阅读进度条 */}
<div
className='h-[2px] bg-gradient-to-r from-brand-from via-brand to-brand-to origin-left transition-transform duration-150 ease-out'
style={{ transform: `scaleX(${progress / 100})` }}
aria-hidden
/>
{/* 头部 */}
<div className='relative flex items-center justify-between px-5 sm:px-6 h-14 border-b border-gray-200/80 dark:border-gray-800/80 flex-shrink-0 bg-white/95 dark:bg-gray-900/95 backdrop-blur-md'>
{/* 头部环境光 */}
<div
className='pointer-events-none absolute inset-0 overflow-hidden'
aria-hidden
>
<div className='absolute -top-12 left-8 w-48 h-24 bg-gradient-to-br from-brand-from/15 to-brand-to/10 dark:from-brand-from/15 dark:to-brand-to/10 blur-3xl rounded-full' />
</div>
<div className='relative flex items-center gap-2.5'>
<div className='relative'>
<div className='absolute inset-0 bg-gradient-to-br from-brand-from to-brand-to blur-md opacity-50 dark:opacity-40 rounded-lg' />
<div className='relative w-7 h-7 rounded-lg bg-gradient-to-br from-brand-from to-brand-to flex items-center justify-center shadow-sm'>
<IconComponent size={14} className='text-white' />
</div>
</div>
<h2 className='text-sm font-semibold text-gray-900 dark:text-gray-50 tracking-tight'>
{title}
</h2>
</div>
<div className='relative flex items-center gap-1'>
<a
href={config.fullPagePath}
target='_blank'
rel='noopener noreferrer'
className='group inline-flex items-center gap-1 px-2 py-1.5 text-xs font-medium text-gray-500 dark:text-gray-400 hover:text-brand-deep dark:hover:text-brand-from hover:bg-gray-100/80 dark:hover:bg-gray-800/80 rounded-md transition-colors'
title={t('在新标签页打开')}
>
<ExternalLink
size={12}
className='transition-transform group-hover:translate-x-0.5 group-hover:-translate-y-0.5'
/>
<span className='hidden sm:inline'>{t('完整页面')}</span>
</a>
<button
onClick={onClose}
className='p-1.5 rounded-md text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200 hover:bg-gray-100/80 dark:hover:bg-gray-800/80 transition-colors'
aria-label={t('关闭')}
>
<X size={16} />
</button>
</div>
</div>
{/* 内容区 */}
<div className='flex-1 min-h-0 bg-gray-50/30 dark:bg-gray-950/20'>
<DocumentRenderer
apiEndpoint={config.apiEndpoint}
title={title}
cacheKey={config.cacheKey}
emptyMessage={t(config.emptyKey)}
mode='embedded'
docType={type}
onProgressChange={handleProgressChange}
/>
</div>
</div>
</Modal>
);
};
export default AgreementModal;