Skip to content

Commit 22b3dde

Browse files
fix(env-vars): promotion should never leave a copy in personal vars (#1678)
* fix(env-vars): promotion should never leave a copy in personal vars * fix lint
1 parent c1725c1 commit 22b3dde

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/environment/environment.tsx

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,20 @@ import type { EnvironmentVariable as StoreEnvironmentVariable } from '@/stores/s
2424
const logger = createLogger('EnvironmentVariables')
2525

2626
const GRID_COLS = 'grid grid-cols-[minmax(0,1fr),minmax(0,1fr),88px] gap-4'
27-
const INITIAL_ENV_VAR: UIEnvironmentVariable = { key: '', value: '' }
27+
28+
const generateRowId = (() => {
29+
let counter = 0
30+
return () => {
31+
counter += 1
32+
return Date.now() + counter
33+
}
34+
})()
35+
36+
const createEmptyEnvVar = (): UIEnvironmentVariable => ({
37+
key: '',
38+
value: '',
39+
id: generateRowId(),
40+
})
2841

2942
interface UIEnvironmentVariable extends StoreEnvironmentVariable {
3043
id?: number
@@ -132,7 +145,12 @@ export function EnvironmentVariables({
132145

133146
useEffect(() => {
134147
const existingVars = Object.values(variables)
135-
const initialVars = existingVars.length ? existingVars : [INITIAL_ENV_VAR]
148+
const initialVars = existingVars.length
149+
? existingVars.map((envVar) => ({
150+
...envVar,
151+
id: generateRowId(),
152+
}))
153+
: [createEmptyEnvVar()]
136154
initialVarsRef.current = JSON.parse(JSON.stringify(initialVars))
137155
setEnvVars(JSON.parse(JSON.stringify(initialVars)))
138156
pendingClose.current = false
@@ -179,6 +197,22 @@ export function EnvironmentVariables({
179197
}
180198
}, [shouldScrollToBottom])
181199

200+
useEffect(() => {
201+
const personalKeys = envVars.map((envVar) => envVar.key.trim()).filter((key) => key.length > 0)
202+
203+
const uniquePersonalKeys = Array.from(new Set(personalKeys))
204+
205+
const computedConflicts = uniquePersonalKeys.filter((key) => Object.hasOwn(workspaceVars, key))
206+
207+
setConflicts((prev) => {
208+
if (prev.length === computedConflicts.length) {
209+
const sameKeys = prev.every((key) => computedConflicts.includes(key))
210+
if (sameKeys) return prev
211+
}
212+
return computedConflicts
213+
})
214+
}, [envVars, workspaceVars])
215+
182216
const handleWorkspaceKeyRename = useCallback(
183217
(currentKey: string, currentValue: string) => {
184218
const newKey = pendingKeyValue.trim()
@@ -192,17 +226,11 @@ export function EnvironmentVariables({
192226
next[newKey] = currentValue
193227
return next
194228
})
195-
196-
setConflicts((prev) => {
197-
const withoutOld = prev.filter((k) => k !== currentKey)
198-
const personalHasNew = !!useEnvironmentStore.getState().variables[newKey]
199-
return personalHasNew && !withoutOld.includes(newKey) ? [...withoutOld, newKey] : withoutOld
200-
})
201229
},
202-
[pendingKeyValue, renamingKey, setWorkspaceVars, setConflicts]
230+
[pendingKeyValue, renamingKey]
203231
)
204232
const addEnvVar = () => {
205-
const newVar = { key: '', value: '', id: Date.now() }
233+
const newVar = createEmptyEnvVar()
206234
setEnvVars([...envVars, newVar])
207235
setSearchTerm('')
208236
setShouldScrollToBottom(true)
@@ -216,7 +244,7 @@ export function EnvironmentVariables({
216244

217245
const removeEnvVar = (index: number) => {
218246
const newEnvVars = envVars.filter((_, i) => i !== index)
219-
setEnvVars(newEnvVars.length ? newEnvVars : [INITIAL_ENV_VAR])
247+
setEnvVars(newEnvVars.length ? newEnvVars : [createEmptyEnvVar()])
220248
}
221249

222250
const handleValueFocus = (index: number, e: React.FocusEvent<HTMLInputElement>) => {
@@ -290,7 +318,7 @@ export function EnvironmentVariables({
290318
return {
291319
key,
292320
value,
293-
id: Date.now() + Math.random(),
321+
id: generateRowId(),
294322
}
295323
})
296324
.filter((parsed): parsed is NonNullable<typeof parsed> => parsed !== null)
@@ -409,10 +437,10 @@ export function EnvironmentVariables({
409437
onClick={() => {
410438
if (!envVar.key || !envVar.value || !workspaceId) return
411439
setWorkspaceVars((prev) => ({ ...prev, [envVar.key]: envVar.value }))
412-
setConflicts((prev) =>
413-
prev.includes(envVar.key) ? prev : [...prev, envVar.key]
414-
)
415-
removeEnvVar(originalIndex)
440+
setEnvVars((prev) => {
441+
const filtered = prev.filter((entry) => entry !== envVar)
442+
return filtered.length ? filtered : [createEmptyEnvVar()]
443+
})
416444
}}
417445
className='h-9 w-9 rounded-[8px] bg-muted p-0 text-muted-foreground hover:bg-muted/70'
418446
>
@@ -538,7 +566,6 @@ export function EnvironmentVariables({
538566
delete next[key]
539567
return next
540568
})
541-
setConflicts((prev) => prev.filter((k) => k !== key))
542569
}}
543570
className='h-9 w-9 rounded-[8px] bg-muted p-0 text-muted-foreground hover:bg-muted/70'
544571
>
@@ -593,7 +620,6 @@ export function EnvironmentVariables({
593620
delete next[key]
594621
return next
595622
})
596-
setConflicts((prev) => prev.filter((k) => k !== key))
597623
}}
598624
className='h-9 w-9 rounded-[8px] bg-muted p-0 text-muted-foreground hover:bg-muted/70'
599625
>

apps/sim/tools/error-extractors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function extractErrorMessageWithId(
134134

135135
try {
136136
const message = extractor.extract(errorInfo)
137-
if (message && message.trim()) {
137+
if (message?.trim()) {
138138
return message
139139
}
140140
} catch (error) {}
@@ -151,7 +151,7 @@ export function extractErrorMessage(errorInfo?: ErrorInfo, extractorId?: string)
151151
for (const extractor of ERROR_EXTRACTORS) {
152152
try {
153153
const message = extractor.extract(errorInfo)
154-
if (message && message.trim()) {
154+
if (message?.trim()) {
155155
return message
156156
}
157157
} catch (error) {}

0 commit comments

Comments
 (0)