feat: implement isConditionBranchEdge function and update deleteConditionBranch logic; add tests for condition branch line matching

This commit is contained in:
mlogclub
2026-06-30 12:36:17 +08:00
parent 79f0c06790
commit d7782ba6c8
3 changed files with 37 additions and 1 deletions
@@ -1,10 +1,12 @@
import { PlusIcon, XIcon } from "lucide-react"
import { useService, WorkflowLinesManager } from "@flowgram.ai/free-layout-editor"
import { Button } from "@/components/ui/button"
import { cn } from "@/lib/utils"
import { useWorkflowBranchSelection } from "./workflow-branch-selection"
import {
createConditionBranchID,
isConditionBranchEdge,
isBranchRowActionTarget,
normalizeNodeConfig,
type WorkflowConditionBranch,
@@ -19,6 +21,7 @@ export function WorkflowConditionNodeContent({
nodeId: string
onChange: (value: Record<string, unknown>) => void
}) {
const linesManager = useService(WorkflowLinesManager)
const { selectedBranch, onSelectBranch } = useWorkflowBranchSelection()
const config = normalizeNodeConfig(configValue)
const branches = ensureConditionBranches(config.branches ?? [])
@@ -30,6 +33,11 @@ export function WorkflowConditionNodeContent({
})
}
const deleteBranch = (branchId: string) => {
linesManager.getAllLines().forEach((line) => {
if (isConditionBranchEdge(line.toJSON(), nodeId, branchId)) {
line.dispose()
}
})
updateBranches(branches.filter((branch) => branch.id !== branchId))
if (selectedBranch?.nodeId === nodeId && selectedBranch.branchId === branchId) {
onSelectBranch?.(null)
@@ -381,6 +381,26 @@ describe("workflow definition mutations", () => {
assert.deepEqual(plain(deleted.edges.map((edge) => edge.sourcePortID)), ["default"])
})
it("matches condition branch lines by source node and branch port", async () => {
const { isConditionBranchEdge } = await loadModule()
assert.equal(isConditionBranchEdge(
workflowEdge("condition_1", "vip_reply", { sourcePortID: "vip" }),
"condition_1",
"vip"
), true)
assert.equal(isConditionBranchEdge(
workflowEdge("condition_1", "default_reply", { sourcePortID: "default" }),
"condition_1",
"vip"
), false)
assert.equal(isConditionBranchEdge(
workflowEdge("other_condition", "vip_reply", { sourcePortID: "vip" }),
"condition_1",
"vip"
), false)
})
it("adds FlowGram source ports for condition edges without removing existing lines", async () => {
const { normalizeConditionPortsForFlowgram } = await loadModule()
const definition = {
@@ -297,10 +297,18 @@ export function deleteConditionBranch(
})
return {
...nextDefinition,
edges: nextDefinition.edges.filter((edge) => !(edge.sourceNodeID === nodeId && edge.sourcePortID === branchId)),
edges: nextDefinition.edges.filter((edge) => !isConditionBranchEdge(edge, nodeId, branchId)),
}
}
export function isConditionBranchEdge(
edge: { sourceNodeID?: unknown; sourcePortID?: unknown },
nodeId: string,
branchId: string
): boolean {
return String(edge.sourceNodeID ?? "") === nodeId && String(edge.sourcePortID ?? "") === branchId
}
export function normalizeConditionPortsForFlowgram(
definition: AIWorkflowDefinition
): AIWorkflowDefinition {