Files
monkey-ai-web/src/components/common/modals/UserSurveyModal.jsx
T

339 lines
12 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, useEffect, useContext } from 'react';
import { Modal, RadioGroup, Radio, Button, Typography } from '@/components/ui';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import {
API,
clearUserData,
showSuccess,
showError,
showWarning,
} from '../../../helpers';
import {
Sparkles,
Briefcase,
Cpu,
ChevronRight,
UserCircle,
X,
LogOut,
} from 'lucide-react';
import { UserContext } from '../../../context/User';
const { Title, Text } = Typography;
const UserSurveyModal = ({ visible, onClose, onSuccess }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const [, userDispatch] = useContext(UserContext);
const [jobType, setJobType] = useState(null);
const [aiToolUsage, setAIToolUsage] = useState(null);
const [loading, setLoading] = useState(false);
const [showExitConfirm, setShowExitConfirm] = useState(false);
// 弹窗显示时重置状态
useEffect(() => {
if (visible) {
setJobType(null);
setAIToolUsage(null);
setLoading(false);
setShowExitConfirm(false);
}
}, [visible]);
// 处理关闭按钮点击 - 显示确认提示
const handleCloseClick = () => {
setShowExitConfirm(true);
};
// 确认退出 - 执行退出登录
const handleConfirmExit = async () => {
try {
await API.get('/api/user/logout');
showWarning(t('您已退出登录,填写问卷后方可使用平台'));
userDispatch({ type: 'logout' });
clearUserData();
onClose?.();
navigate('/org/login');
} catch (error) {
console.error('Logout failed:', error);
// 即使请求失败也清除本地状态
userDispatch({ type: 'logout' });
clearUserData();
onClose?.();
navigate('/org/login');
}
};
// 取消退出 - 返回填写问卷
const handleCancelExit = () => {
setShowExitConfirm(false);
};
const handleSubmit = async () => {
if (!jobType) {
showError(t('请选择您的职业类型'));
return;
}
if (!aiToolUsage) {
showError(t('请选择您的AI工具使用情况'));
return;
}
setLoading(true);
try {
const res = await API.post('/api/user/survey', {
job_type: jobType,
ai_tool_usage: aiToolUsage,
});
const { success, message } = res.data;
if (success) {
showSuccess(message || t('提交成功,感谢您的反馈!'));
onSuccess?.();
onClose?.();
} else {
showError(message || t('提交失败'));
}
} catch (error) {
showError(t('提交失败,请稍后重试'));
} finally {
setLoading(false);
}
};
const jobTypeOptions = [
{ value: 1, label: t('是,开发/测试/运维等技术岗位'), icon: '💻' },
{ value: 2, label: t('是,产品/设计/运营等技术周边岗位'), icon: '🎨' },
{ value: 3, label: t('否,其他行业'), icon: '🏢' },
];
const aiToolOptions = [
{ value: 1, label: t('是,正在使用'), icon: '🚀' },
{ value: 2, label: t('是,了解或尝试过'), icon: '🔍' },
{ value: 3, label: t('否,但有兴趣尝试'), icon: '✨' },
{ value: 4, label: t('否,暂不需要'), icon: '💤' },
];
return (
<Modal
visible={visible}
closable={false}
maskClosable={false}
footer={null}
width={520}
className='user-survey-modal'
bodyStyle={{ padding: 0 }}
>
<div className='relative overflow-hidden'>
{/* 关闭按钮 */}
<button
onClick={handleCloseClick}
className='absolute top-4 right-4 z-10 p-2 rounded-full bg-gray-100 hover:bg-gray-200 dark:bg-gray-700 dark:hover:bg-gray-600 transition-colors duration-200 group'
title={t('暂不填写')}
>
<X
size={18}
className='text-gray-500 dark:text-gray-400 group-hover:text-gray-700 dark:group-hover:text-gray-200'
/>
</button>
{/* 背景装饰 */}
<div className='absolute inset-0 pointer-events-none overflow-hidden'>
<div className='absolute -top-20 -right-20 w-40 h-40 bg-gradient-to-br from-brand-from/20 to-brand-to/20 rounded-full blur-3xl' />
<div className='absolute -bottom-20 -left-20 w-40 h-40 bg-gradient-to-br from-pink-500/15 to-rose-500/15 rounded-full blur-3xl' />
</div>
{/* 确认退出弹层 */}
{showExitConfirm && (
<div className='absolute inset-0 z-20 bg-white/95 dark:bg-gray-900/95 backdrop-blur-sm flex flex-col items-center justify-center px-8'>
<div className='p-4 bg-gradient-to-br from-amber-100 to-orange-100 dark:from-amber-900/30 dark:to-orange-900/30 rounded-full mb-6'>
<LogOut
size={32}
className='text-amber-600 dark:text-amber-400'
/>
</div>
<h3 className='text-xl font-semibold text-gray-900 dark:text-white mb-3'>
{t('确认退出?')}
</h3>
<p className='text-gray-600 dark:text-gray-400 text-center mb-6 max-w-sm'>
{t('不填写问卷将无法使用平台功能,系统将自动退出登录。')}
</p>
<div className='flex gap-3 w-full max-w-xs'>
<Button
theme='light'
size='large'
onClick={handleCancelExit}
className='flex-1 !rounded-xl !h-11'
>
{t('继续填写')}
</Button>
<Button
theme='solid'
type='danger'
size='large'
onClick={handleConfirmExit}
className='flex-1 !rounded-xl !h-11'
>
{t('确认退出')}
</Button>
</div>
</div>
)}
{/* 头部 */}
<div className='relative px-8 pt-8 pb-6'>
<div className='flex items-center gap-4 mb-4'>
<div className='relative'>
<div className='p-3.5 bg-gradient-to-br from-brand-from to-brand-to rounded-2xl shadow-lg shadow-brand/30'>
<UserCircle size={28} className='text-white' />
</div>
<div className='absolute -top-1 -right-1 w-5 h-5 bg-gradient-to-r from-amber-400 to-orange-500 rounded-full flex items-center justify-center shadow-lg'>
<Sparkles size={12} className='text-white' />
</div>
</div>
<div>
<Title
heading={4}
className='!mb-1 !text-gray-900 dark:!text-white'
>
{t('欢迎加入!')}
</Title>
<Text className='!text-gray-500 dark:!text-gray-400 text-sm'>
{t('帮助我们更好地了解您')}
</Text>
</div>
</div>
<div className='p-3 bg-gradient-to-r from-brand-from/5 to-brand-to/5 dark:from-brand-from/20 dark:to-brand-to/20 rounded-xl border border-brand/10 dark:border-brand-deep/50'>
<Text className='!text-brand-deep dark:!text-brand-from text-sm'>
💡 {t('回答两个小问题,让我们为您提供更适合的服务体验')}
</Text>
</div>
<div className='mt-3 flex items-center gap-2 text-xs text-gray-400 dark:text-gray-500'>
<span>✓</span>
<span>{t('仅需填写一次,之后不会再弹出')}</span>
</div>
</div>
{/* 问题区域 */}
<div className='relative px-8 pb-6 space-y-6'>
{/* 问题1 */}
<div className='space-y-3'>
<div className='flex items-center gap-2'>
<div className='w-6 h-6 rounded-full bg-brand/10 dark:bg-brand-deep/50 flex items-center justify-center'>
<Briefcase
size={14}
className='text-brand-deep dark:text-brand-from'
/>
</div>
<Text strong className='!text-gray-800 dark:!text-gray-200'>
{t('您是IT相关从业者吗?')}
</Text>
</div>
<RadioGroup
value={jobType}
onChange={(e) => setJobType(e.target.value)}
direction='vertical'
className='survey-radio-group'
>
{jobTypeOptions.map((option) => (
<Radio
key={option.value}
value={option.value}
className={`survey-radio-item ${jobType === option.value ? 'selected' : ''}`}
>
<div className='flex items-center gap-2'>
<span className='text-lg'>{option.icon}</span>
<span className='text-gray-700 dark:text-gray-300'>
{option.label}
</span>
</div>
</Radio>
))}
</RadioGroup>
</div>
{/* 问题2 */}
<div className='space-y-3'>
<div className='flex items-center gap-2'>
<div className='w-6 h-6 rounded-full bg-brand/10 dark:bg-brand-deep/50 flex items-center justify-center'>
<Cpu
size={14}
className='text-brand-deep dark:text-brand-from'
/>
</div>
<Text strong className='!text-gray-800 dark:!text-gray-200'>
{t('您使用过AI编程工具吗?')}
</Text>
<Text className='!text-gray-400 dark:!text-gray-500 text-xs'>
{t('(如Cursor/Claude/Copilot等)')}
</Text>
</div>
<RadioGroup
value={aiToolUsage}
onChange={(e) => setAIToolUsage(e.target.value)}
direction='vertical'
className='survey-radio-group'
>
{aiToolOptions.map((option) => (
<Radio
key={option.value}
value={option.value}
className={`survey-radio-item ${aiToolUsage === option.value ? 'selected' : ''}`}
>
<div className='flex items-center gap-2'>
<span className='text-lg'>{option.icon}</span>
<span className='text-gray-700 dark:text-gray-300'>
{option.label}
</span>
</div>
</Radio>
))}
</RadioGroup>
</div>
</div>
{/* 底部按钮 */}
<div className='relative px-8 py-5 border-t border-gray-100 dark:border-gray-800 bg-gray-50/50 dark:bg-gray-900/50'>
<Button
theme='solid'
type='primary'
size='large'
loading={loading}
onClick={handleSubmit}
disabled={!jobType || !aiToolUsage}
className='w-full !bg-gradient-to-r !from-brand-from !to-brand-to hover:!from-brand-from hover:!to-brand-to !border-0 !rounded-xl !h-12 !font-medium !shadow-lg !shadow-brand/25 transition-all duration-200 hover:!shadow-xl hover:!shadow-brand/30 disabled:!opacity-50 disabled:!cursor-not-allowed'
>
<span className='flex items-center justify-center gap-2'>
{t('开始使用')}
<ChevronRight size={18} />
</span>
</Button>
</div>
</div>
</Modal>
);
};
export default UserSurveyModal;