diff --git a/src/components/editor/nodes/ChoiceNode.tsx b/src/components/editor/nodes/ChoiceNode.tsx index e9d2d8c..47f54e4 100644 --- a/src/components/editor/nodes/ChoiceNode.tsx +++ b/src/components/editor/nodes/ChoiceNode.tsx @@ -23,8 +23,11 @@ const MAX_OPTIONS = 6 export default function ChoiceNode({ id, data }: NodeProps) { const { setNodes } = useReactFlow() + const { variables } = useEditorContext() // Puxa as variáveis globais para validar condições const [editingConditionOptionId, setEditingConditionOptionId] = useState(null) + // --- Handlers de Atualização --- + const updatePrompt = useCallback( (e: ChangeEvent) => { setNodes((nodes) => @@ -59,26 +62,43 @@ export default function ChoiceNode({ id, data }: NodeProps) { [id, setNodes] ) - const updateOptionCondition = useCallback( - (optionId: string, condition: Condition | undefined) => { - setNodes((nodes) => - nodes.map((node) => - node.id === id - ? { - ...node, - data: { - ...node.data, - options: node.data.options.map((opt: ChoiceOption) => - opt.id === optionId ? { ...opt, condition } : opt - ), - }, - } - : node - ) + const handleSaveCondition = useCallback((optionId: string, condition: Condition) => { + setNodes((nodes) => + nodes.map((node) => + node.id === id + ? { + ...node, + data: { + ...node.data, + options: node.data.options.map((opt: ChoiceOption) => + opt.id === optionId ? { ...opt, condition } : opt + ), + }, + } + : node ) - }, - [id, setNodes] - ) + ) + setEditingConditionOptionId(null) + }, [id, setNodes]) + + const handleRemoveCondition = useCallback((optionId: string) => { + setNodes((nodes) => + nodes.map((node) => + node.id === id + ? { + ...node, + data: { + ...node.data, + options: node.data.options.map((opt: ChoiceOption) => + opt.id === optionId ? { ...opt, condition: undefined } : opt + ), + }, + } + : node + ) + ) + setEditingConditionOptionId(null) + }, [id, setNodes]) const addOption = useCallback(() => { if (data.options.length >= MAX_OPTIONS) return @@ -89,10 +109,7 @@ export default function ChoiceNode({ id, data }: NodeProps) { ...node, data: { ...node.data, - options: [ - ...node.data.options, - { id: nanoid(), label: '' }, - ], + options: [...node.data.options, { id: nanoid(), label: '' }], }, } : node @@ -110,9 +127,7 @@ export default function ChoiceNode({ id, data }: NodeProps) { ...node, data: { ...node.data, - options: node.data.options.filter( - (opt: ChoiceOption) => opt.id !== optionId - ), + options: node.data.options.filter((opt: ChoiceOption) => opt.id !== optionId), }, } : node @@ -122,6 +137,8 @@ export default function ChoiceNode({ id, data }: NodeProps) { [id, data.options.length, setNodes] ) + // --- Auxiliares --- + const editingOption = useMemo(() => { if (!editingConditionOptionId) return null return data.options.find((opt) => opt.id === editingConditionOptionId) || null @@ -136,186 +153,102 @@ export default function ChoiceNode({ id, data }: NodeProps) { ) return ( - <> -
- - -
- Choice -
- - - -
- {data.options.map((option, index) => ( -
-
- updateOptionLabel(option.id, e.target.value)} - placeholder={`Option ${index + 1}`} - className="flex-1 rounded border border-green-300 bg-white px-2 py-1 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" - /> - - - -
- {option.condition?.variableId && ( -
- if {option.condition.variableName} {option.condition.operator} {String(option.condition.value)} -
- )} -
- ))} -
- - +
+ + +
+ Choice Node
-
+
{data.options.map((option, index) => ( -
-
+
+
updateOptionLabel(option.id, e.target.value)} placeholder={`Option ${index + 1}`} - className="flex-1 rounded border border-green-300 bg-white px-2 py-1 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" + className="flex-1 rounded border border-zinc-200 px-2 py-1 text-xs focus:outline-none focus:ring-1 focus:ring-green-500 dark:border-zinc-700 dark:bg-zinc-800" /> + + {/* Botão de Condição */} + -
+ + {/* Visualização da Condição */} {option.condition && ( -
- - if {option.condition.variableName} {option.condition.operator} {option.condition.value} - +
+ IF: {option.condition.variableName} {option.condition.operator} {option.condition.value} + {hasInvalidConditionReference(option) && " (Variable Missing!)"}
)} + +
))}
+ {/* Modal de Edição de Condição */} {editingOption && ( setEditingConditionOptionId(null)} + onChange={(cond) => { + if (cond) { + handleSaveCondition(editingOption.id, cond) + } else { + handleRemoveCondition(editingOption.id) + } + }} + onClose={() => setEditingConditionOptionId(null)} /> )}
) -} +} \ No newline at end of file