'use client'; import React, { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, TextField, Typography, Box, CircularProgress, Alert, List, ListItem, ListItemText, Paper, IconButton, Divider } from '@mui/material'; import CloseIcon from '@mui/icons-material/Close'; import axios from 'axios'; import i18n from '@/lib/i18n'; /** * 问题生成对话框组件 * @param {Object} props * @param {boolean} props.open - 对话框是否打开 * @param {Function} props.onClose - 关闭对话框的回调函数 * @param {Function} props.onGenerated - 问题生成完成的回调函数 * @param {string} props.projectId - 项目ID * @param {Object} props.tag - 标签对象 * @param {string} props.tagPath - 标签路径 * @param {Object} props.model - 选择的模型配置 */ export default function QuestionGenerationDialog({ open, onClose, onGenerated, projectId, tag, tagPath, model }) { const { t } = useTranslation(); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [count, setCount] = useState(5); const [generatedQuestions, setGeneratedQuestions] = useState([]); // 处理生成问题 const handleGenerateQuestions = async () => { try { setLoading(true); setError(''); const response = await axios.post(`/api/projects/${projectId}/distill/questions`, { tagPath, currentTag: tag.label, tagId: tag.id, count, model, language: i18n.language }); setGeneratedQuestions(response.data); } catch (error) { console.error('生成问题失败:', error); setError(error.response?.data?.error || t('distill.generateQuestionsError')); } finally { setLoading(false); } }; // 处理生成完成 const handleGenerateComplete = async () => { if (onGenerated) { onGenerated(generatedQuestions); } handleClose(); }; // 处理关闭对话框 const handleClose = () => { setGeneratedQuestions([]); setError(''); setCount(5); if (onClose) { onClose(); } }; // 处理数量变化 const handleCountChange = event => { const value = parseInt(event.target.value); if (!isNaN(value) && value >= 1 && value <= 100) { setCount(value); } }; return ( {t('distill.generateQuestionsTitle', { tag: tag?.label || t('distill.unknownTag') })} {error && ( {error} )} {t('distill.tagPath')}: {tagPath || tag?.label || t('distill.unknownTag')} {t('distill.questionCount')}: {generatedQuestions.length > 0 && ( {t('distill.generatedQuestions')}: {generatedQuestions.map((question, index) => ( {index > 0 && } ))} )} {generatedQuestions.length > 0 ? ( ) : ( )} ); }