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:
parent
247e911620
commit
6abb03dfa1
2 changed files with 16 additions and 7 deletions
|
|
@ -16,7 +16,7 @@ import {
|
||||||
} from "../utils";
|
} from "../utils";
|
||||||
import { alertContext } from "./alertContext";
|
import { alertContext } from "./alertContext";
|
||||||
import { typesContext } from "./typesContext";
|
import { typesContext } from "./typesContext";
|
||||||
import { APITemplateType } from "../types/api";
|
import { APIClassType, APITemplateType } from "../types/api";
|
||||||
import ShortUniqueId from "short-unique-id";
|
import ShortUniqueId from "short-unique-id";
|
||||||
import { addEdge } from "reactflow";
|
import { addEdge } from "reactflow";
|
||||||
import {
|
import {
|
||||||
|
|
@ -192,20 +192,26 @@ export function TabsProvider({ children }: { children: ReactNode }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function processFlowEdges(flow) {
|
function processFlowEdges(flow) {
|
||||||
|
if(!flow.data || !flow.data.edges) return;
|
||||||
flow.data.edges.forEach((edge) => {
|
flow.data.edges.forEach((edge) => {
|
||||||
edge.className = "";
|
edge.className = "";
|
||||||
edge.style = { stroke: "#555555" };
|
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) {
|
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];
|
const template = templates[node.data.type];
|
||||||
if (!template) {
|
if (!template) {
|
||||||
setErrorData({ title: `Unknown node type: ${node.data.type}` });
|
setErrorData({ title: `Unknown node type: ${node.data.type}` });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Object.keys(template["template"]).length > 0) {
|
if (Object.keys(template["template"]).length > 0) {
|
||||||
|
updateDisplay_name(node,template);
|
||||||
updateNodeBaseClasses(node, template);
|
updateNodeBaseClasses(node, template);
|
||||||
updateNodeEdges(flow, node, template);
|
updateNodeEdges(flow, node, template);
|
||||||
updateNodeDescription(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"];
|
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) => {
|
flow.data.edges.forEach((edge) => {
|
||||||
if (edge.source === node.id) {
|
if (edge.source === node.id) {
|
||||||
edge.sourceHandle = edge.sourceHandle
|
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"];
|
node.data.node.description = template["description"];
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateNodeTemplate(node, template) {
|
function updateNodeTemplate(node:NodeType,template:APIClassType) {
|
||||||
node.data.node.template = updateTemplate(
|
node.data.node.template = updateTemplate(
|
||||||
template["template"] as unknown as APITemplateType,
|
template["template"] as unknown as APITemplateType,
|
||||||
node.data.node.template 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.
|
// Create a new flow with a default name if no flow is provided.
|
||||||
const newFlow = createNewFlow(flowData, flow);
|
const newFlow = createNewFlow(flowData, flow);
|
||||||
|
processFlowEdges(newFlow);
|
||||||
|
processFlowNodes(newFlow);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { id } = await saveFlowToDatabase(newFlow);
|
const { id } = await saveFlowToDatabase(newFlow);
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import { ReactFlowInstance } from "reactflow";
|
import { ReactFlowInstance } from "reactflow";
|
||||||
|
import { APIClassType } from "../api";
|
||||||
|
|
||||||
const types: { [char: string]: string } = {};
|
const types: { [char: string]: string } = {};
|
||||||
const template: { [char: string]: string } = {};
|
const template: { [char: string]: APIClassType } = {};
|
||||||
const data: { [char: string]: string } = {};
|
const data: { [char: string]: string } = {};
|
||||||
|
|
||||||
export type typesContextType = {
|
export type typesContextType = {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue