feat: implement isConditionBranchEdge function and update deleteConditionBranch logic; add tests for condition branch line matching
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
import { PlusIcon, XIcon } from "lucide-react"
|
import { PlusIcon, XIcon } from "lucide-react"
|
||||||
|
import { useService, WorkflowLinesManager } from "@flowgram.ai/free-layout-editor"
|
||||||
|
|
||||||
import { Button } from "@/components/ui/button"
|
import { Button } from "@/components/ui/button"
|
||||||
import { cn } from "@/lib/utils"
|
import { cn } from "@/lib/utils"
|
||||||
import { useWorkflowBranchSelection } from "./workflow-branch-selection"
|
import { useWorkflowBranchSelection } from "./workflow-branch-selection"
|
||||||
import {
|
import {
|
||||||
createConditionBranchID,
|
createConditionBranchID,
|
||||||
|
isConditionBranchEdge,
|
||||||
isBranchRowActionTarget,
|
isBranchRowActionTarget,
|
||||||
normalizeNodeConfig,
|
normalizeNodeConfig,
|
||||||
type WorkflowConditionBranch,
|
type WorkflowConditionBranch,
|
||||||
@@ -19,6 +21,7 @@ export function WorkflowConditionNodeContent({
|
|||||||
nodeId: string
|
nodeId: string
|
||||||
onChange: (value: Record<string, unknown>) => void
|
onChange: (value: Record<string, unknown>) => void
|
||||||
}) {
|
}) {
|
||||||
|
const linesManager = useService(WorkflowLinesManager)
|
||||||
const { selectedBranch, onSelectBranch } = useWorkflowBranchSelection()
|
const { selectedBranch, onSelectBranch } = useWorkflowBranchSelection()
|
||||||
const config = normalizeNodeConfig(configValue)
|
const config = normalizeNodeConfig(configValue)
|
||||||
const branches = ensureConditionBranches(config.branches ?? [])
|
const branches = ensureConditionBranches(config.branches ?? [])
|
||||||
@@ -30,6 +33,11 @@ export function WorkflowConditionNodeContent({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
const deleteBranch = (branchId: string) => {
|
const deleteBranch = (branchId: string) => {
|
||||||
|
linesManager.getAllLines().forEach((line) => {
|
||||||
|
if (isConditionBranchEdge(line.toJSON(), nodeId, branchId)) {
|
||||||
|
line.dispose()
|
||||||
|
}
|
||||||
|
})
|
||||||
updateBranches(branches.filter((branch) => branch.id !== branchId))
|
updateBranches(branches.filter((branch) => branch.id !== branchId))
|
||||||
if (selectedBranch?.nodeId === nodeId && selectedBranch.branchId === branchId) {
|
if (selectedBranch?.nodeId === nodeId && selectedBranch.branchId === branchId) {
|
||||||
onSelectBranch?.(null)
|
onSelectBranch?.(null)
|
||||||
|
|||||||
@@ -381,6 +381,26 @@ describe("workflow definition mutations", () => {
|
|||||||
assert.deepEqual(plain(deleted.edges.map((edge) => edge.sourcePortID)), ["default"])
|
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 () => {
|
it("adds FlowGram source ports for condition edges without removing existing lines", async () => {
|
||||||
const { normalizeConditionPortsForFlowgram } = await loadModule()
|
const { normalizeConditionPortsForFlowgram } = await loadModule()
|
||||||
const definition = {
|
const definition = {
|
||||||
|
|||||||
@@ -297,10 +297,18 @@ export function deleteConditionBranch(
|
|||||||
})
|
})
|
||||||
return {
|
return {
|
||||||
...nextDefinition,
|
...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(
|
export function normalizeConditionPortsForFlowgram(
|
||||||
definition: AIWorkflowDefinition
|
definition: AIWorkflowDefinition
|
||||||
): AIWorkflowDefinition {
|
): AIWorkflowDefinition {
|
||||||
|
|||||||
Reference in New Issue
Block a user