Feat: Connect conditional operators to other operators #3221 (#8231)

### What problem does this PR solve?

Feat: Connect conditional operators to other operators #3221

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2025-06-13 09:30:34 +08:00
committed by GitHub
parent 99725444f1
commit a9d9215547
11 changed files with 156 additions and 98 deletions

View File

@ -56,6 +56,7 @@ export type RFState = {
source: string,
sourceHandle?: string | null,
target?: string | null,
isConnecting?: boolean,
) => void;
deletePreviousEdgeOfClassificationNode: (connection: Connection) => void;
duplicateNode: (id: string, name: string) => void;
@ -204,7 +205,7 @@ const useGraphStore = create<RFState>()(
]);
break;
case Operator.Switch: {
updateSwitchFormData(source, sourceHandle, target);
updateSwitchFormData(source, sourceHandle, target, true);
break;
}
default:
@ -219,7 +220,7 @@ const useGraphStore = create<RFState>()(
const anchoredNodes = [
Operator.Categorize,
Operator.Relevant,
Operator.Switch,
// Operator.Switch,
];
if (
anchoredNodes.some(
@ -303,7 +304,7 @@ const useGraphStore = create<RFState>()(
const currentEdge = edges.find((x) => x.id === id);
if (currentEdge) {
const { source, sourceHandle } = currentEdge;
const { source, sourceHandle, target } = currentEdge;
const operatorType = getOperatorTypeFromId(source);
// After deleting the edge, set the corresponding field in the node's form field to undefined
switch (operatorType) {
@ -321,7 +322,7 @@ const useGraphStore = create<RFState>()(
]);
break;
case Operator.Switch: {
updateSwitchFormData(source, sourceHandle, undefined);
updateSwitchFormData(source, sourceHandle, target, false);
break;
}
default:
@ -402,15 +403,32 @@ const useGraphStore = create<RFState>()(
return nextNodes;
},
updateSwitchFormData: (source, sourceHandle, target) => {
const { updateNodeForm } = get();
updateSwitchFormData: (source, sourceHandle, target, isConnecting) => {
const { updateNodeForm, edges } = get();
if (sourceHandle) {
// A handle will connect to multiple downstream nodes
let currentHandleTargets = edges
.filter(
(x) =>
x.source === source &&
x.sourceHandle === sourceHandle &&
typeof x.target === 'string',
)
.map((x) => x.target);
let targets: string[] = currentHandleTargets;
if (target) {
if (!isConnecting) {
targets = currentHandleTargets.filter((x) => x !== target);
}
}
if (sourceHandle === SwitchElseTo) {
updateNodeForm(source, target, [SwitchElseTo]);
updateNodeForm(source, targets, [SwitchElseTo]);
} else {
const operatorIndex = getOperatorIndex(sourceHandle);
if (operatorIndex) {
updateNodeForm(source, target, [
updateNodeForm(source, targets, [
'conditions',
Number(operatorIndex) - 1, // The index is the conditions form index
'to',
@ -448,7 +466,7 @@ const useGraphStore = create<RFState>()(
return generateNodeNamesWithIncreasingIndex(name, nodes);
},
})),
{ name: 'graph' },
{ name: 'graph', trace: true },
),
);