diff --git a/app/w/[id]/components/workflow-loop/components/loop-input/loop-input.tsx b/app/w/[id]/components/workflow-loop/components/loop-input/loop-input.tsx index 42d2bac844..8783f1e3cf 100644 --- a/app/w/[id]/components/workflow-loop/components/loop-input/loop-input.tsx +++ b/app/w/[id]/components/workflow-loop/components/loop-input/loop-input.tsx @@ -1,21 +1,88 @@ +import { useState } from 'react' +import { ChevronDown } from 'lucide-react' import { NodeProps } from 'reactflow' import { Badge } from '@/components/ui/badge' +import { Input } from '@/components/ui/input' +import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' +import { cn } from '@/lib/utils' import { useWorkflowStore } from '@/stores/workflow/store' export function LoopInput({ id }: NodeProps) { - // Extract the loop ID from the node ID (removes 'loop-input-' prefix) + // Extract the loop ID from the node ID const loopId = id.replace('loop-input-', '') // Get the max iterations from the store for this loop const maxIterations = useWorkflowStore((state) => state.loops[loopId]?.maxIterations ?? 5) + const updateLoopMaxIterations = useWorkflowStore((state) => state.updateLoopMaxIterations) + + // Local state for input value + const [inputValue, setInputValue] = useState(maxIterations.toString()) + const [open, setOpen] = useState(false) + + const handleChange = (e: React.ChangeEvent) => { + const sanitizedValue = e.target.value.replace(/[^0-9]/g, '') + const numValue = parseInt(sanitizedValue) + + // Only update if it's a valid number and <= 50 + if (!isNaN(numValue)) { + setInputValue(Math.min(50, numValue).toString()) + } else { + setInputValue(sanitizedValue) + } + } + + const handleSave = () => { + const value = parseInt(inputValue) + if (!isNaN(value)) { + const newValue = Math.min(50, value) + updateLoopMaxIterations(loopId, newValue) + // Sync input with store value + setInputValue(newValue.toString()) + } else { + // Reset to current store value if invalid + setInputValue(maxIterations.toString()) + } + } + + const handleKeyDown = (e: React.KeyboardEvent) => { + if (e.key === 'Enter') { + e.preventDefault() + handleSave() + setOpen(false) + } + } return ( - - Max Iterations: {maxIterations} - + + e.stopPropagation()}> + + Max Iterations: {maxIterations} + + + + e.stopPropagation()}> +
+
Max Iterations
+
+ +
+
Enter a number between 1 and 50
+
+
+
) } diff --git a/app/w/[id]/components/workflow-loop/workflow-loop.tsx b/app/w/[id]/components/workflow-loop/workflow-loop.tsx index 2ac4bcf526..5cb1f63d36 100644 --- a/app/w/[id]/components/workflow-loop/workflow-loop.tsx +++ b/app/w/[id]/components/workflow-loop/workflow-loop.tsx @@ -1,3 +1,4 @@ +import { useWorkflowStore } from '@/stores/workflow/store' import { Loop } from '@/stores/workflow/types' interface WorkflowLoopProps { @@ -22,7 +23,8 @@ function createLoopLabelNode(loopId: string, bounds: { x: number; y: number }) { // Helper function to create loop input node function createLoopInputNode(loopId: string, bounds: { x: number; width: number }) { - const BADGE_WIDTH = 128 + const loop = useWorkflowStore.getState().loops[loopId] + const BADGE_WIDTH = loop?.maxIterations > 9 ? 153 : 144 return { id: `loop-input-${loopId}`,