From 33a2484d47830616213bca00e062608066155602 Mon Sep 17 00:00:00 2001 From: Cristhian Zanforlin Lousa Date: Wed, 12 Mar 2025 20:44:13 -0300 Subject: [PATCH] feat: Add updateHiddenOutputs helper function to manage output visibility (#6932) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✨ (update-hidden-outputs.ts): add a new helper function to update hidden outputs in the frontend CustomNodes module ♻️ (use-update-all-nodes.ts): refactor useUpdateAllNodes hook to update hidden outputs for all nodes in the frontend CustomNodes module ♻️ (use-update-node-code.ts): refactor useUpdateNodeCode hook to update hidden outputs for a specific node's code in the frontend CustomNodes module ♻️ (index.ts): refactor types in the flow module to include OutputFieldType for better type checking and consistency --- .../helpers/update-hidden-outputs.ts | 19 +++++++++++++++++++ .../CustomNodes/hooks/use-update-all-nodes.ts | 12 +++++++++++- .../CustomNodes/hooks/use-update-node-code.ts | 9 +++++++++ src/frontend/src/types/flow/index.ts | 3 ++- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/frontend/src/CustomNodes/helpers/update-hidden-outputs.ts diff --git a/src/frontend/src/CustomNodes/helpers/update-hidden-outputs.ts b/src/frontend/src/CustomNodes/helpers/update-hidden-outputs.ts new file mode 100644 index 000000000..d2122f869 --- /dev/null +++ b/src/frontend/src/CustomNodes/helpers/update-hidden-outputs.ts @@ -0,0 +1,19 @@ +import { OutputFieldType } from "@/types/api"; + +export const updateHiddenOutputs = ( + outputs: OutputFieldType[], + updatedOutputs: OutputFieldType[], +) => { + if (outputs && updatedOutputs) { + outputs.forEach((output) => { + const indexOutput = updatedOutputs?.findIndex( + (o) => o.name === output.name, + ); + if (indexOutput === -1 || indexOutput === undefined) return; + updatedOutputs![indexOutput].hidden = output.hidden; + }); + return updatedOutputs; + } + + return outputs; +}; diff --git a/src/frontend/src/CustomNodes/hooks/use-update-all-nodes.ts b/src/frontend/src/CustomNodes/hooks/use-update-all-nodes.ts index 9be164173..4740897d1 100644 --- a/src/frontend/src/CustomNodes/hooks/use-update-all-nodes.ts +++ b/src/frontend/src/CustomNodes/hooks/use-update-all-nodes.ts @@ -1,7 +1,8 @@ import { AllNodeType } from "@/types/flow"; import { cloneDeep } from "lodash"; import { useCallback } from "react"; -import { APIClassType } from "../../types/api"; +import { APIClassType, OutputFieldType } from "../../types/api"; +import { updateHiddenOutputs } from "../helpers/update-hidden-outputs"; export type UpdateNodesType = { nodeId: string; @@ -27,6 +28,8 @@ const useUpdateAllNodes = ( if (nodeIndex === -1) return; const updatedNode = newNodes[nodeIndex]; + const outputs = updatedNode?.data.node?.outputs; + updatedNode.data = { ...updatedNode.data, node: { @@ -44,6 +47,13 @@ const useUpdateAllNodes = ( } updatedNode.data.node!.template[name].value = code; + + const updatedOutputs = updatedNode.data.node?.outputs; + updatedNode.data.node!.outputs = updateHiddenOutputs( + outputs!, + updatedOutputs!, + ); + updateNodeInternals(nodeId); }); diff --git a/src/frontend/src/CustomNodes/hooks/use-update-node-code.ts b/src/frontend/src/CustomNodes/hooks/use-update-node-code.ts index 961a8056b..4ce6d80f0 100644 --- a/src/frontend/src/CustomNodes/hooks/use-update-node-code.ts +++ b/src/frontend/src/CustomNodes/hooks/use-update-node-code.ts @@ -2,6 +2,7 @@ import useFlowStore from "@/stores/flowStore"; import { cloneDeep } from "lodash"; // or any other deep cloning library you prefer import { useCallback } from "react"; import { APIClassType } from "../../types/api"; +import { updateHiddenOutputs } from "../helpers/update-hidden-outputs"; const useUpdateNodeCode = ( dataId: string, @@ -32,6 +33,14 @@ const useUpdateNodeCode = ( setIsOutdated(false); setIsUserEdited(false); + const outputs = dataNode.outputs; + const updatedOutputs = newNodeClass.outputs; + + newNode.data.node!.outputs = updateHiddenOutputs( + outputs!, + updatedOutputs!, + ); + return newNode; }); diff --git a/src/frontend/src/types/flow/index.ts b/src/frontend/src/types/flow/index.ts index 881d842eb..0fe74b8bd 100644 --- a/src/frontend/src/types/flow/index.ts +++ b/src/frontend/src/types/flow/index.ts @@ -1,6 +1,6 @@ import { Edge, Node, ReactFlowJsonObject } from "@xyflow/react"; import { BuildStatus } from "../../constants/enums"; -import { APIClassType } from "../api/index"; +import { APIClassType, OutputFieldType } from "../api/index"; export type PaginatedFlowsType = { items: FlowType[]; @@ -48,6 +48,7 @@ export type noteClassType = Pick< backgroundColor?: string; [key: string]: any; }; + outputs?: OutputFieldType[]; }; export type NoteDataType = {