forked from stoplightio/elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.tsx
More file actions
185 lines (163 loc) · 6.86 KB
/
Copy pathModel.tsx
File metadata and controls
185 lines (163 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { JsonSchemaViewer } from '@stoplight/json-schema-viewer';
import { Box, CopyButton, Flex, Heading, HStack, NodeAnnotation, Panel, Select, Text, VStack } from '@stoplight/mosaic';
import { CodeViewer } from '@stoplight/mosaic-code-viewer';
import { withErrorBoundary } from '@stoplight/react-error-boundary';
import cn from 'classnames';
import { JSONSchema7 } from 'json-schema';
import * as React from 'react';
import { useResolvedObject, useSchemaInlineRefResolver } from '../../../context/InlineRefResolver';
import { useOptionsCtx } from '../../../context/Options';
import { useIsCompact } from '../../../hooks/useIsCompact';
import { exceedsSize, generateExamplesFromJsonSchema } from '../../../utils/exampleGeneration/exampleGeneration';
import { getOriginalObject } from '../../../utils/ref-resolving/resolvedObject';
import { LoadMore } from '../../LoadMore';
import { MarkdownViewer } from '../../MarkdownViewer';
import { DocsComponentProps } from '..';
import { DeprecatedBadge, InternalBadge } from '../HttpOperation/Badges';
import LazySchemaTreePreviewer from '../HttpOperation/LazySchemaTreePreviewer';
import { ExportButton } from '../HttpService/ExportButton';
import { NodeVendorExtensions } from '../NodeVendorExtensions';
import { TwoColumnLayout } from '../TwoColumnLayout';
export type ModelProps = DocsComponentProps<JSONSchema7>;
const ModelComponent: React.FC<ModelProps> = ({
data: unresolvedData,
className,
nodeTitle,
layoutOptions,
exportProps,
disableProps,
}) => {
const [resolveRef, maxRefDepth] = useSchemaInlineRefResolver();
const data = useResolvedObject(unresolvedData) as JSONSchema7;
const { nodeHasChanged, renderExtensionAddon } = useOptionsCtx();
const { ref: layoutRef, isCompact } = useIsCompact(layoutOptions);
const nodeId = (data?.['x-stoplight' as keyof JSONSchema7] as { [key: string]: any })?.id;
const title = data.title ?? nodeTitle;
const isDeprecated = !!data['deprecated' as keyof JSONSchema7];
const isInternal = !!data['x-internal' as keyof JSONSchema7];
const shouldDisplayHeader =
!layoutOptions?.noHeading && (title !== undefined || (exportProps && !layoutOptions?.hideExport));
const titleChanged = nodeHasChanged?.({ nodeId, attr: ['title', 'internal'] });
const header = (shouldDisplayHeader || isInternal || isDeprecated) && (
<Flex justifyContent="between" alignItems="center">
<Box pos="relative">
<HStack spacing={5}>
{title && (
<Heading size={1} fontWeight="semibold">
{title}
</Heading>
)}
<HStack spacing={2}>
{isDeprecated && <DeprecatedBadge />}
{isInternal && <InternalBadge />}
</HStack>
</HStack>
<NodeAnnotation change={titleChanged} />
</Box>
{localStorage.getItem('use_new_mask_workflow') === 'true'
? null
: exportProps && !layoutOptions?.hideExport && !isCompact && <ExportButton {...exportProps} />}
</Flex>
);
const modelExamples = !layoutOptions?.hideModelExamples && <ModelExamples data={data} isCollapsible={isCompact} />;
const descriptionChanged = nodeHasChanged?.({ nodeId, attr: 'description' });
const getMaskProperties = (): Array<{ path: string; required?: boolean }> => {
const disablePropsConfig = disableProps?.models;
const absolutePathsToHide: Array<{ path: string; required?: boolean }> = [];
if (disableProps?.models) {
disablePropsConfig.forEach((configEntry: any) => {
const { location, paths } = configEntry;
paths.forEach((item: any) => {
const fullPath = location === '#' ? item?.path : `${location}/${item.path}`;
let object: any = { path: fullPath };
if (item.hasOwnProperty('required')) {
object = { ...object, required: item?.required };
}
absolutePathsToHide.push(object);
});
});
}
return absolutePathsToHide;
};
const description = (
<VStack spacing={10}>
{data.description &&
(data.type === 'object' || (data.allOf || []).some(s => (s as JSONSchema7).type === 'object')) && (
<Box pos="relative">
<MarkdownViewer role="textbox" markdown={data.description} />
<NodeAnnotation change={descriptionChanged} />
</Box>
)}
<NodeVendorExtensions data={data} />
{localStorage.getItem('use_new_mask_workflow') !== 'true' && isCompact && modelExamples}
{data && localStorage.getItem('use_new_mask_workflow') === 'true' ? (
<LazySchemaTreePreviewer schema={data} hideData={getMaskProperties()} />
) : (
<JsonSchemaViewer
resolveRef={resolveRef}
maxRefDepth={maxRefDepth}
schema={getOriginalObject(data)}
nodeHasChanged={nodeHasChanged}
renderExtensionAddon={renderExtensionAddon}
skipTopLevelDescription
/>
)}
</VStack>
);
return (
<TwoColumnLayout
ref={layoutRef}
className={cn('Model', className)}
header={header}
left={description}
right={!isCompact && modelExamples}
/>
);
};
const ModelExamples = React.memo(({ data, isCollapsible = false }: { data: JSONSchema7; isCollapsible?: boolean }) => {
const [chosenExampleIndex, setChosenExampleIndex] = React.useState(0);
const [show, setShow] = React.useState<boolean>(false);
const [loading, setLoading] = React.useState<boolean>(false);
const examples = React.useMemo(() => generateExamplesFromJsonSchema(data), [data]);
const selectedExample = examples[chosenExampleIndex]?.data;
const handleLoadMorePress = React.useCallback(() => {
setLoading(true);
setTimeout(() => setShow(true), 50);
}, []);
const examplesSelect = examples.length > 1 && (
<Select
aria-label="Example"
value={String(chosenExampleIndex)}
options={examples.map(({ label }, index) => ({ value: index, label }))}
onChange={value => setChosenExampleIndex(parseInt(String(value), 10))}
size="sm"
triggerTextPrefix="Example: "
/>
);
return (
<Panel rounded isCollapsible={isCollapsible} defaultIsOpen={!isCollapsible}>
<Panel.Titlebar rightComponent={selectedExample ? <CopyButton size="sm" copyValue={selectedExample} /> : null}>
{examplesSelect || (
<Text color="body" role="heading">
Example
</Text>
)}
</Panel.Titlebar>
<Panel.Content p={0}>
{show || !exceedsSize(selectedExample) ? (
<CodeViewer
aria-label={selectedExample}
noCopyButton
maxHeight="500px"
language="json"
value={selectedExample}
showLineNumbers
/>
) : (
<LoadMore loading={loading} onClick={handleLoadMorePress} />
)}
</Panel.Content>
</Panel>
);
});
export const Model = withErrorBoundary<ModelProps>(ModelComponent, { recoverableProps: ['data'] });