Skip to content

Commit

Permalink
fix: Add Helper for Field Processing & Update Code Validation Logic (l…
Browse files Browse the repository at this point in the history
…angflow-ai#4429)

* 📝 (GenericNode/index.tsx): Add import statement for cloneDeep from lodash library
📝 (GenericNode/index.tsx): Add import statement for processNodeAdvancedFields helper function
📝 (GenericNode/index.tsx): Add edges variable to store edges state using useFlowStore hook
📝 (GenericNode/index.tsx): Modify onSuccess callback to process and update node data with advanced fields using processNodeAdvancedFields helper function

* ✨ (process-node-advanced-fields.ts): add a new function to process advanced fields of a node based on edges in a graph for better customization and control.

* ♻️ (GenericNode/index.tsx): Remove unused import of cloneDeep from lodash to clean up the code and improve maintainability.

* 🔧 (process-node-advanced-fields.ts): rename variable edgesWithNode to relevantEdges for clarity and consistency
🐛 (process-node-advanced-fields.ts): fix condition to check relevantEdges length instead of edgesWithNode length for correct logic

---------

Co-authored-by: anovazzi1 <[email protected]>
  • Loading branch information
Cristhianzl and anovazzi1 authored Nov 7, 2024
1 parent 311bf00 commit ada5f17
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/frontend/src/CustomNodes/GenericNode/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { getNodeInputColors } from "../helpers/get-node-input-colors";
import { getNodeInputColorsName } from "../helpers/get-node-input-colors-name";
import { getNodeOutputColors } from "../helpers/get-node-output-colors";
import { getNodeOutputColorsName } from "../helpers/get-node-output-colors-name";
import { processNodeAdvancedFields } from "../helpers/process-node-advanced-fields";
import useCheckCodeValidity from "../hooks/use-check-code-validity";
import useUpdateNodeCode from "../hooks/use-update-node-code";
import getFieldTitle from "../utils/get-field-title";
Expand Down Expand Up @@ -85,6 +86,8 @@ export default function GenericNode({

const { mutate: validateComponentCode } = usePostValidateComponentCode();

const edges = useFlowStore((state) => state.edges);

const handleUpdateCode = () => {
setLoadingUpdate(true);
takeSnapshot();
Expand All @@ -99,9 +102,15 @@ export default function GenericNode({
validateComponentCode(
{ code: currentCode, frontend_node: data.node },
{
onSuccess: ({ data, type }) => {
if (data && type && updateNodeCode) {
updateNodeCode(data, currentCode, "code", type);
onSuccess: ({ data: resData, type }) => {
if (resData && type && updateNodeCode) {
const newNode = processNodeAdvancedFields(
resData,
edges,
data.id,
);

updateNodeCode(newNode, currentCode, "code", type);
setLoadingUpdate(false);
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { APIClassType } from "@/types/api";
import { cloneDeep } from "lodash";
import { Edge } from "reactflow";

export function processNodeAdvancedFields(
resData: APIClassType,
edges: Edge[],
nodeId: string,
) {
let newNode = cloneDeep(resData);

const relevantEdges = edges.filter(
(edge) => edge.source !== nodeId || edge.target !== nodeId,
);

if (relevantEdges.length === 0) {
return newNode;
}

for (const edge of relevantEdges) {
const field = edge?.data?.targetHandle?.fieldName;

if (field) {
const fieldTemplate = newNode.template[field];
if (fieldTemplate?.advanced === true) {
newNode.template[field].advanced = false;
}
}
}

return newNode;
}

0 comments on commit ada5f17

Please sign in to comment.