From 625a8cc371dda8eea8fec7f0635e926c9488f35d Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 16 Apr 2025 16:43:58 +0800 Subject: [PATCH] feat: add necessary store api --- .../workflow/current-vars-store/store.ts | 33 +++++++++++++++++++ .../workflow/last-run-store/store.ts | 12 +++++++ 2 files changed, 45 insertions(+) diff --git a/web/app/components/workflow/current-vars-store/store.ts b/web/app/components/workflow/current-vars-store/store.ts index f8b40d9f07..e1efa9690e 100644 --- a/web/app/components/workflow/current-vars-store/store.ts +++ b/web/app/components/workflow/current-vars-store/store.ts @@ -25,6 +25,9 @@ type CurrentVarsActions = { setNodeVars: (nodeId: string, payload: NodeVars) => void clearNodeVars: (nodeId: string) => void getNodeVars: (nodeId: string) => NodeVars | undefined + hasNodeVars: (nodeId: string) => boolean + setVar: (nodeId: string, key: string, value: any) => void + getVar: (nodeId: string, key: string) => any } type CurrentVarsStore = CurrentVarsState & CurrentVarsActions @@ -79,6 +82,36 @@ export const createCurrentVarsStore = () => { const nodes = get().nodes return nodes.find(node => node.id === nodeId) }, + hasNodeVars: (nodeId) => { + return !!get().getNodeVars(nodeId) + }, + setVar: (nodeId, key, value) => { + set(produce((state: CurrentVarsStore) => { + // eslint-disable-next-line sonarjs/no-nested-functions + const nodes = state.nodes.map((node) => { + if (node.id === nodeId) { + return produce(node, (draft) => { + const index = draft.vars.findIndex(v => v.key === key) + if (index !== -1) + draft.vars[index].value = value + }) + } + return node + }) + state.nodes = nodes + })) + }, + getVar(nodeId, key) { + const node = get().getNodeVars(nodeId) + if (!node) + return undefined + + const variable = node.vars.find(v => v.key === key) + if (!variable) + return undefined + + return variable.value + }, })) } diff --git a/web/app/components/workflow/last-run-store/store.ts b/web/app/components/workflow/last-run-store/store.ts index c21ff7b61c..db5710944e 100644 --- a/web/app/components/workflow/last-run-store/store.ts +++ b/web/app/components/workflow/last-run-store/store.ts @@ -24,6 +24,7 @@ type LastRunActions = { setInfos: (vars: NodeInfo[]) => void getInfos: () => NodeInfo[] getNodeInfo: (nodeId: string) => NodeInfo | undefined + getVar: (nodeId: string, key: string) => any } type LastRunStore = LastRunState & LastRunActions @@ -55,6 +56,17 @@ export const createLastRunStore = () => { const nodes = get().nodes return nodes.find(node => node.id === nodeId) }, + getVar: (nodeId, key) => { + const node = get().getNodeInfo(nodeId) + if (!node) + return undefined + + const varItem = node.vars.find(v => v.key === key) + if (!varItem) + return undefined + + return varItem.value + }, })) }