import { PlusIcon, XIcon } from "lucide-react" import { Button } from "@/components/ui/button" import { cn } from "@/lib/utils" import { useWorkflowBranchSelection } from "./workflow-branch-selection" import { createConditionBranchID, normalizeNodeConfig, type WorkflowConditionBranch, } from "./workflow-utils" export function WorkflowConditionNodeContent({ configValue, nodeId, onChange, }: { configValue: Record | undefined nodeId: string onChange: (value: Record) => void }) { const { selectedBranch, onSelectBranch } = useWorkflowBranchSelection() const config = normalizeNodeConfig(configValue) const branches = ensureConditionBranches(config.branches ?? []) const updateBranches = (nextBranches: WorkflowConditionBranch[]) => { onChange({ ...config, branches: ensureConditionBranches(nextBranches), }) } const deleteBranch = (branchId: string) => { updateBranches(branches.filter((branch) => branch.id !== branchId)) if (selectedBranch?.nodeId === nodeId && selectedBranch.branchId === branchId) { onSelectBranch?.(null) } } return (
{branches.map((branch, index) => ( onSelectBranch?.({ nodeId, branchId: branch.id })} onDelete={() => deleteBranch(branch.id)} /> ))}
) } function WorkflowConditionBranchRow({ branch, index, selected, onSelect, onDelete, }: { branch: WorkflowConditionBranch index: number selected: boolean onSelect: () => void onDelete: () => void }) { const branchType = branch.default ? "else" : index === 0 ? "if" : "elseif" return (
{ event.stopPropagation() onSelect() }} onMouseDownCapture={(event) => { event.stopPropagation() }} onClick={(event) => { event.stopPropagation() }} > {branchType} {branch.name || (branch.default ? "默认分支" : branch.id)} {branch.default ? null : ( )}
) } function ensureConditionBranches(branches: WorkflowConditionBranch[]) { const normalized = branches.some((branch) => branch.default) ? branches : [...branches, { id: "default", name: "默认分支", targetNodeId: "", default: true }] return [ ...normalized.filter((branch) => !branch.default), ...normalized.filter((branch) => branch.default).slice(0, 1), ] }