refactor(api/index.ts): remove unused variable property from APITemplateType

fix(utils/reactflowUtils.ts): import TemplateVariableType from types/api
feat(utils/reactflowUtils.ts): add mergeNodeTemplates function to merge node templates and update display names
feat(utils/reactflowUtils.ts): add isHandleConnected function to check if a handle is connected to an edge
refactor(utils/reactflowUtils.ts): remove TODO comments and update comments for mergeNodeTemplates and isHandleConnected functions
feat(utils/reactflowUtils.ts): add generateNodeTemplate function to generate node templates from a flow
This commit is contained in:
anovazzi1 2023-09-08 12:24:04 -03:00
commit 056cf6f66d
2 changed files with 77 additions and 2 deletions

View file

@ -5,7 +5,6 @@ export type APIDataType = { [key: string]: APIKindType };
export type APIObjectType = { kind: APIKindType; [key: string]: APIKindType };
export type APIKindType = { class: APIClassType; [key: string]: APIClassType };
export type APITemplateType = {
variable: TemplateVariableType;
[key: string]: TemplateVariableType;
};
export type APIClassType = {

View file

@ -9,7 +9,7 @@ import {
XYPosition,
} from "reactflow";
import { specialCharsRegex } from "../constants/constants";
import { APITemplateType } from "../types/api";
import { APITemplateType, TemplateVariableType } from "../types/api";
import {
FlowType,
NodeType,
@ -538,6 +538,82 @@ function updateGroupNodeTemplate(template: APITemplateType) {
});
return template;
}
// TODO UPDATE TO NEW HANDLE FORMAT
export function mergeNodeTemplates({
nodes,
edges,
}: {
nodes: NodeType[];
edges: Edge[];
}): APITemplateType {
/* this function receives a flow and iterate trhow each node
and merge the templates with only the visible fields
if there are two keys with the same name in the flow, we will update the display name of each one
to show from which node it came from
*/
let template: APITemplateType = {};
nodes.forEach((node) => {
let nodeTemplate = _.cloneDeep(node.data.node!.template);
Object.keys(nodeTemplate)
.filter((field_name) => field_name.charAt(0) !== "_")
.forEach((key) => {
if (
nodeTemplate[key].show &&
!isHandleConnected(edges, key, nodeTemplate[key], node.id)
) {
template[key + "_" + node.id] = nodeTemplate[key];
template[key + "_" + node.id].proxy = { id: node.id, field: key };
if (node.type === "groupNode") {
template[key + "_" + node.id].display_name =
node.data.node!.flow!.name + " - " + nodeTemplate[key].name;
} else {
template[key + "_" + node.id].display_name =
node.data.type + " - " + nodeTemplate[key].name;
}
}
});
});
return template;
}
// TODO UPDATE TO NEW HANDLE FORMAT
function isHandleConnected(
edges: Edge[],
key: string,
field: TemplateVariableType,
nodeId: string
) {
/*
this function receives a flow and a handleId and check if there is a connection with this handle
*/
if (field.proxy) {
if (
edges.some(
(e) =>
e.targetHandle ===
field.type +
"|" +
key +
"|" +
nodeId +
"|" +
field.proxy.id +
"|" +
field.proxy.field
)
) {
return true;
}
} else {
if (
edges.some(
(e) => e.targetHandle === field.type + "|" + key + "|" + nodeId
)
) {
return true;
}
}
return false;
}
export function generateNodeTemplate(Flow: FlowType) {
/*