import React, { useState, useEffect } from 'react'; import { PencilIcon, CheckIcon, XMarkIcon } from '@heroicons/react/24/outline'; import { SegmentMatchingRule, SegmentMatchingRuleHelper } from '../../types/template'; import { AiClassification } from '../../types/aiClassification'; import { useTemplateStore } from '../../stores/templateStore'; import { CustomSelect } from '../CustomSelect'; import { AiClassificationService } from '../../services/aiClassificationService'; interface SegmentMatchingRuleEditorProps { segmentId: string; currentRule: SegmentMatchingRule; onRuleUpdated?: (newRule: SegmentMatchingRule) => void; } /** * 片段匹配规则编辑器组件 * 遵循前端开发规范的组件设计,支持固定素材和AI分类规则的设置 */ export const SegmentMatchingRuleEditor: React.FC = ({ segmentId, currentRule, onRuleUpdated, }) => { const [isEditing, setIsEditing] = useState(false); const [editingRule, setEditingRule] = useState(currentRule); const [aiClassifications, setAiClassifications] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const { updateSegmentMatchingRule } = useTemplateStore(); // 加载AI分类列表 useEffect(() => { const loadClassifications = async () => { try { const classifications = await AiClassificationService.getActiveClassifications(); setAiClassifications(classifications); } catch (error) { console.error('加载AI分类失败:', error); } }; if (isEditing) { loadClassifications(); } }, [isEditing]); // 重置编辑状态 useEffect(() => { setEditingRule(currentRule); setError(null); }, [currentRule, isEditing]); const handleStartEdit = () => { setIsEditing(true); setEditingRule(currentRule); setError(null); }; const handleCancelEdit = () => { setIsEditing(false); setEditingRule(currentRule); setError(null); }; const handleSaveRule = async () => { try { setLoading(true); setError(null); await updateSegmentMatchingRule(segmentId, editingRule); setIsEditing(false); onRuleUpdated?.(editingRule); } catch (error) { const errorMessage = error instanceof Error ? error.message : '保存匹配规则失败'; setError(errorMessage); } finally { setLoading(false); } }; const handleRuleTypeChange = (ruleType: string) => { if (ruleType === 'fixed') { setEditingRule(SegmentMatchingRuleHelper.createFixedMaterial()); } else if (ruleType === 'ai_classification') { // 如果有可用的AI分类,选择第一个作为默认值 if (aiClassifications.length > 0) { const firstClassification = aiClassifications[0]; setEditingRule(SegmentMatchingRuleHelper.createAiClassification( firstClassification.id, firstClassification.name )); } } else if (ruleType === 'random') { setEditingRule(SegmentMatchingRuleHelper.createRandomMatch()); } else if (ruleType === 'priority_order') { // 默认选择所有激活的AI分类 const categoryIds = aiClassifications.map(c => c.id); setEditingRule(SegmentMatchingRuleHelper.createPriorityOrder(categoryIds)); } }; const handleAiClassificationChange = (classificationId: string) => { const classification = aiClassifications.find(c => c.id === classificationId); if (classification) { setEditingRule(SegmentMatchingRuleHelper.createAiClassification( classification.id, classification.name )); } }; const handlePriorityOrderChange = (categoryId: string, isSelected: boolean) => { if (typeof editingRule === 'object' && 'PriorityOrder' in editingRule) { const currentCategoryIds = editingRule.PriorityOrder.category_ids; let newCategoryIds: string[]; if (isSelected) { // 添加分类ID newCategoryIds = [...currentCategoryIds, categoryId]; } else { // 移除分类ID newCategoryIds = currentCategoryIds.filter(id => id !== categoryId); } setEditingRule(SegmentMatchingRuleHelper.createPriorityOrder(newCategoryIds)); } }; const getCurrentRuleType = (rule: SegmentMatchingRule): string => { if (SegmentMatchingRuleHelper.isFixedMaterial(rule)) { return 'fixed'; } else if (SegmentMatchingRuleHelper.isAiClassification(rule)) { return 'ai_classification'; } else if (SegmentMatchingRuleHelper.isRandomMatch(rule)) { return 'random'; } else if (SegmentMatchingRuleHelper.isPriorityOrder(rule)) { return 'priority_order'; } return 'fixed'; // 默认值 }; const ruleTypeOptions = [ { value: 'fixed', label: '固定素材' }, { value: 'ai_classification', label: 'AI分类素材' }, { value: 'random', label: '随机匹配' }, { value: 'priority_order', label: '按顺序匹配' }, ]; const classificationOptions = aiClassifications.map(classification => ({ value: classification.id, label: classification.name, })); if (!isEditing) { return (
匹配规则: {SegmentMatchingRuleHelper.getDisplayName(currentRule)}
); } return (
编辑匹配规则:
{SegmentMatchingRuleHelper.isAiClassification(editingRule) && (
)} {SegmentMatchingRuleHelper.isPriorityOrder(editingRule) && (
系统将按照AI分类的权重顺序依次尝试匹配素材,权重高的分类优先匹配
{aiClassifications .sort((a, b) => (b.weight || 0) - (a.weight || 0)) // 按权重降序排列 .map((classification) => { const currentCategoryIds = typeof editingRule === 'object' && 'PriorityOrder' in editingRule ? editingRule.PriorityOrder.category_ids : []; const isSelected = currentCategoryIds.includes(classification.id); return ( ); })}
{typeof editingRule === 'object' && 'PriorityOrder' in editingRule && editingRule.PriorityOrder.category_ids.length === 0 && (
⚠️ 请至少选择一个AI分类
)}
)}
{error && (
⚠️ {error}
)} {loading && (
正在保存匹配规则...
)}
); };