Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { useCallback, useEffect, useMemo, useState } from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import {
Badge,
Button,
Expand Down Expand Up @@ -223,6 +223,7 @@ interface VariablesProps {
}

export function Variables({ readOnly = false }: VariablesProps) {
const pendingFocusIdRef = useRef<string | null>(null)
const activeWorkflowId = useWorkflowRegistry((s) => s.activeWorkflowId)

const { isOpen, position, width, height, setIsOpen, setPosition, setDimensions } =
Expand Down Expand Up @@ -342,14 +343,22 @@ export function Variables({ readOnly = false }: VariablesProps) {

const handleAddVariable = useCallback(() => {
if (!activeWorkflowId || readOnly) return
collaborativeAddVariable({
pendingFocusIdRef.current = collaborativeAddVariable({
name: '',
type: 'plain',
value: '',
workflowId: activeWorkflowId,
})
}, [activeWorkflowId, collaborativeAddVariable, readOnly])

// The new row's input only exists after the next render, so focus it from its ref callback.
const focusIfPending = useCallback((variableId: string, element: HTMLInputElement | null) => {
if (!element || pendingFocusIdRef.current !== variableId) return
pendingFocusIdRef.current = null
element.focus()
element.select()
}, [])

const handleRemoveVariable = useCallback(
(variableId: string) => {
if (readOnly) return
Expand Down Expand Up @@ -522,6 +531,7 @@ export function Variables({ readOnly = false }: VariablesProps) {
<div className='flex flex-col gap-1'>
<Label className='text-small'>{STRINGS.labels.name}</Label>
<Input
ref={(element) => focusIfPending(variable.id, element)}
name='name'
autoComplete='off'
value={localNames[variable.id] ?? variable.name}
Expand Down