From 0b7368e23d6e6a01cf878049e231730114f6d35a Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Thu, 6 Feb 2025 17:34:22 -0300 Subject: [PATCH] refactor: prevent disconnected handles from executing when input template edge is hidden (#6132) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ (reactflowUtils.ts): add new function filterHiddenFieldsEdges to filter out edges based on hidden fields in node templates * ♻️ (reactflowUtils.ts): refactor filterHiddenFieldsEdges function to accept targetNode directly instead of nodes array to improve code readability and maintainability * 🐛 (reactflowUtils.ts): fix a bug where nodeInputType is accessed without null check, causing potential runtime error --------- Co-authored-by: anovazzi1 --- src/frontend/src/utils/reactflowUtils.ts | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/frontend/src/utils/reactflowUtils.ts b/src/frontend/src/utils/reactflowUtils.ts index 864b24c8a..ee779e8dc 100644 --- a/src/frontend/src/utils/reactflowUtils.ts +++ b/src/frontend/src/utils/reactflowUtils.ts @@ -133,10 +133,36 @@ export function cleanEdges(nodes: AllNodeType[], edges: EdgeType[]) { } } } + + newEdges = filterHiddenFieldsEdges(edge, newEdges, targetNode); }); return newEdges; } +export function filterHiddenFieldsEdges( + edge: EdgeType, + newEdges: EdgeType[], + targetNode: AllNodeType, +) { + if (targetNode) { + const nodeInputType = edge.data?.targetHandle?.inputTypes; + const nodeTemplates = targetNode.data.node!.template; + + Object.keys(nodeTemplates).forEach((key) => { + if (!nodeTemplates[key]?.input_types) return; + if ( + nodeTemplates[key]?.input_types?.some((type) => + nodeInputType?.includes(type), + ) && + !nodeTemplates[key].show + ) { + newEdges = newEdges.filter((e) => e.id !== edge.id); + } + }); + } + return newEdges; +} + export function detectBrokenEdgesEdges(nodes: AllNodeType[], edges: Edge[]) { function generateAlertObject(sourceNode, targetNode, edge) { const targetHandleObject: targetHandleType = scapeJSONParse(