refactor(ui): clean code

This commit is contained in:
Fu Diwei 2025-02-06 16:18:23 +08:00
parent 24b591ed62
commit 4b931f782e
2 changed files with 14 additions and 32 deletions
ui/src
domain
stores/workflow

@ -276,21 +276,21 @@ export const updateNode = (node: WorkflowNode, targetNode: WorkflowNode) => {
}); });
}; };
export const addNode = (node: WorkflowNode, preId: string, targetNode: WorkflowNode) => { export const addNode = (node: WorkflowNode, previousNodeId: string, targetNode: WorkflowNode) => {
return produce(node, (draft) => { return produce(node, (draft) => {
let current = draft; let current = draft;
while (current) { while (current) {
if (current.id === preId && targetNode.type !== WorkflowNodeType.Branch && targetNode.type !== WorkflowNodeType.ExecuteResultBranch) { if (current.id === previousNodeId && targetNode.type !== WorkflowNodeType.Branch && targetNode.type !== WorkflowNodeType.ExecuteResultBranch) {
targetNode.next = current.next; targetNode.next = current.next;
current.next = targetNode; current.next = targetNode;
break; break;
} else if (current.id === preId && (targetNode.type === WorkflowNodeType.Branch || targetNode.type === WorkflowNodeType.ExecuteResultBranch)) { } else if (current.id === previousNodeId && (targetNode.type === WorkflowNodeType.Branch || targetNode.type === WorkflowNodeType.ExecuteResultBranch)) {
targetNode.branches![0].next = current.next; targetNode.branches![0].next = current.next;
current.next = targetNode; current.next = targetNode;
break; break;
} }
if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) { if (current.type === WorkflowNodeType.Branch || current.type === WorkflowNodeType.ExecuteResultBranch) {
current.branches = current.branches!.map((branch) => addNode(branch, preId, targetNode)); current.branches = current.branches!.map((branch) => addNode(branch, previousNodeId, targetNode));
} }
current = current.next as WorkflowNode; current = current.next as WorkflowNode;
} }
@ -382,15 +382,15 @@ export const removeBranch = (node: WorkflowNode, branchNodeId: string, branchInd
}); });
}; };
export const getWorkflowOutputBeforeId = (root: WorkflowNode, nodeId: string, type: string): WorkflowNode[] => {
// 1 个分支的节点,不应该能获取到相邻分支上节点的输出 // 1 个分支的节点,不应该能获取到相邻分支上节点的输出
export const getWorkflowOutputBeforeId = (node: WorkflowNode, id: string, type: string): WorkflowNode[] => {
const output: WorkflowNode[] = []; const output: WorkflowNode[] = [];
const traverse = (current: WorkflowNode, output: WorkflowNode[]) => { const traverse = (current: WorkflowNode, output: WorkflowNode[]) => {
if (!current) { if (!current) {
return false; return false;
} }
if (current.id === id) { if (current.id === nodeId) {
return true; return true;
} }
@ -422,7 +422,7 @@ export const getWorkflowOutputBeforeId = (node: WorkflowNode, id: string, type:
return traverse(current.next as WorkflowNode, output); return traverse(current.next as WorkflowNode, output);
}; };
traverse(node, output); traverse(root, output);
return output; return output;
}; };
@ -446,21 +446,3 @@ export const isAllNodesValidated = (node: WorkflowNode): boolean => {
return true; return true;
}; };
/**
* @deprecated
*/
export const getExecuteMethod = (node: WorkflowNode): { trigger: string; triggerCron: string } => {
if (node.type === WorkflowNodeType.Start) {
const config = node.config as WorkflowNodeConfigForStart;
return {
trigger: config.trigger ?? "",
triggerCron: config.triggerCron ?? "",
};
} else {
return {
trigger: "",
triggerCron: "",
};
}
};

@ -25,14 +25,14 @@ export type WorkflowState = {
discard(): void; discard(): void;
destroy(): void; destroy(): void;
addNode: (node: WorkflowNode, preId: string) => void; addNode: (node: WorkflowNode, previousNodeId: string) => void;
updateNode: (node: WorkflowNode) => void; updateNode: (node: WorkflowNode) => void;
removeNode: (nodeId: string) => void; removeNode: (nodeId: string) => void;
addBranch: (branchId: string) => void; addBranch: (branchId: string) => void;
removeBranch: (branchId: string, index: number) => void; removeBranch: (branchId: string, index: number) => void;
getWorkflowOuptutBeforeId: (id: string, type: string) => WorkflowNode[]; getWorkflowOuptutBeforeId: (nodeId: string, type: string) => WorkflowNode[];
}; };
export const useWorkflowStore = create<WorkflowState>((set, get) => ({ export const useWorkflowStore = create<WorkflowState>((set, get) => ({
@ -143,10 +143,10 @@ export const useWorkflowStore = create<WorkflowState>((set, get) => ({
}); });
}, },
addNode: async (node: WorkflowNode, preId: string) => { addNode: async (node: WorkflowNode, previousNodeId: string) => {
if (!get().initialized) throw "Workflow not initialized yet"; if (!get().initialized) throw "Workflow not initialized yet";
const root = addNode(get().workflow.draft!, preId, node); const root = addNode(get().workflow.draft!, previousNodeId, node);
const resp = await saveWorkflow({ const resp = await saveWorkflow({
id: get().workflow.id!, id: get().workflow.id!,
draft: root, draft: root,
@ -243,7 +243,7 @@ export const useWorkflowStore = create<WorkflowState>((set, get) => ({
}); });
}, },
getWorkflowOuptutBeforeId: (id: string, type: string) => { getWorkflowOuptutBeforeId: (nodeId: string, type: string) => {
return getWorkflowOutputBeforeId(get().workflow.draft as WorkflowNode, id, type); return getWorkflowOutputBeforeId(get().workflow.draft as WorkflowNode, nodeId, type);
}, },
})); }));