'use client'; import { Box, Typography, TextField, Chip, Button, Paper } from '@mui/material'; import { useTranslation } from 'react-i18next'; import { useState } from 'react'; import AddIcon from '@mui/icons-material/Add'; import AIGenerateButton from './AIGenerateButton'; export default function AnswerInput({ answerType, answer, onAnswerChange, labels, customFormat, projectId, imageName, question }) { const { t, i18n } = useTranslation(); const [newLabel, setNewLabel] = useState(''); const [jsonError, setJsonError] = useState(''); // 文字类型输入 if (answerType === 'text') { return ( {t('images.answer', { defaultValue: '文本答案' })} * onAnswerChange(e.target.value)} placeholder={t('images.answerPlaceholder', { defaultValue: '请输入答案...' })} sx={{ '& .MuiOutlinedInput-root': { borderRadius: 3, backgroundColor: 'background.paper', '& fieldset': { borderWidth: 2, borderColor: 'divider' }, '&:hover fieldset': { borderColor: 'primary.main' }, '&.Mui-focused fieldset': { borderColor: 'primary.main', borderWidth: 2 } }, '& textarea': { fontSize: '14px', lineHeight: 1.6 } }} /> ); } // 标签类型输入 - 提前解析 labels,避免条件中的 hooks 问题 if (answerType === 'label') { const selectedLabels = Array.isArray(answer) ? answer : []; // 解析 labels(可能是 JSON 字符串或数组) let labelOptions = []; if (typeof labels === 'string' && labels) { try { labelOptions = JSON.parse(labels); } catch (e) { labelOptions = []; } } else if (Array.isArray(labels)) { labelOptions = labels; } if (!labelOptions.includes('其他') && !labelOptions.includes('other')) { labelOptions.push(i18n.language === 'en' ? 'other' : '其他'); } const handleToggleLabel = label => { if (selectedLabels.includes(label)) { onAnswerChange(selectedLabels.filter(l => l !== label)); } else { let newLabels = [...selectedLabels, label]; onAnswerChange(newLabels); } }; const handleAddNewLabel = () => { if (newLabel.trim() && !labelOptions.includes(newLabel.trim())) { handleToggleLabel(newLabel.trim()); setNewLabel(''); } }; return ( {t('images.selectLabels', { defaultValue: '标签选择' })} * {/* 可选标签 */} {t('images.availableLabels', { defaultValue: '可选标签' })} {labelOptions && labelOptions.length > 0 ? ( labelOptions.map(label => ( handleToggleLabel(label)} color={selectedLabels.includes(label) ? 'primary' : 'default'} variant={selectedLabels.includes(label) ? 'filled' : 'outlined'} sx={{ borderRadius: 2, fontWeight: 500, fontSize: '0.875rem', height: 36, cursor: 'pointer', transition: 'all 0.2s ease', '&:hover': { transform: 'translateY(-1px)', boxShadow: 2 } }} /> )) ) : ( {t('images.noLabelsAvailable', { defaultValue: '暂无可选标签' })} )} {/* 添加新标签 */} {/* setNewLabel(e.target.value)} placeholder={t('images.addNewLabel', { defaultValue: '添加新标签...' })} onKeyPress={e => { if (e.key === 'Enter') { handleAddNewLabel(); } }} sx={{ flex: 1, '& .MuiOutlinedInput-root': { borderRadius: 2, backgroundColor: 'background.paper', '& fieldset': { borderWidth: 2 }, '&:hover fieldset': { borderColor: 'primary.main' } } }} /> } onClick={handleAddNewLabel} disabled={!newLabel.trim()} variant="contained" sx={{ borderRadius: 2, px: 3, fontWeight: 600, textTransform: 'none', boxShadow: 2, '&:hover': { boxShadow: 4 } }} > {t('common.add', { defaultValue: '添加' })} */} {/* 已选择标签 */} {/* {selectedLabels.length > 0 && ( {t('images.selectedLabels', { defaultValue: '已选择' })} ({selectedLabels.length}) {selectedLabels.map(label => ( handleToggleLabel(label)} color="primary" sx={{ borderRadius: 2, fontWeight: 500, fontSize: '0.875rem', height: 36, '& .MuiChip-deleteIcon': { fontSize: '18px', '&:hover': { color: 'error.main' } } }} /> ))} )} */} ); } // 自定义格式输入 if (answerType === 'custom_format') { const handleJsonChange = value => { onAnswerChange(value); // 验证 JSON 格式 if (value.trim()) { try { JSON.parse(value); setJsonError(''); } catch (e) { setJsonError(t('images.invalidJsonFormat', { defaultValue: 'JSON 格式不正确' })); } } else { setJsonError(''); } }; const handleUseTemplate = () => { if (customFormat) { try { let templateJson; if (typeof customFormat === 'string') { templateJson = JSON.parse(customFormat); } else { templateJson = customFormat; } const formatted = JSON.stringify(templateJson, null, 2); onAnswerChange(formatted); setJsonError(''); } catch (e) { onAnswerChange('{}'); } } }; if (answer && typeof answer === 'object') { answer = JSON.stringify(answer, null, 2); } return ( {t('images.customFormatAnswer', { defaultValue: '自定义格式答案' })} * {customFormat && ( {t('images.useTemplate', { defaultValue: '使用模板' })} )} {/* {t('images.formatJson', { defaultValue: '格式化' })} */} {/* 显示格式要求 */} {customFormat && ( {t('images.formatRequirement', { defaultValue: '格式要求' })} {typeof customFormat === 'string' ? customFormat : JSON.stringify(customFormat, null, 2)} )} {/* JSON 输入框 */} handleJsonChange(e.target.value)} placeholder={t('images.customFormatPlaceholder', { defaultValue: '请输入符合格式的 JSON...' })} error={!!jsonError} sx={{ '& .MuiOutlinedInput-root': { borderRadius: 3, backgroundColor: 'background.paper', '& fieldset': { borderWidth: 2 }, '&:hover fieldset': { borderColor: 'primary.main' }, '&.Mui-focused fieldset': { borderColor: 'primary.main', borderWidth: 2 }, '&.Mui-error fieldset': { borderColor: 'error.main', borderWidth: 2 } }, '& textarea': { fontFamily: 'Monaco, Consolas, "Courier New", monospace', fontSize: '13px', lineHeight: 1.5 }, '& .MuiFormHelperText-root': { fontSize: '0.875rem', fontWeight: 500 } }} /> ); } return null; }
{typeof customFormat === 'string' ? customFormat : JSON.stringify(customFormat, null, 2)}