import { useState, useEffect } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import JsonEditor from "./JsonEditor"; import { updateValueAtPath, JsonObject } from "@/utils/jsonPathUtils"; import { generateDefaultValue, formatFieldLabel } from "@/utils/schemaUtils"; export type JsonValue = | string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue }; export type JsonSchemaType = { type: "string" | "number" | "integer" | "boolean" | "array" | "object"; description?: string; properties?: Record; items?: JsonSchemaType; }; interface DynamicJsonFormProps { schema: JsonSchemaType; value: JsonValue; onChange: (value: JsonValue) => void; maxDepth?: number; } const DynamicJsonForm = ({ schema, value, onChange, maxDepth = 3, }: DynamicJsonFormProps) => { const [isJsonMode, setIsJsonMode] = useState(false); const [jsonError, setJsonError] = useState(); // Add state for storing raw JSON value const [rawJsonValue, setRawJsonValue] = useState( JSON.stringify(value ?? generateDefaultValue(schema), null, 2) ); // Update rawJsonValue when value prop changes useEffect(() => { if (!isJsonMode) { setRawJsonValue(JSON.stringify(value ?? generateDefaultValue(schema), null, 2)); } }, [value, schema, isJsonMode]); const handleSwitchToFormMode = () => { if (isJsonMode) { // When switching to Form mode, ensure we have valid JSON try { const parsed = JSON.parse(rawJsonValue); // Update the parent component's state with the parsed value onChange(parsed); // Switch to form mode setIsJsonMode(false); } catch (err) { setJsonError(err instanceof Error ? err.message : "Invalid JSON"); } } else { // Update raw JSON value when switching to JSON mode setRawJsonValue(JSON.stringify(value ?? generateDefaultValue(schema), null, 2)); setIsJsonMode(true); } }; const renderFormFields = ( propSchema: JsonSchemaType, currentValue: JsonValue, path: string[] = [], depth: number = 0, ) => { if ( depth >= maxDepth && (propSchema.type === "object" || propSchema.type === "array") ) { // Render as JSON editor when max depth is reached return ( { try { const parsed = JSON.parse(newValue); handleFieldChange(path, parsed); setJsonError(undefined); } catch (err) { setJsonError(err instanceof Error ? err.message : "Invalid JSON"); } }} error={jsonError} /> ); } switch (propSchema.type) { case "string": case "number": case "integer": return ( handleFieldChange( path, propSchema.type === "string" ? e.target.value : Number(e.target.value), ) } placeholder={propSchema.description} /> ); case "boolean": return ( handleFieldChange(path, e.target.checked)} className="w-4 h-4" /> ); case "object": { // Handle case where we have a value but no schema properties const objectValue = currentValue as JsonObject || {}; // If we have schema properties, use them to render fields if (propSchema.properties) { return (
{Object.entries(propSchema.properties).map(([key, prop]) => (
{renderFormFields( prop, objectValue[key], [...path, key], depth + 1, )}
))}
); } // If we have a value but no schema properties, render fields based on the value else if (Object.keys(objectValue).length > 0) { return (
{Object.entries(objectValue).map(([key, value]) => (
handleFieldChange([...path, key], e.target.value) } />
))}
); } // If we have neither schema properties nor value, return null return null; } case "array": { const arrayValue = Array.isArray(currentValue) ? currentValue : []; if (!propSchema.items) return null; return (
{propSchema.description && (

{propSchema.description}

)} {propSchema.items?.description && (

Items: {propSchema.items.description}

)}
{arrayValue.map((item, index) => (
{renderFormFields( propSchema.items as JsonSchemaType, item, [...path, index.toString()], depth + 1, )}
))}
); } default: return null; } }; const handleFieldChange = (path: string[], fieldValue: JsonValue) => { if (path.length === 0) { onChange(fieldValue); return; } try { const newValue = updateValueAtPath(value, path, fieldValue); onChange(newValue); } catch (error) { console.error("Failed to update form value:", error); onChange(value); } }; return (
{isJsonMode ? ( { setRawJsonValue(newValue); try { if (/^\s*[{[].*[}\]]\s*$/.test(newValue)) { const parsed = JSON.parse(newValue); onChange(parsed); setJsonError(undefined); } } catch { // Don't set an error during typing - that will happen when the user // tries to save or submit the form } }} error={jsonError} /> ) : ( // If schema type is object but value is not an object or is empty, and we have actual JSON data, // render a simple representation of the JSON data schema.type === "object" && (typeof value !== "object" || value === null || Object.keys(value).length === 0) && rawJsonValue && rawJsonValue !== "{}" ? (

Form view not available for this JSON structure. Using simplified view:

              {rawJsonValue}
            

Use JSON mode for full editing capabilities.

) : ( renderFormFields(schema, value) ) )}
); }; export default DynamicJsonForm;