import { Suspense, lazy, useEffect, useState } from "react"; import { twMerge } from "tailwind-merge"; import type { CodeEditorProps } from "./CodeEditor"; import { useDebouncedCallback } from "@mantine/hooks"; const CodeEditor = lazy(() => import("./CodeEditor")); export type JsonEditorProps = Omit & { value?: object; onChange?: (value: object) => void; emptyAs?: any; onInvalid?: (error: Error) => void; }; export function JsonEditor({ editable, className, value, onChange, onBlur, emptyAs = undefined, onInvalid, ...props }: JsonEditorProps) { const [editorValue, setEditorValue] = useState( value ? JSON.stringify(value, null, 2) : emptyAs, ); const [error, setError] = useState(false); const handleChange = useDebouncedCallback((given: string) => { try { setError(false); onChange?.(given ? JSON.parse(given) : emptyAs); } catch (e) { onInvalid?.(e as Error); setError(true); } }, 250); const handleBlur = (e) => { try { const formatted = JSON.stringify(value, null, 2); setEditorValue(formatted); } catch (e) {} onBlur?.(e); }; useEffect(() => { if (!editorValue) { setEditorValue(value ? JSON.stringify(value, null, 2) : emptyAs); } }, [value]); return ( ); }