forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigEditor.tsx
More file actions
65 lines (58 loc) · 1.67 KB
/
ConfigEditor.tsx
File metadata and controls
65 lines (58 loc) · 1.67 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
'use client';
import { useState, useEffect } from 'react';
import { Textarea } from '@/components/ui/textarea';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { useTranslation } from '@/hooks/useTranslation';
interface ConfigEditorProps {
value: string;
onSave: (value: string) => void;
label?: string;
}
export function ConfigEditor({ value, onSave, label }: ConfigEditorProps) {
const { t } = useTranslation();
const [text, setText] = useState(value);
const [error, setError] = useState<string | null>(null);
useEffect(() => { setText(value); }, [value]);
function handleSave() {
try {
JSON.parse(text);
setError(null);
onSave(text);
} catch (e) {
setError(e instanceof Error ? e.message : 'Invalid JSON');
}
}
function handleFormat() {
try {
const parsed = JSON.parse(text);
setText(JSON.stringify(parsed, null, 2));
setError(null);
} catch (e) {
setError(e instanceof Error ? e.message : 'Invalid JSON');
}
}
return (
<div className="space-y-2">
{label && <Label>{label}</Label>}
<Textarea
value={text}
onChange={(e) => {
setText(e.target.value);
setError(null);
}}
className="font-mono text-sm min-h-[200px]"
placeholder="{}"
/>
{error && <p className="text-xs text-destructive">{error}</p>}
<div className="flex gap-2">
<Button size="sm" onClick={handleSave}>
{t('common.save')}
</Button>
<Button size="sm" variant="outline" onClick={handleFormat}>
Format
</Button>
</div>
</div>
);
}