import React, { useState, useMemo } from 'react'; import { EyeIcon, EyeSlashIcon } from '@heroicons/react/24/outline'; import { AiClassification, AiClassificationFormData } from '../types/aiClassification'; interface AiClassificationRealTimePreviewProps { /** 当前表单数据 */ currentFormData: AiClassificationFormData; /** 现有分类列表 */ existingClassifications: AiClassification[]; /** 是否为编辑模式 */ isEdit?: boolean; /** 编辑中的分类ID */ editingClassificationId?: string; } /** * AI分类实时预览组件 * 遵循前端开发规范的预览组件设计,实现分类修改时的提示词实时预览 */ export const AiClassificationRealTimePreview: React.FC = ({ currentFormData, existingClassifications, isEdit = false, editingClassificationId, }) => { const [isExpanded, setIsExpanded] = useState(false); // 生成预览用的分类列表 const previewClassifications = useMemo(() => { let classifications = [...existingClassifications]; if (isEdit && editingClassificationId) { // 编辑模式:替换现有分类 const index = classifications.findIndex(c => c.id === editingClassificationId); if (index !== -1) { classifications[index] = { ...classifications[index], name: currentFormData.name, prompt_text: currentFormData.prompt_text, description: currentFormData.description || undefined, sort_order: currentFormData.sort_order, }; } } else { // 创建模式:添加新分类 if (currentFormData.name.trim() && currentFormData.prompt_text.trim()) { const newClassification: AiClassification = { id: 'preview-new', name: currentFormData.name, prompt_text: currentFormData.prompt_text, description: currentFormData.description || undefined, is_active: true, sort_order: currentFormData.sort_order, weight: currentFormData.weight, created_at: new Date().toISOString(), updated_at: new Date().toISOString(), }; classifications.push(newClassification); } } // 只返回激活的分类,并按排序顺序排列 return classifications .filter(c => c.is_active) .sort((a, b) => a.sort_order - b.sort_order); }, [currentFormData, existingClassifications, isEdit, editingClassificationId]); // 生成完整提示词 const fullPrompt = useMemo(() => { if (previewClassifications.length === 0) { return '暂无激活的分类,无法生成提示词'; } // 生成分类名称列表 const categoryNames = previewClassifications .map(c => c.name) .join('\n - '); // 生成详细分类描述 const categoriesStr = previewClassifications .map(c => `**${c.name}**: ${c.prompt_text}`) .join('\n - '); return `请分析这个视频的内容,并将其分类到以下类别之一: - ${categoryNames} 请按以下步骤进行分析: 1. **冲突处理**: - 按最大可见面积分类 2. **内容分类**(仅对包含目标商品且质量合格的视频): - ${categoriesStr} 请返回JSON格式的结果: { "category": "分类结果", "confidence": 0.85, "reasoning": "详细的分类理由,包括商品匹配情况和内容特征", "features": ["观察到的关键特征1", "关键特征2", "关键特征3"], "product_match": true/false, "quality_score": 0.9 } **分类优先级**: 1. 商品匹配 > 内容分类 2. 质量合格 > 内容丰富 3. 明确分类 > 模糊归类 请仔细观察视频内容,确保分类准确性。`; }, [previewClassifications]); // 检查是否有变化 const hasChanges = useMemo(() => { return currentFormData.name.trim() !== '' || currentFormData.prompt_text.trim() !== ''; }, [currentFormData]); return (
{/* 预览标题栏 */}
setIsExpanded(!isExpanded)} >
{isExpanded ? ( ) : ( )}

实时预览

{hasChanges && ( 有变化 )}
{previewClassifications.length} 个激活分类
{/* 预览内容 */} {isExpanded && (
{/* 分类列表预览 */}
激活的分类 ({previewClassifications.length})
{previewClassifications.map((classification, index) => (
{index + 1}. {classification.name} {classification.id === 'preview-new' && ( (新增) )} {classification.id === editingClassificationId && ( (编辑中) )}
{classification.prompt_text}
))}
{/* 完整提示词预览 */}
完整提示词预览
                {fullPrompt}
              
字符数: {fullPrompt.length}
{/* 提示信息 */}
提示:
  • • 预览会实时反映您的修改
  • • 只有激活状态的分类会包含在提示词中
  • • 分类按排序顺序显示
  • • 保存后预览将成为实际的提示词
)}
); };