4
createContext,
5
Fragment,
6
use,
7
- useMemo,
7
useState,
8
type ReactNode,
9
} from 'react';
47
required?: boolean;
48
as?: 'property' | 'body';
49
generated: SchemaUIGeneratedData;
50
+ isResponse?: boolean;
51
}
52
53
const DataContext = createContext<SchemaUIGeneratedData | null>(null);
54
+const ResponseContext = createContext(false);
55
56
function useData() {
57
const ctx = use(DataContext);
59
return ctx;
60
}
61
62
+function useIsResponse() {
63
+ return use(ResponseContext);
64
+}
65
+
66
export function CustomSchemaUI({
67
name,
68
required = false,
69
as = 'property',
70
generated,
71
+ isResponse = false,
72
}: SchemaUIProps) {
73
const schema = generated.refs[generated.$root];
68
- const isProperty = as === 'property' || !isExpandable(schema);
74
+ const isProperty = as === 'property' || !isExpandable(schema, generated.refs);
75
76
return (
77
<DataContext value={generated}>
72
- {isProperty ? (
73
- <SchemaProperty
74
- name={name}
75
- $type={generated.$root}
76
- required={required}
77
- isRoot
78
- />
79
- ) : (
80
- <SchemaContent $type={generated.$root} />
81
- )}
78
+ <ResponseContext value={isResponse}>
79
+ {isProperty ? (
80
+ <SchemaProperty
81
+ name={name}
82
+ $type={generated.$root}
83
+ required={required}
84
+ isRoot
85
+ />
86
+ ) : (
87
+ <SchemaContent $type={generated.$root} />
88
+ )}
89
+ </ResponseContext>
90
</DataContext>
91
);
92
}
128
{schema.items.map((item, i) => (
129
<div key={item.$type} className="pl-3 border-l-2 border-fd-border">
130
<span className="text-sm font-medium">{item.name}</span>
123
- {isExpandable(refs[item.$type]) && (
131
+ {isExpandable(refs[item.$type], refs) && (
132
<ExpandableContent $type={item.$type} parentPath={parentPath} />
133
)}
134
</div>
154
isRoot?: boolean;
155
}) {
156
const { refs } = useData();
157
+ const isResponse = useIsResponse();
158
const schema = refs[$type];
159
const fullPath = parentPath ? `${parentPath}.${name}` : name;
160
152
- const hasChildren = isExpandable(schema);
161
+ const hasChildren = isExpandable(schema, refs);
162
const typeDisplay = getTypeDisplay(schema);
163
164
return (
171
<span className="text-sm font-mono text-fd-muted-foreground">
172
{typeDisplay}
173
</span>
165
- {required && (
174
+ {required && !isResponse && (
175
<span className="text-xs text-red-400 font-medium">Required</span>
176
)}
177
{schema.deprecated && (
244
245
return (
246
<Collapsible open={isOpen} onOpenChange={setIsOpen} className="mt-3">
238
- <CollapsibleTrigger className="group flex items-center gap-2 px-3 py-2 text-sm font-medium rounded-lg border border-fd-border bg-fd-card hover:bg-fd-accent/50 transition-colors">
247
+ <CollapsibleTrigger className="group flex items-center gap-1 px-2 py-1 text-xs text-fd-muted-foreground hover:text-fd-foreground font-medium rounded border border-fd-border hover:bg-fd-accent/30 transition-colors">
248
{isOpen ? (
249
<>
241
- <X className="h-4 w-4" />
250
+ <X className="h-3 w-3" />
251
Hide {label}
252
</>
253
) : (
254
<>
246
- <Plus className="h-4 w-4" />
255
+ <Plus className="h-3 w-3" />
256
Show {childCount > 0 ? `${childCount} ` : ''}{label}
257
</>
258
)}
259
</CollapsibleTrigger>
260
<CollapsibleContent>
252
- <div className="mt-2 pl-4 border-l-2 border-fd-border">
261
+ <div className="mt-2 pl-3 border-l border-fd-border">
262
<SchemaContent $type={$type} parentPath={parentPath} />
263
</div>
264
</CollapsibleContent>
266
);
267
}
268
260
-function isExpandable(schema: SchemaData): boolean {
269
+function isExpandable(schema: SchemaData, refs?: Record<string, SchemaData>): boolean {
270
if (schema.type === 'object' && schema.props.length > 0) return true;
262
- if (schema.type === 'array') return true;
263
- if ((schema.type === 'or' || schema.type === 'and') && schema.items.length > 0) return true;
271
+ if (schema.type === 'array') {
272
+ // Only expandable if items have structure (object/nested)
273
+ if (!refs) return true;
274
+ const itemSchema = refs[schema.item.$type];
275
+ if (!itemSchema) return true;
276
+ return itemSchema.type !== 'primitive';
277
+ }
278
+ if ((schema.type === 'or' || schema.type === 'and') && schema.items.length > 0) {
279
+ // Only expandable if at least one variant has nested structure
280
+ if (!refs) return true;
281
+ return schema.items.some((item) => {
282
+ const itemSchema = refs[item.$type];
283
+ if (!itemSchema) return false;
284
+ return isExpandable(itemSchema, refs);
285
+ });
286
+ }
287
return false;
288
}
289