|
1 | | -import * as React from 'react'; |
2 | | - |
3 | | -import { safeParse } from '@stoplight/json'; |
4 | | -import { Dictionary, ISchema } from '@stoplight/types'; |
5 | | -import { Box } from '@stoplight/ui-kit'; |
6 | | -import dropRight = require('lodash/dropRight'); |
7 | | -import isEmpty = require('lodash/isEmpty'); |
| 1 | +/* @jsx jsx */ |
8 | 2 |
|
| 3 | +import { jsx } from '@emotion/core'; |
| 4 | +import { Box, IBox } from '@stoplight/ui-kit'; |
| 5 | +import { Component } from 'react'; |
| 6 | +import { ErrorMessage } from './common/ErrorMessage'; |
9 | 7 | import { MutedText } from './common/MutedText'; |
10 | | -import { Row } from './common/Row'; |
11 | | -import { dereferenceSchema } from './dereferenceSchema'; |
12 | | -import { renderSchema } from './renderers/renderSchema'; |
13 | | -import { IProp } from './types'; |
14 | | -import { buildAllOfSchema } from './util/buildAllOfSchema'; |
| 8 | +import { ISchemaView, SchemaView } from './Schema'; |
15 | 9 | import { isSchemaViewerEmpty } from './util/isSchemaViewerEmpty'; |
16 | 10 |
|
17 | | -export interface IJsonSchemaViewer { |
18 | | - name?: string; |
19 | | - dereferencedSchema?: string | ISchema; |
20 | | - defaultExpandedDepth?: number; |
21 | | - schemas: object; |
22 | | - schema: string | ISchema; |
23 | | - limitPropertyCount: number; |
24 | | - hideRoot?: boolean; |
25 | | - expanded?: boolean; |
26 | | - emptyText?: string; |
27 | | - hideInheritedFrom?: boolean; |
28 | | -} |
| 11 | +export interface IJsonSchemaViewer extends ISchemaView, IBox {} |
29 | 12 |
|
30 | 13 | export interface IJsonSchemaViewerState { |
31 | | - showExtra: boolean; |
32 | | - expandedRows: Dictionary<boolean>; |
| 14 | + error: null | string; |
33 | 15 | } |
34 | 16 |
|
35 | | -export class JsonSchemaViewer extends React.Component<IJsonSchemaViewer, IJsonSchemaViewerState> { |
| 17 | +export class JsonSchemaViewer extends Component<IJsonSchemaViewer, IJsonSchemaViewerState> { |
36 | 18 | public state = { |
37 | | - showExtra: false, |
38 | | - expandedRows: { |
39 | | - all: false, |
40 | | - }, |
| 19 | + error: null, |
41 | 20 | }; |
42 | 21 |
|
| 22 | + // there is no error hook yet, see https://reactjs.org/docs/hooks-faq.html#how-do-lifecycle-methods-correspond-to-hooks |
| 23 | + public static getDerivedStateFromError(error: Error): { error: IJsonSchemaViewerState['error'] } { |
| 24 | + return { error: error.message }; |
| 25 | + } |
| 26 | + |
43 | 27 | public render() { |
44 | 28 | const { |
45 | | - name, |
46 | | - schema, |
47 | | - dereferencedSchema, |
48 | | - schemas = {}, |
49 | | - limitPropertyCount, |
50 | | - hideRoot, |
51 | | - expanded = false, |
52 | | - defaultExpandedDepth = 1, |
53 | | - emptyText, |
54 | | - hideInheritedFrom = false, |
55 | | - } = this.props; |
56 | | - |
57 | | - const emptyElem = <MutedText className="u-none">{emptyText || 'No schema defined.'}</MutedText>; |
58 | | - |
59 | | - // an empty array or object is still a valid response, schema is ONLY really empty when a combiner type has no information |
60 | | - if (isSchemaViewerEmpty(schema)) { |
61 | | - return <div>{emptyElem}</div>; |
62 | | - } |
63 | | - |
64 | | - let parsed: IProp; |
65 | | - if (typeof (dereferencedSchema || schema) === 'string') { |
66 | | - parsed = safeParse((dereferencedSchema || schema) as string) as IProp; |
67 | | - } else { |
68 | | - parsed = (dereferencedSchema || schema) as IProp; |
69 | | - } |
70 | | - |
71 | | - try { |
72 | | - if (!dereferencedSchema || isEmpty(dereferencedSchema)) { |
73 | | - parsed = dereferenceSchema(parsed, { definitions: schemas }, hideInheritedFrom); |
74 | | - } |
75 | | - } catch (e) { |
76 | | - console.error('JsonSchemaViewer dereference error', e); |
77 | | - return ( |
78 | | - <Box as="p" p={3} className="u-error"> |
79 | | - There is an error in your {name} schema definition. |
80 | | - </Box> |
81 | | - ); |
82 | | - } |
83 | | - |
84 | | - if (!parsed || !Object.keys(parsed).length || (parsed.properties && !Object.keys(parsed.properties).length)) { |
85 | | - return emptyElem; |
86 | | - } |
87 | | - |
88 | | - let rowElems: any[]; |
89 | | - |
90 | | - const { expandedRows } = this.state; |
91 | | - expandedRows.all = expanded; |
92 | | - |
93 | | - try { |
94 | | - // resolve root allOfs, simplifies things later |
95 | | - if (parsed.allOf) { |
96 | | - const elems = parsed.allOf; |
97 | | - |
98 | | - if (parsed.type) elems.push({ type: parsed.type }); |
99 | | - |
100 | | - parsed = buildAllOfSchema({ elems }); |
101 | | - } |
102 | | - |
103 | | - rowElems = renderSchema({ |
| 29 | + props: { |
| 30 | + name, |
| 31 | + schema, |
| 32 | + dereferencedSchema, |
104 | 33 | schemas, |
105 | | - expandedRows, |
| 34 | + limitPropertyCount, |
| 35 | + hideRoot, |
| 36 | + expanded, |
106 | 37 | defaultExpandedDepth, |
107 | | - schema: parsed, |
108 | | - level: hideRoot && (parsed.type === 'object' || parsed.hasOwnProperty('allOf')) ? -1 : 0, |
109 | | - name: 'root', |
110 | | - rowElems: [], |
111 | | - toggleExpandRow: this.toggleExpandRow, |
112 | 38 | hideInheritedFrom, |
113 | | - jsonPath: 'root', |
114 | | - hideRoot, |
115 | | - }); |
116 | | - } catch (e) { |
117 | | - console.error('JSV:error', e); |
118 | | - rowElems = [<Row className="u-error">{`Error rendering schema. ${e}`}</Row>]; |
119 | | - } |
120 | | - |
121 | | - const { showExtra } = this.state; |
122 | | - const propOverflowCount = rowElems.length - limitPropertyCount; |
123 | | - |
124 | | - if (limitPropertyCount) { |
125 | | - if (!showExtra && propOverflowCount > 0) { |
126 | | - rowElems = dropRight(rowElems, propOverflowCount); |
127 | | - } |
| 39 | + ...props |
| 40 | + }, |
| 41 | + state: { error }, |
| 42 | + } = this; |
| 43 | + |
| 44 | + if (error) { |
| 45 | + // todo: handle these: |
| 46 | + /* |
| 47 | + <Box as="p" p={3} className="u-error"> |
| 48 | + There is an error in your {name} schema definition. |
| 49 | + </Box> |
| 50 | + */ |
| 51 | + |
| 52 | + /*<Row className="u-error">{`Error rendering schema. ${e}`}</Row>]*/ |
| 53 | + return <ErrorMessage>{error}</ErrorMessage>; |
128 | 54 | } |
129 | 55 |
|
130 | | - if (isEmpty(rowElems)) { |
131 | | - return emptyElem; |
| 56 | + // an empty array or object is still a valid response, schema is ONLY really empty when a combiner type has no information |
| 57 | + if (isSchemaViewerEmpty(schema)) { |
| 58 | + return <MutedText>No schema defined</MutedText>; |
132 | 59 | } |
133 | 60 |
|
134 | 61 | return ( |
135 | | - <div className="JSV us-t u-schemaColors"> |
136 | | - {rowElems} |
137 | | - |
138 | | - {showExtra || propOverflowCount > 0 ? ( |
139 | | - <div onClick={this.toggleShowExtra}> |
140 | | - {/* className={cn('JSV-toggleExtra', { 'is-on': showExtra })} */} |
141 | | - {showExtra ? 'collapse' : `...show ${propOverflowCount} more properties`} |
142 | | - </div> |
143 | | - ) : null} |
144 | | - </div> |
| 62 | + <Box {...props}> |
| 63 | + {( |
| 64 | + <SchemaView |
| 65 | + defaultExpandedDepth={defaultExpandedDepth} |
| 66 | + dereferencedSchema={dereferencedSchema} |
| 67 | + expanded={expanded} |
| 68 | + hideInheritedFrom={hideInheritedFrom} |
| 69 | + hideRoot={hideRoot} |
| 70 | + limitPropertyCount={limitPropertyCount} |
| 71 | + name={name} |
| 72 | + schema={schema} |
| 73 | + schemas={schemas} |
| 74 | + /> |
| 75 | + ) || <MutedText>No schema defined</MutedText>} |
| 76 | + </Box> |
145 | 77 | ); |
146 | 78 | } |
147 | | - |
148 | | - public toggleShowExtra = () => { |
149 | | - const { showExtra } = this.state; |
150 | | - this.setState({ showExtra: !showExtra }); |
151 | | - }; |
152 | | - |
153 | | - public toggleExpandRow = (rowKey: string, expanded: boolean) => { |
154 | | - const { expandedRows } = this.state; |
155 | | - const update = {}; |
156 | | - update[rowKey] = expanded; |
157 | | - this.setState({ expandedRows: Object.assign({}, expandedRows, update) }); |
158 | | - }; |
159 | 79 | } |
0 commit comments