From 36724a2b472fc23de47d2241c2f551d7a2a6d553 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 16 Apr 2025 16:52:15 +0800 Subject: [PATCH] feat: use current vars hooks --- .../workflow/hooks/use-current-vars.ts | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 web/app/components/workflow/hooks/use-current-vars.ts diff --git a/web/app/components/workflow/hooks/use-current-vars.ts b/web/app/components/workflow/hooks/use-current-vars.ts new file mode 100644 index 0000000000..f6ca561d76 --- /dev/null +++ b/web/app/components/workflow/hooks/use-current-vars.ts @@ -0,0 +1,31 @@ +import { useCurrentVarsStore } from '../current-vars-store/store' +import { useLastRunStore } from '../last-run-store/store' +const useCurrentVars = () => { + const currentVars = useCurrentVarsStore(state => state.nodes) + const getCurrentVar = useCurrentVarsStore(state => state.getVar) + const getLastRunVar = useLastRunStore(state => state.getVar) + const setCurrentVar = useCurrentVarsStore(state => state.setVar) + const clearCurrentVars = useCurrentVarsStore(state => state.clearVars) + const clearNodeVars = useCurrentVarsStore(state => state.clearNodeVars) + + const isVarChanged = (nodeId: string, key: string) => { + return getCurrentVar(nodeId, key) !== getLastRunVar(nodeId, key) + } + + const resetToLastRunVar = (nodeId: string, key: string) => { + const lastRunVar = getLastRunVar(nodeId, key) + if (lastRunVar) + setCurrentVar(nodeId, key, lastRunVar) + } + + return { + currentVars, + isVarChanged, + clearCurrentVars, + clearNodeVars, + setCurrentVar, + resetToLastRunVar, + } +} + +export default useCurrentVars