'use client' import { useCallback, ChangeEvent } from 'react' import { Handle, Position, NodeProps, useReactFlow } from 'reactflow' type ChoiceOption = { id: string label: string } type ChoiceNodeData = { prompt: string options: ChoiceOption[] } export default function ChoiceNode({ id, data }: NodeProps) { const { setNodes } = useReactFlow() const updatePrompt = useCallback( (e: ChangeEvent) => { setNodes((nodes) => nodes.map((node) => node.id === id ? { ...node, data: { ...node.data, prompt: e.target.value } } : node ) ) }, [id, setNodes] ) const updateOptionLabel = useCallback( (optionId: string, label: string) => { setNodes((nodes) => nodes.map((node) => node.id === id ? { ...node, data: { ...node.data, options: node.data.options.map((opt: ChoiceOption) => opt.id === optionId ? { ...opt, label } : opt ), }, } : node ) ) }, [id, setNodes] ) return (
Choice
{data.options.map((option, index) => (
updateOptionLabel(option.id, e.target.value)} placeholder={`Option ${index + 1}`} className="w-full rounded border border-green-300 bg-white px-2 py-1 pr-3 text-sm focus:border-green-500 focus:outline-none focus:ring-1 focus:ring-green-500 dark:border-green-600 dark:bg-zinc-800 dark:text-white dark:placeholder-zinc-400" />
))}
) }