Fix formatting

This commit is contained in:
Ola Hungerford
2025-02-27 07:21:04 -07:00
parent 426fb87640
commit 238c22830b
8 changed files with 283 additions and 246 deletions

View File

@@ -11,10 +11,16 @@ interface JsonEditorProps {
error?: string;
}
const JsonEditor = ({ value, onChange, error: externalError }: JsonEditorProps) => {
const JsonEditor = ({
value,
onChange,
error: externalError,
}: JsonEditorProps) => {
const [editorContent, setEditorContent] = useState(value);
const [internalError, setInternalError] = useState<string | undefined>(undefined);
const [internalError, setInternalError] = useState<string | undefined>(
undefined,
);
useEffect(() => {
setEditorContent(value);
}, [value]);
@@ -22,8 +28,8 @@ const JsonEditor = ({ value, onChange, error: externalError }: JsonEditorProps)
const formatJson = (json: string): string => {
try {
// Handle empty arrays and objects specifically
if (json.trim() === '[]') return '[]';
if (json.trim() === '{}') return '{}';
if (json.trim() === "[]") return "[]";
if (json.trim() === "{}") return "{}";
return JSON.stringify(JSON.parse(json), null, 2);
} catch {
return json;
@@ -52,17 +58,15 @@ const JsonEditor = ({ value, onChange, error: externalError }: JsonEditorProps)
return (
<div className="relative space-y-2">
<div className="flex justify-end">
<Button
variant="outline"
size="sm"
onClick={handleFormatJson}
>
<Button variant="outline" size="sm" onClick={handleFormatJson}>
Format JSON
</Button>
</div>
<div
className={`border rounded-md ${
displayError ? "border-red-500" : "border-gray-200 dark:border-gray-800"
displayError
? "border-red-500"
: "border-gray-200 dark:border-gray-800"
}`}
>
<Editor
@@ -81,7 +85,9 @@ const JsonEditor = ({ value, onChange, error: externalError }: JsonEditorProps)
className="w-full"
/>
</div>
{displayError && <p className="text-sm text-red-500 mt-1">{displayError}</p>}
{displayError && (
<p className="text-sm text-red-500 mt-1">{displayError}</p>
)}
</div>
);
};