import React, { useState, useEffect } from "react";
import { JoyDoc, validateSchema } from "@joyfill/components";
function DocumentEditor() {
const [doc, setDoc] = useState(null);
const [schemaError, setSchemaError] = useState(null);
useEffect(() => {
if (doc) {
const error = validateSchema(doc);
setSchemaError(error);
}
}, [doc]);
const handleError = (error) => {
if (error.code === "ERROR_SCHEMA_VALIDATION") {
setSchemaError(error);
}
};
return (
<div>
{/* Schema Error Display */}
{schemaError && (
<div
style={{
padding: "20px",
margin: "20px",
border: "2px solid #f44336",
borderRadius: "8px",
backgroundColor: "#ffebee",
}}
>
<h3 style={{ color: "#c62828", margin: "0 0 10px 0" }}>
Schema Validation Error
</h3>
<p style={{ margin: "0 0 10px 0" }}>{schemaError.message}</p>
{schemaError.code === "ERROR_SCHEMA_VERSION" && (
<p style={{ margin: "0 0 10px 0" }}>
<strong>Error:</strong> {schemaError.error}
</p>
)}
<p style={{ margin: "0 0 10px 0" }}>
<strong>Schema Version:</strong> {schemaError.details.schemaVersion}
</p>
<p style={{ margin: "0 0 10px 0" }}>
<strong>SDK Version:</strong> {schemaError.details.sdkVersion}
</p>
{schemaError.errors && (
<details style={{ marginTop: "10px" }}>
<summary style={{ cursor: "pointer", fontWeight: "bold" }}>
Validation Details
</summary>
<pre
style={{
marginTop: "10px",
padding: "10px",
backgroundColor: "#f5f5f5",
borderRadius: "4px",
overflow: "auto",
fontSize: "12px",
}}
>
{JSON.stringify(schemaError.errors, null, 2)}
</pre>
</details>
)}
</div>
)}
<JoyDoc
doc={doc}
mode="edit"
onError={handleError}
features={{
validateSchema: true, // Explicitly enable schema validation
}}
/>
</div>
);
}
export default DocumentEditor;