From f6c280f79df35ce00b00f9526db9daf5d2e1d562 Mon Sep 17 00:00:00 2001 From: Emir Karabeg Date: Tue, 25 Mar 2025 21:15:53 -0700 Subject: [PATCH] improvement(vars): variable rename reference change --- sim/stores/panel/variables/store.ts | 68 ++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/sim/stores/panel/variables/store.ts b/sim/stores/panel/variables/store.ts index bf7538ed55..e74a6cf669 100644 --- a/sim/stores/panel/variables/store.ts +++ b/sim/stores/panel/variables/store.ts @@ -4,6 +4,7 @@ import { devtools, persist } from 'zustand/middleware' import { Variable, VariablesStore } from './types' import { API_ENDPOINTS } from '@/stores/constants' import { useWorkflowRegistry } from '@/stores/workflows/registry/store' +import { useSubBlockStore } from '@/stores/workflows/subblock/store' const logger = createLogger('Variables Store') const SAVE_DEBOUNCE_DELAY = 500 // 500ms debounce delay @@ -104,7 +105,9 @@ export const useVariablesStore = create()( // If name is being updated, ensure it's unique if (update.name) { - const workflowId = state.variables[id].workflowId + const oldVariable = state.variables[id] + const oldVariableName = oldVariable.name + const workflowId = oldVariable.workflowId const workflowVariables = Object.values(state.variables).filter( v => v.workflowId === workflowId && v.id !== id ) @@ -118,6 +121,69 @@ export const useVariablesStore = create()( nameIndex++ } + // If name has changed, update references in subblocks + if (uniqueName !== oldVariableName) { + // Update references in subblock store + const subBlockStore = useSubBlockStore.getState() + const activeWorkflowId = useWorkflowRegistry.getState().activeWorkflowId + + if (activeWorkflowId) { + // Get the workflow values for the active workflow + const workflowValues = subBlockStore.workflowValues[activeWorkflowId] || {} + const updatedWorkflowValues = { ...workflowValues } + + // Loop through blocks + Object.entries(workflowValues).forEach(([blockId, blockValues]) => { + // Loop through subblocks and update references + Object.entries(blockValues as Record).forEach(([subBlockId, value]) => { + const oldVarName = oldVariableName.replace(/\s+/g, '').toLowerCase() + const newVarName = uniqueName.replace(/\s+/g, '').toLowerCase() + const regex = new RegExp(``, 'gi') + + // Use a recursive function to handle all object types + updatedWorkflowValues[blockId][subBlockId] = updateReferences( + value, + regex, + `` + ) + + // Helper function to recursively update references in any data structure + function updateReferences(value: any, regex: RegExp, replacement: string): any { + // Handle string values + if (typeof value === 'string') { + return regex.test(value) ? value.replace(regex, replacement) : value + } + + // Handle arrays + if (Array.isArray(value)) { + return value.map((item) => updateReferences(item, regex, replacement)) + } + + // Handle objects + if (value !== null && typeof value === 'object') { + const result = { ...value } + for (const key in result) { + result[key] = updateReferences(result[key], regex, replacement) + } + return result + } + + // Return unchanged for other types + return value + } + }) + }) + + // Update the subblock store with the new values + useSubBlockStore.setState({ + workflowValues: { + ...subBlockStore.workflowValues, + [activeWorkflowId]: updatedWorkflowValues, + }, + }) + } + } + // Update with unique name update = { ...update, name: uniqueName } }