'use client'; import { Dialog, DialogTitle, DialogContent, DialogActions, Button, TextField, Box, Typography, Select, MenuItem, FormControl, InputLabel, Slider, Card, CardContent, IconButton, CircularProgress } from '@mui/material'; import DeleteIcon from '@mui/icons-material/Delete'; import { useState, useEffect } from 'react'; import { useTranslation } from 'react-i18next'; /** * 评估集变体编辑对话框 */ export default function EvalVariantDialog({ open, onClose, onGenerate, onSave }) { const { t } = useTranslation(); const [step, setStep] = useState('config'); // 'config' | 'preview' const [loading, setLoading] = useState(false); const [config, setConfig] = useState({ questionType: 'open_ended', count: 1 }); const [items, setItems] = useState([]); // Reset state when dialog opens useEffect(() => { if (open) { setStep('config'); setConfig({ questionType: 'open_ended', count: 1 }); setItems([]); setLoading(false); } }, [open]); const handleGenerate = async () => { setLoading(true); try { const data = await onGenerate(config); // Ensure data is an array const newItems = Array.isArray(data) ? data : [data]; setItems(newItems); setStep('preview'); } catch (error) { console.error(error); } finally { setLoading(false); } }; const handleSave = () => { onSave(items); }; const handleItemChange = (index, field, value) => { const newItems = [...items]; newItems[index] = { ...newItems[index], [field]: value }; setItems(newItems); }; const handleDeleteItem = index => { const newItems = items.filter((_, i) => i !== index); setItems(newItems); if (newItems.length === 0) { setStep('config'); } }; const renderConfigStep = () => ( {t('datasets.evalVariantConfigHint', '请选择生成的题目类型和数量,AI 将基于当前问答对进行改写。')} {t('datasets.questionType', '题目类型')} {t('datasets.generateCount', '生成数量')}: {config.count} setConfig({ ...config, count: value })} step={1} marks min={1} max={5} valueLabelDisplay="auto" /> ); const renderPreviewStep = () => ( {t('datasets.evalVariantPreviewHint', '您可以编辑生成的题目,确认无误后保存到评估集。')} {items.map((item, index) => ( handleDeleteItem(index)} sx={{ position: 'absolute', right: 8, top: 8 }} > {t('datasets.questionIndex', '题目 {{index}}', { index: index + 1 })} handleItemChange(index, 'question', e.target.value)} size="small" /> {/* Render Options for choice questions */} {(item.options || config.questionType.includes('choice')) && ( { let val = e.target.value; try { // Try to parse if user inputs valid JSON, otherwise keep string const parsed = JSON.parse(val); if (Array.isArray(parsed)) val = parsed; } catch (e) {} handleItemChange(index, 'options', val); }} helperText={t('datasets.optionsHint', '例如: ["选项A", "选项B"]')} size="small" /> )} { let val = e.target.value; // For multiple choice, answer might be array if (config.questionType === 'multiple_choice') { try { const parsed = JSON.parse(val); if (Array.isArray(parsed)) val = parsed; } catch (e) {} } handleItemChange(index, 'correctAnswer', val); }} helperText={ config.questionType === 'multiple_choice' ? t('datasets.answerArrayHint', '多选题答案请输入数组,如 ["A", "C"]') : config.questionType === 'true_false' ? t('datasets.answerBoolHint', '判断题答案请输入 ✅ 或 ❌') : '' } size="small" /> ))} ); return ( {step === 'config' ? t('datasets.evalVariantTitle', '生成评估集变体') : t('datasets.evalVariantPreviewTitle', '确认生成的题目')} {step === 'config' ? renderConfigStep() : renderPreviewStep()} {step === 'config' ? ( ) : ( )} ); }