-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNodePropertiesPanel.tsx
More file actions
213 lines (194 loc) · 8.74 KB
/
NodePropertiesPanel.tsx
File metadata and controls
213 lines (194 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* NodePropertiesPanel - Side panel for editing a selected node's properties.
*
* Displays the node type, category, editable instance name, and all fields
* with literal/variable mode toggles. Special handling exists for
* SetBlackboard and DeclareVariable `output_key` fields (variable dropdown
* or bracket-wrapped text input respectively).
*/
import React, { useState } from 'react';
import { X } from 'lucide-react';
import { AppNode, NodeField, Variable } from '../types';
import './NodePropertiesPanel.css';
// ---------------------------------------------------------------------------
// Props
// ---------------------------------------------------------------------------
interface NodePropertiesPanelProps {
node: AppNode;
variables: Variable[];
onUpdateField: (nodeId: string, fieldName: string, value: string | number | boolean, valueType: 'literal' | 'variable') => void;
onUpdateName?: (nodeId: string, name: string) => void;
onClose: () => void;
}
const NodePropertiesPanel: React.FC<NodePropertiesPanelProps> = ({
node,
variables,
onUpdateField,
onUpdateName,
onClose
}) => {
const [fieldEdits, setFieldEdits] = useState<Record<string, { value: string | number | boolean; valueType: 'literal' | 'variable' }>>({});
const [nodeName, setNodeName] = useState<string>(node.data?.nodeName || '');
const handleFieldChange = (fieldName: string, value: string | number | boolean) => {
const currentEdit = fieldEdits[fieldName] || { valueType: 'literal' };
const updated = { ...currentEdit, value };
setFieldEdits({ ...fieldEdits, [fieldName]: updated });
onUpdateField(node.id, fieldName, value, currentEdit.valueType);
};
const handleValueTypeChange = (fieldName: string, valueType: 'literal' | 'variable') => {
const currentEdit = fieldEdits[fieldName] || { value: '' };
const updated = { ...currentEdit, valueType };
setFieldEdits({ ...fieldEdits, [fieldName]: updated });
onUpdateField(node.id, fieldName, currentEdit.value, valueType);
};
const handleNameChange = (newName: string) => {
setNodeName(newName);
if (onUpdateName) {
onUpdateName(node.id, newName);
}
};
const fields = node.data?.fields || [];
return (
<div className="node-properties-panel">
<div className="panel-header">
<h3>Node Properties</h3>
<button onClick={onClose} className="close-btn">
<X size={18} />
</button>
</div>
<div className="panel-content">
<div className="node-info">
<div className="info-row">
<span className="info-label">Type:</span>
<span className="info-value">{node.data?.name || 'Unknown'}</span>
</div>
<div className="info-row">
<span className="info-label">Category:</span>
<span className="info-value">{node.data?.category || 'Unknown'}</span>
</div>
</div>
<div className="name-section">
<h4>Node Name</h4>
<input
type="text"
value={nodeName}
onChange={(e) => handleNameChange(e.target.value)}
placeholder="No name"
className="field-input"
/>
</div>
{fields.length > 0 && (
<div className="fields-section">
<h4>Fields</h4>
{fields.map((field: NodeField, idx: number) => {
const currentEdit = fieldEdits[field.name] || {
value: field.value,
valueType: field.valueType || 'literal'
};
// Special handling for output_key in SetBlackboard and DeclareVariable
const isSetBlackboard = node.data?.type === 'SetBlackboard';
const isDeclareVariable = node.data?.type === 'DeclareVariable';
const isOutputKey = field.name === 'output_key';
const portDirection = field.portDirection;
// For display purposes, strip brackets if they exist in the value
const displayValue = typeof currentEdit.value === 'string'
? currentEdit.value.replace(/^\{|\}$/g, '')
: currentEdit.value;
// Port direction label for subtree fields
const portLabel = portDirection === 'input' ? '[IN] ' : portDirection === 'output' ? '[OUT] ' : '';
return (
<div key={idx} className={`field-editor ${portDirection ? `port-field-${portDirection}` : ''}`}>
<label className="field-label">
{portDirection && <span className={`port-badge ${portDirection}`}>{portLabel}</span>}
{field.name}
</label>
<div className="field-description">{field.description}</div>
{/* Hide value type selector for output_key in SetBlackboard and DeclareVariable */}
{!(isOutputKey && (isSetBlackboard || isDeclareVariable)) && (
<div className="value-type-selector">
<button
className={`type-btn ${currentEdit.valueType === 'literal' ? 'active' : ''}`}
onClick={() => handleValueTypeChange(field.name, 'literal')}
>
Literal
</button>
<button
className={`type-btn ${currentEdit.valueType === 'variable' ? 'active' : ''}`}
onClick={() => handleValueTypeChange(field.name, 'variable')}
>
Variable
</button>
</div>
)}
{/* SetBlackboard output_key: dropdown of existing variables */}
{isSetBlackboard && isOutputKey ? (
<select
value={displayValue.toString()}
onChange={(e) => handleFieldChange(field.name, `{${e.target.value}}`)}
className="field-input variable-select"
>
<option value="">Select variable...</option>
{variables.map(v => (
<option key={v.name} value={v.name}>
{`{${v.name}}`}
</option>
))}
</select>
) : isDeclareVariable && isOutputKey ? (
/* DeclareVariable output_key: text input with bracket display */
<div className="blackboard-input-wrapper">
<span className="bracket-prefix">{'{'}</span>
<input
type="text"
value={displayValue.toString()}
onChange={(e) => handleFieldChange(field.name, `{${e.target.value}}`)}
className="field-input blackboard-input"
placeholder="variable_name"
/>
<span className="bracket-suffix">{'}'}</span>
</div>
) : currentEdit.valueType === 'literal' ? (
field.type === 'boolean' ? (
<select
value={currentEdit.value.toString()}
onChange={(e) => handleFieldChange(field.name, e.target.value === 'true')}
className="field-input"
>
<option value="true">true</option>
<option value="false">false</option>
</select>
) : (
<input
type={field.type === 'number' ? 'number' : 'text'}
value={currentEdit.value.toString()}
onChange={(e) => {
const val = field.type === 'number' ? parseFloat(e.target.value) || 0 : e.target.value;
handleFieldChange(field.name, val);
}}
className="field-input"
/>
)
) : (
<select
value={currentEdit.value.toString()}
onChange={(e) => handleFieldChange(field.name, e.target.value)}
className="field-input variable-select"
>
<option value="">Select variable...</option>
{variables.map(v => (
<option key={v.name} value={v.name}>
{v.name}
</option>
))}
</select>
)}
</div>
);
})}
</div>
)}
</div>
</div>
);
};
export default NodePropertiesPanel;