From 4b931f782e16b2c8629ed656ba457da9b97d1e9d Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Thu, 6 Feb 2025 16:18:23 +0800 Subject: [PATCH] refactor(ui): clean code --- ui/src/domain/workflow.ts | 34 ++++++++------------------------- ui/src/stores/workflow/index.ts | 12 ++++++------ 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/ui/src/domain/workflow.ts b/ui/src/domain/workflow.ts index d001f2e9..82625c21 100644 --- a/ui/src/domain/workflow.ts +++ b/ui/src/domain/workflow.ts @@ -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) => { let current = draft; 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; current.next = targetNode; 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; current.next = targetNode; break; } 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; } @@ -382,15 +382,15 @@ export const removeBranch = (node: WorkflowNode, branchNodeId: string, branchInd }); }; -// 1 个分支的节点,不应该能获取到相邻分支上节点的输出 -export const getWorkflowOutputBeforeId = (node: WorkflowNode, id: string, type: string): WorkflowNode[] => { +export const getWorkflowOutputBeforeId = (root: WorkflowNode, nodeId: string, type: string): WorkflowNode[] => { + // 1 个分支的节点,不应该能获取到相邻分支上节点的输出 const output: WorkflowNode[] = []; const traverse = (current: WorkflowNode, output: WorkflowNode[]) => { if (!current) { return false; } - if (current.id === id) { + if (current.id === nodeId) { return true; } @@ -422,7 +422,7 @@ export const getWorkflowOutputBeforeId = (node: WorkflowNode, id: string, type: return traverse(current.next as WorkflowNode, output); }; - traverse(node, output); + traverse(root, output); return output; }; @@ -446,21 +446,3 @@ export const isAllNodesValidated = (node: WorkflowNode): boolean => { 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: "", - }; - } -}; diff --git a/ui/src/stores/workflow/index.ts b/ui/src/stores/workflow/index.ts index 4e1559ff..9ec83926 100644 --- a/ui/src/stores/workflow/index.ts +++ b/ui/src/stores/workflow/index.ts @@ -25,14 +25,14 @@ export type WorkflowState = { discard(): void; destroy(): void; - addNode: (node: WorkflowNode, preId: string) => void; + addNode: (node: WorkflowNode, previousNodeId: string) => void; updateNode: (node: WorkflowNode) => void; removeNode: (nodeId: string) => void; addBranch: (branchId: string) => void; removeBranch: (branchId: string, index: number) => void; - getWorkflowOuptutBeforeId: (id: string, type: string) => WorkflowNode[]; + getWorkflowOuptutBeforeId: (nodeId: string, type: string) => WorkflowNode[]; }; export const useWorkflowStore = create((set, get) => ({ @@ -143,10 +143,10 @@ export const useWorkflowStore = create((set, get) => ({ }); }, - addNode: async (node: WorkflowNode, preId: string) => { + addNode: async (node: WorkflowNode, previousNodeId: string) => { 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({ id: get().workflow.id!, draft: root, @@ -243,7 +243,7 @@ export const useWorkflowStore = create((set, get) => ({ }); }, - getWorkflowOuptutBeforeId: (id: string, type: string) => { - return getWorkflowOutputBeforeId(get().workflow.draft as WorkflowNode, id, type); + getWorkflowOuptutBeforeId: (nodeId: string, type: string) => { + return getWorkflowOutputBeforeId(get().workflow.draft as WorkflowNode, nodeId, type); }, }));