-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfigurationPanel.tsx
More file actions
385 lines (359 loc) · 14.2 KB
/
Copy pathConfigurationPanel.tsx
File metadata and controls
385 lines (359 loc) · 14.2 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { useEffect, useState, useCallback, useRef } from 'react';
import { useShallow } from 'zustand/shallow';
import { Settings, Loader2, RefreshCw, Lock, Save, X, RotateCcw } from 'lucide-react';
import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Badge } from '@/components/ui/badge';
import { useAppStore, type AppState } from '@/lib/store';
import type { Parameter, ParameterType } from '@/lib/types';
import type { SovdResourceEntityType } from '@/lib/types';
interface ConfigurationPanelProps {
entityId: string;
/** Optional parameter name to highlight */
highlightParam?: string;
/** Entity type for API calls */
entityType?: SovdResourceEntityType;
}
/**
* Get badge color for parameter type
*/
function getTypeBadgeVariant(type: ParameterType): 'default' | 'secondary' | 'outline' {
switch (type) {
case 'bool':
return 'default';
case 'int':
case 'double':
return 'secondary';
default:
return 'outline';
}
}
/**
* Parameter row component with inline editing
*/
function ParameterRow({
param,
onSetParameter,
onResetParameter,
isHighlighted,
}: {
param: Parameter;
onSetParameter: (name: string, value: unknown) => Promise<boolean>;
onResetParameter: (name: string) => Promise<boolean>;
isHighlighted?: boolean;
}) {
const [isEditing, setIsEditing] = useState(false);
const [editValue, setEditValue] = useState<string>('');
const [isSaving, setIsSaving] = useState(false);
const [isResetting, setIsResetting] = useState(false);
const startEditing = useCallback(() => {
if (param.read_only) return;
setEditValue(formatValue(param.value, param.type));
setIsEditing(true);
}, [param]);
const cancelEditing = useCallback(() => {
setIsEditing(false);
setEditValue('');
}, []);
const saveValue = useCallback(async () => {
setIsSaving(true);
try {
const parsedValue = parseValue(editValue, param.type);
const success = await onSetParameter(param.name, parsedValue);
if (success) {
setIsEditing(false);
}
} finally {
setIsSaving(false);
}
}, [editValue, param, onSetParameter]);
const resetValue = useCallback(async () => {
if (param.read_only) return;
setIsResetting(true);
try {
await onResetParameter(param.name);
} finally {
setIsResetting(false);
}
}, [param, onResetParameter]);
const handleKeyDown = useCallback(
(e: React.KeyboardEvent) => {
if (e.key === 'Enter') {
saveValue();
} else if (e.key === 'Escape') {
cancelEditing();
}
},
[saveValue, cancelEditing]
);
// Toggle for boolean parameters
const toggleBool = useCallback(async () => {
if (param.read_only || param.type !== 'bool') return;
setIsSaving(true);
try {
await onSetParameter(param.name, !param.value);
} finally {
setIsSaving(false);
}
}, [param, onSetParameter]);
return (
<div
className={`flex items-center gap-3 p-3 rounded-lg border bg-card hover:bg-accent/30 transition-colors ${isHighlighted ? 'ring-2 ring-primary border-primary' : ''}`}
>
{/* Parameter name */}
<div className="flex-1 min-w-0">
<div className="flex items-center gap-2">
<span className="font-mono text-sm truncate">{param.name}</span>
{param.read_only && (
<span title="Read-only">
<Lock className="w-3 h-3 text-muted-foreground" />
</span>
)}
</div>
{param.description && (
<p className="text-xs text-muted-foreground truncate mt-0.5">{param.description}</p>
)}
</div>
{/* Type badge */}
<Badge variant={getTypeBadgeVariant(param.type)} className="shrink-0">
{param.type}
</Badge>
{/* Value display/edit */}
<div className="w-40 shrink-0">
{param.type === 'bool' ? (
// Boolean toggle button
<Button
variant={param.value ? 'default' : 'outline'}
size="sm"
className="w-full"
disabled={param.read_only || isSaving}
onClick={toggleBool}
>
{isSaving ? <Loader2 className="w-4 h-4 animate-spin" /> : param.value ? 'true' : 'false'}
</Button>
) : isEditing ? (
// Editing mode
<div className="flex items-center gap-1">
<Input
value={editValue}
onChange={(e) => setEditValue(e.target.value)}
onKeyDown={handleKeyDown}
className="h-8 text-sm font-mono"
autoFocus
disabled={isSaving}
/>
<Button
variant="ghost"
size="sm"
onClick={saveValue}
disabled={isSaving}
className="h-8 w-8 p-0"
>
{isSaving ? <Loader2 className="w-4 h-4 animate-spin" /> : <Save className="w-4 h-4" />}
</Button>
<Button
variant="ghost"
size="sm"
onClick={cancelEditing}
disabled={isSaving}
className="h-8 w-8 p-0"
>
<X className="w-4 h-4" />
</Button>
</div>
) : (
// Display mode - click to edit
<div
className={`px-3 py-1.5 rounded border text-sm font-mono truncate ${
param.read_only
? 'bg-muted text-muted-foreground cursor-not-allowed'
: 'bg-background cursor-pointer hover:border-primary focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-2'
}`}
onClick={startEditing}
onKeyDown={(e) => {
if (!param.read_only && (e.key === 'Enter' || e.key === ' ')) {
e.preventDefault();
startEditing();
}
}}
role={param.read_only ? undefined : 'button'}
tabIndex={param.read_only ? undefined : 0}
aria-label={
param.read_only
? `${param.name}: ${formatValue(param.value, param.type)} (read-only)`
: `Edit ${param.name}`
}
title={param.read_only ? 'Read-only parameter' : 'Click to edit'}
>
{formatValue(param.value, param.type)}
</div>
)}
</div>
{/* Reset to default button */}
{!param.read_only && (
<Button
variant="ghost"
size="sm"
onClick={resetValue}
disabled={isResetting}
className="h-8 w-8 p-0 shrink-0"
title="Reset to default"
>
{isResetting ? <Loader2 className="w-4 h-4 animate-spin" /> : <RotateCcw className="w-4 h-4" />}
</Button>
)}
</div>
);
}
/**
* Format parameter value for display
*/
function formatValue(value: unknown, type: ParameterType): string {
if (value === null || value === undefined) return '';
if (type.endsWith('_array') || Array.isArray(value)) {
return JSON.stringify(value);
}
return String(value);
}
/**
* Parse string input to appropriate type
*/
function parseValue(input: string, type: ParameterType): unknown {
switch (type) {
case 'bool':
return input.toLowerCase() === 'true';
case 'int':
return parseInt(input, 10);
case 'double':
return parseFloat(input);
case 'string':
return input;
case 'byte_array':
case 'bool_array':
case 'int_array':
case 'double_array':
case 'string_array':
try {
return JSON.parse(input);
} catch {
// Return empty array instead of invalid string to prevent type mismatch
return [];
}
default:
return input;
}
}
export function ConfigurationPanel({ entityId, highlightParam, entityType = 'components' }: ConfigurationPanelProps) {
const {
configurations,
isLoadingConfigurations,
fetchConfigurations,
setParameter,
resetParameter,
resetAllConfigurations,
} = useAppStore(
useShallow((state: AppState) => ({
configurations: state.configurations,
isLoadingConfigurations: state.isLoadingConfigurations,
fetchConfigurations: state.fetchConfigurations,
setParameter: state.setParameter,
resetParameter: state.resetParameter,
resetAllConfigurations: state.resetAllConfigurations,
}))
);
const [isResettingAll, setIsResettingAll] = useState(false);
const parameters = configurations.get(entityId) || [];
const prevEntityIdRef = useRef<string | null>(null);
// Fetch configurations on mount and when entityId changes
useEffect(() => {
// Always fetch if entityId changed, or if not yet loaded
if (prevEntityIdRef.current !== entityId || !configurations.has(entityId)) {
fetchConfigurations(entityId, entityType);
}
prevEntityIdRef.current = entityId;
}, [entityId, entityType, fetchConfigurations, configurations]);
const handleRefresh = useCallback(() => {
fetchConfigurations(entityId, entityType);
}, [entityId, fetchConfigurations, entityType]);
const handleSetParameter = useCallback(
async (name: string, value: unknown) => {
return setParameter(entityId, name, value, entityType);
},
[entityId, setParameter, entityType]
);
const handleResetParameter = useCallback(
async (name: string) => {
return resetParameter(entityId, name, entityType);
},
[entityId, resetParameter, entityType]
);
const handleResetAll = useCallback(async () => {
setIsResettingAll(true);
try {
await resetAllConfigurations(entityId, entityType);
} finally {
setIsResettingAll(false);
}
}, [entityId, resetAllConfigurations, entityType]);
if (isLoadingConfigurations && parameters.length === 0) {
return (
<Card>
<CardContent className="flex items-center justify-center py-8">
<Loader2 className="w-6 h-6 animate-spin text-muted-foreground" />
</CardContent>
</Card>
);
}
return (
<Card>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
<Settings className="w-5 h-5 text-muted-foreground" />
<CardTitle className="text-base">Configurations</CardTitle>
<span className="text-xs text-muted-foreground">({parameters.length} parameters)</span>
</div>
<div className="flex items-center gap-1">
<Button
variant="outline"
size="sm"
onClick={handleResetAll}
disabled={isResettingAll || parameters.length === 0}
title="Reset all parameters to defaults"
>
{isResettingAll ? (
<Loader2 className="w-4 h-4 animate-spin mr-1" />
) : (
<RotateCcw className="w-4 h-4 mr-1" />
)}
Reset All
</Button>
<Button variant="ghost" size="sm" onClick={handleRefresh} disabled={isLoadingConfigurations}>
<RefreshCw className={`w-4 h-4 ${isLoadingConfigurations ? 'animate-spin' : ''}`} />
</Button>
</div>
</div>
</CardHeader>
<CardContent>
{parameters.length === 0 ? (
<div className="text-center text-muted-foreground py-4">
No parameters available for this component.
</div>
) : (
<div className="space-y-2 max-h-[500px] overflow-y-auto pr-1">
{parameters.map((param) => (
<ParameterRow
key={param.name}
param={param}
onSetParameter={handleSetParameter}
onResetParameter={handleResetParameter}
isHighlighted={param.name === highlightParam}
/>
))}
</div>
)}
</CardContent>
</Card>
);
}