Skip to content

Commit 9181e93

Browse files
committed
fix(variables): use stable ref objects for editor container refs
1 parent 2372651 commit 9181e93

File tree

1 file changed

+18
-16
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/variables-input

1 file changed

+18
-16
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/variables-input/variables-input.tsx

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function VariablesInput({
102102
const [activeSourceBlockId, setActiveSourceBlockId] = useState<string | null>(null)
103103
const valueInputRefs = useRef<Record<string, HTMLInputElement | HTMLTextAreaElement>>({})
104104
const overlayRefs = useRef<Record<string, HTMLDivElement>>({})
105-
const editorContainerRefs = useRef<Record<string, HTMLDivElement | null>>({})
105+
const editorContainerRefs = useRef<Record<string, React.RefObject<HTMLDivElement | null>>>({})
106106
const [dragHighlight, setDragHighlight] = useState<Record<string, boolean>>({})
107107
const [collapsedAssignments, setCollapsedAssignments] = useState<Record<string, boolean>>({})
108108

@@ -184,6 +184,13 @@ export function VariablesInput({
184184
const updateAssignmentRef = useRef(updateAssignment)
185185
updateAssignmentRef.current = updateAssignment
186186

187+
const getEditorRef = useCallback((assignmentId: string) => {
188+
if (!editorContainerRefs.current[assignmentId]) {
189+
editorContainerRefs.current[assignmentId] = { current: null }
190+
}
191+
return editorContainerRefs.current[assignmentId]
192+
}, [])
193+
187194
const editorValueChangeHandlersRef = useRef<Record<string, (newValue: string) => void>>({})
188195

189196
const getEditorValueChangeHandler = useCallback(
@@ -192,8 +199,8 @@ export function VariablesInput({
192199
editorValueChangeHandlersRef.current[assignmentId] = (newValue: string) => {
193200
updateAssignmentRef.current(assignmentId, { value: newValue })
194201

195-
const container = editorContainerRefs.current[assignmentId]
196-
const textarea = container?.querySelector('textarea')
202+
const editorRef = editorContainerRefs.current[assignmentId]
203+
const textarea = editorRef?.current?.querySelector('textarea')
197204
if (textarea) {
198205
const pos = textarea.selectionStart
199206
setCursorPosition(pos)
@@ -244,8 +251,8 @@ export function VariablesInput({
244251
setTimeout(() => {
245252
const newCursorPos = newValue.length - textAfterCursor.length
246253
if (isCodeEditor) {
247-
const container = editorContainerRefs.current[activeFieldId]
248-
const textarea = container?.querySelector('textarea')
254+
const editorRef = editorContainerRefs.current[activeFieldId]
255+
const textarea = editorRef?.current?.querySelector('textarea')
249256
if (textarea) {
250257
textarea.focus()
251258
textarea.selectionStart = newCursorPos
@@ -337,8 +344,8 @@ export function VariablesInput({
337344
const data = JSON.parse(e.dataTransfer.getData('application/json'))
338345
if (data.type !== 'connectionBlock') return
339346

340-
const container = editorContainerRefs.current[assignmentId]
341-
const textarea = container?.querySelector('textarea')
347+
const editorRef = editorContainerRefs.current[assignmentId]
348+
const textarea = editorRef?.current?.querySelector('textarea')
342349
const assignment = assignments.find((a) => a.id === assignmentId)
343350
const currentValue = assignment?.value || ''
344351
const dropPosition = textarea?.selectionStart ?? currentValue.length
@@ -516,11 +523,7 @@ export function VariablesInput({
516523
</Code.Gutter>
517524
<Code.Content
518525
paddingLeft={`${gutterWidth}px`}
519-
editorRef={
520-
((el: HTMLDivElement | null) => {
521-
editorContainerRefs.current[assignment.id] = el
522-
}) as unknown as React.RefObject<HTMLDivElement | null>
523-
}
526+
editorRef={getEditorRef(assignment.id)}
524527
>
525528
<Code.Placeholder
526529
gutterWidth={gutterWidth}
@@ -552,10 +555,9 @@ export function VariablesInput({
552555
inputRef={
553556
{
554557
current:
555-
(editorContainerRefs.current[
556-
assignment.id
557-
]?.querySelector('textarea') as HTMLTextAreaElement) ??
558-
null,
558+
(getEditorRef(assignment.id).current?.querySelector(
559+
'textarea'
560+
) as HTMLTextAreaElement) ?? null,
559561
} as React.RefObject<HTMLTextAreaElement | HTMLInputElement>
560562
}
561563
/>

0 commit comments

Comments
 (0)