refactor(tabsContext.tsx): add type annotations to function parameters and return types

feat(tabsContext.tsx): add support for display_name property in node templates to allow custom node names
fix(tabsContext.tsx): add null checks to prevent errors when processing flow edges and nodes
refactor(typesContext/index.ts): change template object type to APIClassType to match usage in tabsContext
This commit is contained in:
anovazzi1 2023-06-23 18:26:31 -03:00
commit 6abb03dfa1
2 changed files with 16 additions and 7 deletions

View file

@ -16,7 +16,7 @@ import {
} from "../utils";
import { alertContext } from "./alertContext";
import { typesContext } from "./typesContext";
import { APITemplateType } from "../types/api";
import { APIClassType, APITemplateType } from "../types/api";
import ShortUniqueId from "short-unique-id";
import { addEdge } from "reactflow";
import {
@ -192,20 +192,26 @@ export function TabsProvider({ children }: { children: ReactNode }) {
}
function processFlowEdges(flow) {
if(!flow.data || !flow.data.edges) return;
flow.data.edges.forEach((edge) => {
edge.className = "";
edge.style = { stroke: "#555555" };
});
}
function updateDisplay_name(node:NodeType,template:APIClassType) {
node.data.node.display_name = template["display_name"]?template["display_name"]:node.data.type;
}
function processFlowNodes(flow) {
flow.data.nodes.forEach((node) => {
if(!flow.data || !flow.data.nodes) return;
flow.data.nodes.forEach((node:NodeType) => {
const template = templates[node.data.type];
if (!template) {
setErrorData({ title: `Unknown node type: ${node.data.type}` });
return;
}
if (Object.keys(template["template"]).length > 0) {
updateDisplay_name(node,template);
updateNodeBaseClasses(node, template);
updateNodeEdges(flow, node, template);
updateNodeDescription(node, template);
@ -214,11 +220,11 @@ export function TabsProvider({ children }: { children: ReactNode }) {
});
}
function updateNodeBaseClasses(node, template) {
function updateNodeBaseClasses(node:NodeType,template:APIClassType) {
node.data.node.base_classes = template["base_classes"];
}
function updateNodeEdges(flow, node, template) {
function updateNodeEdges(flow:FlowType, node:NodeType,template:APIClassType) {
flow.data.edges.forEach((edge) => {
if (edge.source === node.id) {
edge.sourceHandle = edge.sourceHandle
@ -230,11 +236,11 @@ export function TabsProvider({ children }: { children: ReactNode }) {
});
}
function updateNodeDescription(node, template) {
function updateNodeDescription(node:NodeType,template:APIClassType) {
node.data.node.description = template["description"];
}
function updateNodeTemplate(node, template) {
function updateNodeTemplate(node:NodeType,template:APIClassType) {
node.data.node.template = updateTemplate(
template["template"] as unknown as APITemplateType,
node.data.node.template as APITemplateType
@ -463,6 +469,8 @@ export function TabsProvider({ children }: { children: ReactNode }) {
// Create a new flow with a default name if no flow is provided.
const newFlow = createNewFlow(flowData, flow);
processFlowEdges(newFlow);
processFlowNodes(newFlow);
try {
const { id } = await saveFlowToDatabase(newFlow);

View file

@ -1,7 +1,8 @@
import { ReactFlowInstance } from "reactflow";
import { APIClassType } from "../api";
const types: { [char: string]: string } = {};
const template: { [char: string]: string } = {};
const template: { [char: string]: APIClassType } = {};
const data: { [char: string]: string } = {};
export type typesContextType = {