'use client'; import { useState, useEffect, useMemo } from 'react'; import { X } from 'lucide-react'; import type { Requirement, Effort, DictItem, SourceType, SourceTarget } from '@/lib/requirement'; import type { Priority } from '@/lib/derive'; import { EFFORT_LABEL, SOURCE_TYPE_LABEL, SOURCE_TARGET_LABEL } from '@/lib/requirement'; interface RequirementModalProps { open: boolean; initial?: Requirement | null; products: { id: string; name: string }[]; projects: { id: string; name: string; productId: string }[]; versions: { id: string; name: string; projectId: string }[]; sourceTargets: SourceTarget[]; types: DictItem[]; platforms: DictItem[]; requirements: Requirement[]; onClose: () => void; onSubmit: (data: any) => void; onOpenDrawer: (type: 'source' | 'type' | 'platform') => void; } const PRIORITIES: Priority[] = ['P0', 'P1', 'P2', 'P3', 'P4']; const EFFORTS: Effort[] = ['S', 'M', 'L', 'XL']; const SOURCE_TYPES: SourceType[] = ['customer', 'internal', 'operation', 'aftersale', 'market', 'competitor', 'management']; const PRIORITY_COLORS: Record = { P0: 'bg-red-500 text-white', P1: 'bg-orange-500 text-white', P2: 'bg-blue-500 text-white', P3: 'bg-zinc-500 text-white', P4: 'bg-zinc-400 text-white', }; export function RequirementModal({ open, initial, products, projects, versions, sourceTargets, types, platforms, requirements, onClose, onSubmit, onOpenDrawer, }: RequirementModalProps) { const [title, setTitle] = useState(''); const [description, setDescription] = useState(''); const [sourceType, setSourceType] = useState('customer'); const [sourceTarget, setSourceTarget] = useState(''); const [productId, setProductId] = useState(''); const [projectId, setProjectId] = useState(''); const [selectedPlatforms, setSelectedPlatforms] = useState([]); const [typeId, setTypeId] = useState(''); const [priority, setPriority] = useState('P2'); const [effort, setEffort] = useState('M'); const [productOwner, setProductOwner] = useState(''); const [parentId, setParentId] = useState(''); const [versionId, setVersionId] = useState(''); const [platformDropOpen, setPlatformDropOpen] = useState(false); const filteredProjects = useMemo( () => productId ? projects.filter((p) => p.productId === productId) : projects, [projects, productId], ); const filteredVersions = useMemo( () => projectId ? versions.filter((v) => v.projectId === projectId) : [], [versions, projectId], ); useEffect(() => { if (initial) { setTitle(initial.title); setDescription(initial.description || ''); setSourceType(initial.sourceType || 'customer'); setSourceTarget(initial.sourceTarget || ''); setProductId(initial.productId || ''); setProjectId(initial.projectId || ''); setVersionId(initial.versionId || ''); setSelectedPlatforms(initial.platforms || []); setTypeId(initial.typeId || ''); setPriority(initial.priority || 'P2'); setEffort(initial.effort || 'M'); setProductOwner(initial.productOwner || ''); setParentId(initial.parentId || ''); } else { setTitle(''); setDescription(''); setSourceType('customer'); setSourceTarget(''); setProductId(''); setProjectId(''); setVersionId(''); setSelectedPlatforms([]); setTypeId(''); setPriority('P2'); setEffort('M'); setProductOwner(''); setParentId(''); } }, [initial, open]); if (!open) return null; const filteredTargets = sourceTargets.filter(t => t.sourceType === sourceType); const togglePlatform = (id: string) => { setSelectedPlatforms((prev) => prev.includes(id) ? prev.filter((p) => p !== id) : [...prev, id] ); }; const handleSubmit = () => { if (!title.trim()) return; onSubmit({ title: title.trim(), description: description.trim(), sourceType, sourceTarget: sourceTarget || undefined, productId: productId || undefined, projectId: projectId || undefined, versionId: versionId || undefined, platforms: selectedPlatforms, typeId: typeId || undefined, priority, productOwner: productOwner.trim() || undefined, parentId: parentId || undefined, }); }; const platformDisplay = selectedPlatforms.length > 0 ? platforms .filter((p) => selectedPlatforms.includes(p.id)) .map((p) => p.name) .join(', ') : '请选择'; return (
e.stopPropagation()} > {/* Header */}

{initial ? '编辑需求' : '新建需求'}

{/* 1. 需求概述 */}
setTitle(e.target.value)} placeholder="请输入需求标题" className="h-9 w-full rounded-lg border border-[var(--line)] bg-[var(--bg-card)] px-3 text-[13px] text-[var(--ink)] placeholder:text-[var(--ink-muted)] outline-none focus:border-[var(--accent)]" />
{/* 2. 需求描述 */}