import { useState, useEffect } from "react" import { Resolver } from "@stoplight/json-ref-resolver" import { CreateNodes, Collapsible } from "@theme/JSONSchemaViewer/components" import { JSVOptionsContextProvider, SchemaHierarchyContextProvider, } from "@theme/JSONSchemaViewer/contexts" import type { JSX } from "react" import type { JSONSchema } from "@theme/JSONSchemaViewer/types" import type { JSVOptions } from "@theme/JSONSchemaViewer/contexts" import { LoadingLabel, ErrorOccurredLabel, } from "@theme/JSONSchemaViewer/labels" import type { IResolveOpts } from "@stoplight/json-ref-resolver/types" export type Props = { /** * The JSON schema to use */ schema: unknown /** * To customize the ref resolving * By default, only inline references will be dereferenced by @stoplight/json-ref-resolver */ resolverOptions?: IResolveOpts /** * To customize the viewer itself */ viewerOptions?: Omit /** * To customize the styles of the viewer, to override docusaurus styles on a specific page */ className?: string } type InnerViewerProperties = { // Thanks to @stoplight/json-ref-resolver, $ref are either : // 1. resolved // 2. unresolved (as circular stuff are not on the roadmap) schema: JSONSchema // Options for viewer viewerOptions?: Omit // To customize the styles of the viewer, to override docusaurus styles on a specific page className?: string } // Translated labels function ErrorOccurred(props: { error: Error }): JSX.Element { const { error } = props return (
) } // Internal function JSONSchemaInnerViewer(props: InnerViewerProperties): JSX.Element { const { schema, viewerOptions } = props // Title of the schema, for user friendliness const title = typeof schema !== "boolean" && schema.title !== undefined ? schema.title : "Schema" // state for provider const startingState: JSVOptions = { fullSchema: schema, // spread provided attributes ...viewerOptions, } return ( {title}} detailsProps={{ open: true, className: props.className || "json-schema-viewer", }} > ) } // Entry point export default function JSONSchemaViewer(props: Props): JSX.Element { const { schema: originalSchema, resolverOptions, viewerOptions } = props const [error, setError] = useState(undefined as undefined | Error) const [resolvedSchema, setResolvedSchema] = useState( undefined as undefined | JSONSchema, ) useEffect(() => { // Time to do the job new Resolver() .resolve(originalSchema, resolverOptions) .then((result) => { setResolvedSchema(result.result) }) .catch((err) => { setError(err) }) }, []) if (error !== undefined) { return } else if (resolvedSchema === undefined) { return } else { return ( ) } }