From 1ad3c183a7b879fdad096e018a7bc8dccbd8782e Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Santos Souza de Miranda Date: Wed, 21 Jan 2026 12:58:20 -0300 Subject: [PATCH] feat: [US-025] - Create custom variable node component Co-Authored-By: Claude Opus 4.5 --- .../editor/[projectId]/FlowchartEditor.tsx | 2 + src/components/editor/nodes/VariableNode.tsx | 103 ++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 src/components/editor/nodes/VariableNode.tsx diff --git a/src/app/editor/[projectId]/FlowchartEditor.tsx b/src/app/editor/[projectId]/FlowchartEditor.tsx index 7031f8b..8567611 100644 --- a/src/app/editor/[projectId]/FlowchartEditor.tsx +++ b/src/app/editor/[projectId]/FlowchartEditor.tsx @@ -20,6 +20,7 @@ import 'reactflow/dist/style.css' import Toolbar from '@/components/editor/Toolbar' import DialogueNode from '@/components/editor/nodes/DialogueNode' import ChoiceNode from '@/components/editor/nodes/ChoiceNode' +import VariableNode from '@/components/editor/nodes/VariableNode' import type { FlowchartData, FlowchartNode, FlowchartEdge } from '@/types/flowchart' type FlowchartEditorProps = { @@ -56,6 +57,7 @@ function FlowchartEditorInner({ initialData }: FlowchartEditorProps) { () => ({ dialogue: DialogueNode, choice: ChoiceNode, + variable: VariableNode, }), [] ) diff --git a/src/components/editor/nodes/VariableNode.tsx b/src/components/editor/nodes/VariableNode.tsx new file mode 100644 index 0000000..ee7ec4b --- /dev/null +++ b/src/components/editor/nodes/VariableNode.tsx @@ -0,0 +1,103 @@ +'use client' + +import { useCallback, ChangeEvent } from 'react' +import { Handle, Position, NodeProps, useReactFlow } from 'reactflow' + +type VariableNodeData = { + variableName: string + operation: 'set' | 'add' | 'subtract' + value: number +} + +export default function VariableNode({ id, data }: NodeProps) { + const { setNodes } = useReactFlow() + + const updateVariableName = useCallback( + (e: ChangeEvent) => { + setNodes((nodes) => + nodes.map((node) => + node.id === id + ? { ...node, data: { ...node.data, variableName: e.target.value } } + : node + ) + ) + }, + [id, setNodes] + ) + + const updateOperation = useCallback( + (e: ChangeEvent) => { + setNodes((nodes) => + nodes.map((node) => + node.id === id + ? { ...node, data: { ...node.data, operation: e.target.value as 'set' | 'add' | 'subtract' } } + : node + ) + ) + }, + [id, setNodes] + ) + + const updateValue = useCallback( + (e: ChangeEvent) => { + const value = parseFloat(e.target.value) || 0 + setNodes((nodes) => + nodes.map((node) => + node.id === id + ? { ...node, data: { ...node.data, value } } + : node + ) + ) + }, + [id, setNodes] + ) + + return ( +
+ + +
+ Variable +
+ + + +
+ + + +
+ + +
+ ) +}