fumadocs-openapi 12.1.0 → 12.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/css/generated/shared.css +1 -1
  2. package/dist/.translations/index.d.ts +63 -17
  3. package/dist/.translations/keys.js +63 -17
  4. package/dist/operation.d.ts +6 -1
  5. package/dist/operation.js +2 -4
  6. package/dist/packages/shared-api/dist/components/select.js +8 -7
  7. package/dist/playground/auth.d.ts +13 -3
  8. package/dist/playground/auth.js +58 -46
  9. package/dist/playground/fetcher.d.ts +8 -0
  10. package/dist/playground/fetcher.js +30 -1
  11. package/dist/playground/index.d.ts +2 -1
  12. package/dist/playground/index.js +2 -1
  13. package/dist/playground/use-playground.d.ts +62 -0
  14. package/dist/playground/use-playground.js +140 -0
  15. package/dist/ui/components/endpoint.js +37 -0
  16. package/dist/ui/operation/index.js +6 -14
  17. package/dist/ui/playground/client.d.ts +9 -26
  18. package/dist/ui/playground/client.js +150 -390
  19. package/dist/ui/playground/components/fields.js +693 -0
  20. package/dist/ui/playground/components/oauth-panel.js +182 -0
  21. package/dist/ui/playground/components/request-panel.js +480 -0
  22. package/dist/ui/playground/components/response-panel.js +150 -0
  23. package/dist/ui/playground/components/result-display.d.ts +1 -0
  24. package/dist/ui/playground/components/result-display.js +187 -147
  25. package/dist/ui/playground/components/segmented.js +21 -0
  26. package/dist/ui/playground/components/server-select.js +100 -89
  27. package/dist/ui/playground/components/url-bar.js +96 -0
  28. package/dist/ui/playground/status-info.js +32 -45
  29. package/dist/utils/document/load.js +1 -1
  30. package/package.json +10 -10
  31. package/dist/packages/shared-api/dist/components/collapsible.d.ts +0 -7
  32. package/dist/packages/shared-api/dist/components/collapsible.js +0 -16
  33. package/dist/packages/shared-api/dist/components/dialog.js +0 -55
  34. package/dist/packages/shared-api/dist/components/input.js +0 -14
  35. package/dist/packages/shared-api/dist/components/label.js +0 -11
  36. package/dist/packages/shared-api/dist/components/playground/inputs.js +0 -439
  37. package/dist/ui/playground/components/oauth-dialog.js +0 -259
@@ -0,0 +1,693 @@
1
+ "use client";
2
+ import { Select as Select$1, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../packages/shared-api/dist/components/select.js";
3
+ import { Markdown } from "../../components/markdown.js";
4
+ import { cn } from "../../../utils/cn.js";
5
+ import { anyFields, useFieldInfo, useResolvedSchema, useSchemaContext, useSchemaUtils } from "../../../packages/shared-api/dist/components/playground/schema.js";
6
+ import { Fragment, useId, useMemo, useState } from "react";
7
+ import { dereference, matches, matchesType, mergeAllOf, stringify } from "@fumadocs/json-schema";
8
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
9
+ import { useArray, useDataEngine, useFieldValue, useObject } from "@fumari/stf";
10
+ import { isPlainObject, stringifyFieldKey } from "@fumari/stf/lib/utils";
11
+ import { useTranslations } from "@fuma-translate/react";
12
+ import { Popover } from "@base-ui/react/popover";
13
+ import { Check, ChevronRight, ChevronsUpDown, Info, Plus, Trash2, Upload, X } from "lucide-react";
14
+ import { Select } from "@base-ui/react/select";
15
+ import { buttonVariants } from "fumadocs-ui/components/ui/button";
16
+ //#region src/ui/playground/components/fields.tsx
17
+ /** the type of a field, at the end of its cell like a unit */
18
+ const typeClassName = "flex shrink-0 items-center pe-1 font-mono text-xs text-fd-muted-foreground";
19
+ /** holds the actions of field, reserved in tables so their types align, an end padding when stacked */
20
+ const slotClassName = "flex w-7 shrink-0 items-center justify-center @max-sm:pe-2 @max-sm:empty:w-2";
21
+ const rowButtonClassName = "flex h-10 w-full items-center gap-2 px-4 text-[0.8125rem] text-fd-muted-foreground transition-colors hover:bg-fd-accent/40 hover:text-fd-accent-foreground focus-visible:bg-fd-secondary focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-inset focus-visible:ring-fd-ring/60";
22
+ function getType(field) {
23
+ if (typeof field.type === "string") return field.type;
24
+ if (field.properties || field.patternProperties || field.additionalProperties) return "object";
25
+ if (field.items) return "array";
26
+ }
27
+ function typeLabel(field, alias = false) {
28
+ return field.const !== void 0 ? JSON.stringify(field.const) : stringify(field, { alias });
29
+ }
30
+ /**
31
+ * Resolve the unions and multiple types of a field from its value, like `useFieldInfo()` does initially.
32
+ */
33
+ function resolveField(schema, value, depth = 0) {
34
+ let field = dereference(schema);
35
+ if (typeof field !== "object") return;
36
+ if (field.allOf) field = mergeAllOf(field);
37
+ const union = field.anyOf ?? field.oneOf;
38
+ if (union && union.length > 0 && depth < 4) {
39
+ const idx = union.findIndex((item) => typeof item === "object" && matches(item, value));
40
+ return resolveField(union[idx === -1 ? 0 : idx], value, depth + 1);
41
+ }
42
+ if (Array.isArray(field.type)) return {
43
+ ...field,
44
+ type: field.type.find((item) => matchesType(value, item)) ?? field.type[0]
45
+ };
46
+ return field;
47
+ }
48
+ /** the candidates that open in their own panel, the siblings of a nested field */
49
+ function getNestedFields(engine, candidates) {
50
+ const out = [];
51
+ for (const item of candidates) {
52
+ const field = resolveField(item.schema, engine.get(item.fieldName));
53
+ const type = field && getType(field);
54
+ if (type === "object" || type === "array") out.push(item);
55
+ }
56
+ return out;
57
+ }
58
+ function joinNodes(a, b) {
59
+ if (a && b) return /* @__PURE__ */ jsxs(Fragment$1, { children: [a, b] });
60
+ return a ?? b;
61
+ }
62
+ /**
63
+ * Resolve the unions and multiple types of a schema to the selected one, with the selectors to change it.
64
+ */
65
+ function SchemaSwitch({ fieldName, schema, depth = 0, children }) {
66
+ const field = useResolvedSchema(schema);
67
+ const { info, updateInfo } = useFieldInfo(fieldName, field, depth);
68
+ const union = info.unionField ? field[info.unionField] : void 0;
69
+ const types = Array.isArray(field.type) ? field.type : void 0;
70
+ const member = useMemo(() => {
71
+ if (union && union.length > 0) return union[info.oneOf];
72
+ if (types) return {
73
+ ...field,
74
+ type: info.selectedType ?? types[0]
75
+ };
76
+ }, [
77
+ field,
78
+ union,
79
+ types,
80
+ info.oneOf,
81
+ info.selectedType
82
+ ]);
83
+ if (member === void 0) return children(field, null);
84
+ let selector = null;
85
+ if (union && union.length > 1) selector = /* @__PURE__ */ jsx(TypeSelect, {
86
+ value: String(info.oneOf),
87
+ options: union.map((item, i) => ({
88
+ value: String(i),
89
+ label: stringify(item, { alias: true })
90
+ })),
91
+ onChange: (v) => updateInfo({ oneOf: Number(v) })
92
+ });
93
+ else if (types && types.length > 1) selector = /* @__PURE__ */ jsx(TypeSelect, {
94
+ value: info.selectedType ?? types[0],
95
+ options: types.map((type) => ({
96
+ value: type,
97
+ label: type
98
+ })),
99
+ onChange: (v) => updateInfo({ selectedType: v })
100
+ });
101
+ return /* @__PURE__ */ jsx(SchemaSwitch, {
102
+ fieldName,
103
+ schema: member,
104
+ depth: depth + 1,
105
+ children: (resolved, inner) => children(resolved, joinNodes(selector, inner))
106
+ });
107
+ }
108
+ function TypeSelect({ value, options, onChange }) {
109
+ const t = useTranslations({ note: "playground" });
110
+ return /* @__PURE__ */ jsxs(Select$1, {
111
+ items: options,
112
+ value,
113
+ onValueChange: (v) => v !== null && onChange(v),
114
+ children: [/* @__PURE__ */ jsxs(Select.Trigger, {
115
+ "aria-label": t("Type"),
116
+ className: "flex h-5 max-w-40 min-w-0 cursor-pointer items-center gap-0.5 rounded-md border bg-fd-secondary ps-1.5 pe-1 font-mono text-xs text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring/50 data-popup-open:bg-fd-accent data-popup-open:text-fd-accent-foreground",
117
+ children: [/* @__PURE__ */ jsx(SelectValue, { className: "truncate" }), /* @__PURE__ */ jsx(Select.Icon, {
118
+ className: "shrink-0",
119
+ children: /* @__PURE__ */ jsx(ChevronsUpDown, { className: "size-3" })
120
+ })]
121
+ }), /* @__PURE__ */ jsx(SelectContent, { children: options.map((option) => /* @__PURE__ */ jsx(SelectItem, {
122
+ value: option.value,
123
+ className: "font-mono text-xs",
124
+ children: option.label
125
+ }, option.value)) })]
126
+ });
127
+ }
128
+ /** a field in the editor: its name on the left, its input filling the cell on the right */
129
+ function Row({ name, required = false, info, htmlFor, readOnly = false, children }) {
130
+ return /* @__PURE__ */ jsxs("div", {
131
+ className: "group/row grid border-b last:border-b-0 @max-sm:pb-3 @sm:min-h-10 @sm:grid-cols-[minmax(9rem,1fr)_minmax(0,2fr)]",
132
+ children: [/* @__PURE__ */ jsxs("div", {
133
+ className: "flex min-w-0 items-center gap-1 py-2 ps-4 pe-3 @max-sm:pb-1.5",
134
+ children: [
135
+ /* @__PURE__ */ jsx("label", {
136
+ htmlFor,
137
+ title: name,
138
+ className: "truncate font-mono text-[0.8125rem] text-fd-foreground",
139
+ children: name
140
+ }),
141
+ required && /* @__PURE__ */ jsx("span", {
142
+ className: "-ms-0.5 text-red-400",
143
+ children: "*"
144
+ }),
145
+ info
146
+ ]
147
+ }), /* @__PURE__ */ jsx("div", {
148
+ className: cn("flex min-h-9 min-w-0 items-stretch text-[0.8125rem] transition-colors @max-sm:mx-4 @max-sm:overflow-hidden @max-sm:rounded-lg @max-sm:border @max-sm:bg-fd-secondary/50 @sm:min-h-10 @sm:border-s", !readOnly && "hover:bg-fd-accent/40 has-focus-visible:bg-fd-secondary has-focus-visible:ring-1 has-focus-visible:ring-fd-ring/60 has-focus-visible:ring-inset"),
149
+ children
150
+ })]
151
+ });
152
+ }
153
+ function formatRange(value, min, exclusiveMin, max, exclusiveMax) {
154
+ const out = [];
155
+ if (min !== void 0) out.push(`${min} <=`);
156
+ else if (exclusiveMin !== void 0) out.push(`${exclusiveMin} <`);
157
+ out.push(value);
158
+ if (max !== void 0) out.push(`<= ${max}`);
159
+ else if (exclusiveMax !== void 0) out.push(`< ${exclusiveMax}`);
160
+ if (out.length > 1) return out.join(" ");
161
+ }
162
+ /** the description and constraints of a field, opened from an info button on hover or click */
163
+ function FieldInfo({ name, field, description }) {
164
+ const t = useTranslations({ note: "schema UI" });
165
+ const tPlayground = useTranslations({ note: "playground" });
166
+ const constraints = [];
167
+ if (field.default !== void 0) constraints.push([t("Default"), JSON.stringify(field.default)]);
168
+ let range = formatRange("value", field.minimum, field.exclusiveMinimum, field.maximum, field.exclusiveMaximum);
169
+ if (range) constraints.push([t("Range"), range]);
170
+ range = formatRange("length", field.minLength, void 0, field.maxLength, void 0);
171
+ if (range) constraints.push([t("Length"), range]);
172
+ range = formatRange("items", field.minItems, void 0, field.maxItems, void 0);
173
+ if (range) constraints.push([t("Items"), range]);
174
+ if (field.format && field.format !== "binary") constraints.push([t("Format"), field.format]);
175
+ if (field.pattern) constraints.push([t("Match"), field.pattern]);
176
+ if (field.examples && field.examples.length > 0) constraints.push([t("Example"), JSON.stringify(field.examples[0])]);
177
+ if (!description && constraints.length === 0) return null;
178
+ return /* @__PURE__ */ jsxs(Popover.Root, { children: [/* @__PURE__ */ jsx(Popover.Trigger, {
179
+ openOnHover: true,
180
+ delay: 200,
181
+ closeDelay: 100,
182
+ "aria-label": tPlayground("Field Info"),
183
+ className: "inline-flex size-5 shrink-0 ms-auto items-center justify-center rounded text-fd-muted-foreground/70 transition-colors hover:text-fd-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring data-popup-open:text-fd-foreground",
184
+ children: /* @__PURE__ */ jsx(Info, { className: "size-3.5" })
185
+ }), /* @__PURE__ */ jsx(Popover.Portal, { children: /* @__PURE__ */ jsx(Popover.Positioner, {
186
+ side: "bottom",
187
+ align: "start",
188
+ sideOffset: 6,
189
+ className: "z-50",
190
+ children: /* @__PURE__ */ jsxs(Popover.Popup, {
191
+ className: "w-72 max-w-[calc(100vw-2rem)] origin-(--transform-origin) rounded-xl border bg-fd-popover p-3 text-fd-popover-foreground shadow-lg outline-none transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-95 data-ending-style:opacity-0 data-starting-style:scale-95 data-starting-style:opacity-0 motion-reduce:transition-none",
192
+ children: [
193
+ /* @__PURE__ */ jsxs("p", {
194
+ className: "flex items-baseline gap-3",
195
+ children: [/* @__PURE__ */ jsx("span", {
196
+ className: "truncate font-mono text-[0.8125rem] font-medium",
197
+ children: name
198
+ }), /* @__PURE__ */ jsx("span", {
199
+ className: "ms-auto shrink-0 font-mono text-xs text-fd-muted-foreground",
200
+ children: typeLabel(field)
201
+ })]
202
+ }),
203
+ description && /* @__PURE__ */ jsx("div", {
204
+ className: "mt-1.5 text-xs leading-relaxed text-fd-muted-foreground prose-no-margin [&_a]:underline [&_code]:font-mono",
205
+ children: /* @__PURE__ */ jsx(Markdown, { md: description })
206
+ }),
207
+ constraints.length > 0 && /* @__PURE__ */ jsx("dl", {
208
+ className: "mt-2.5 grid grid-cols-[auto_minmax(0,1fr)] gap-x-3 gap-y-1 border-t pt-2.5 text-xs",
209
+ children: constraints.map(([label, value]) => /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx("dt", {
210
+ className: "text-fd-muted-foreground",
211
+ children: label
212
+ }), /* @__PURE__ */ jsx("dd", {
213
+ className: "truncate font-mono",
214
+ title: value,
215
+ children: value
216
+ })] }, label))
217
+ })
218
+ ]
219
+ })
220
+ }) })] });
221
+ }
222
+ function SlotButton({ label, onClick, children }) {
223
+ return /* @__PURE__ */ jsx("button", {
224
+ type: "button",
225
+ "aria-label": label,
226
+ title: label,
227
+ onClick,
228
+ className: "inline-flex size-5 items-center justify-center rounded text-fd-muted-foreground transition-colors hover:bg-fd-accent hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring [&_svg]:size-3",
229
+ children
230
+ });
231
+ }
232
+ function UnsetButton({ fieldName }) {
233
+ const engine = useDataEngine();
234
+ const t = useTranslations({ note: "playground" });
235
+ const [isDefined] = useFieldValue(fieldName, { compute: (value) => value !== void 0 });
236
+ if (!isDefined) return null;
237
+ return /* @__PURE__ */ jsx(SlotButton, {
238
+ label: t("Unset"),
239
+ onClick: () => engine.delete(fieldName),
240
+ children: /* @__PURE__ */ jsx(X, {})
241
+ });
242
+ }
243
+ function ValueRow({ fieldName, field, type, end, required, marked = required, name, description, slot }) {
244
+ const id = stringifyFieldKey(fieldName);
245
+ return /* @__PURE__ */ jsxs(Row, {
246
+ name,
247
+ required: marked,
248
+ htmlFor: id,
249
+ info: /* @__PURE__ */ jsx(FieldInfo, {
250
+ name,
251
+ field,
252
+ description
253
+ }),
254
+ children: [
255
+ /* @__PURE__ */ jsx(FieldValue, {
256
+ id,
257
+ fieldName,
258
+ field,
259
+ required
260
+ }),
261
+ type && /* @__PURE__ */ jsx("span", {
262
+ className: typeClassName,
263
+ children: type
264
+ }),
265
+ end,
266
+ /* @__PURE__ */ jsx("span", {
267
+ className: slotClassName,
268
+ children: slot
269
+ })
270
+ ]
271
+ });
272
+ }
273
+ /** the type in the cell of a primitive field, its selector when it has multiple */
274
+ function cellType(field, selector) {
275
+ if (selector) return selector;
276
+ if (field.enum === void 0 && field.const === void 0 && field.type !== "null") return typeLabel(field);
277
+ }
278
+ /** the input of a primitive field */
279
+ function FieldValue({ id, fieldName, field, required = false }) {
280
+ const [value, setValue] = useFieldValue(fieldName);
281
+ const t = useTranslations({ note: "playground" });
282
+ if (field.type === "null") return /* @__PURE__ */ jsx("span", {
283
+ className: "flex flex-1 items-center px-3 font-mono text-fd-muted-foreground",
284
+ children: "null"
285
+ });
286
+ if (field.type === "string" && field.format === "binary") return /* @__PURE__ */ jsxs("label", {
287
+ htmlFor: id,
288
+ className: "flex min-w-0 flex-1 cursor-pointer items-center gap-2 px-3",
289
+ children: [
290
+ /* @__PURE__ */ jsx(Upload, { className: "size-3.5 shrink-0 text-fd-muted-foreground" }),
291
+ value instanceof File ? /* @__PURE__ */ jsx("span", {
292
+ className: "truncate",
293
+ children: value.name
294
+ }) : /* @__PURE__ */ jsx("span", {
295
+ className: "text-fd-muted-foreground",
296
+ children: t("Choose a file")
297
+ }),
298
+ /* @__PURE__ */ jsx("input", {
299
+ id,
300
+ type: "file",
301
+ className: "sr-only",
302
+ onChange: (e) => {
303
+ const file = e.target.files?.item(0);
304
+ if (file) setValue(file);
305
+ }
306
+ })
307
+ ]
308
+ });
309
+ const options = field.const !== void 0 ? [field.const] : field.enum;
310
+ if (options && options.length > 0) {
311
+ const idx = options.indexOf(value);
312
+ const items = options.map((item, i) => ({
313
+ value: String(i),
314
+ label: typeof item === "string" ? item : JSON.stringify(item)
315
+ }));
316
+ if (!required) items.push({
317
+ value: "-1",
318
+ label: /* @__PURE__ */ jsx("span", {
319
+ className: "text-fd-muted-foreground",
320
+ children: t("Unset")
321
+ })
322
+ });
323
+ return /* @__PURE__ */ jsx(CellSelect, {
324
+ id,
325
+ items,
326
+ value: idx !== -1 ? String(idx) : required ? null : "-1",
327
+ onValueChange: (v) => setValue(v === "-1" ? void 0 : options[Number(v)]),
328
+ placeholder: t("Select")
329
+ });
330
+ }
331
+ if (field.type === "boolean") return /* @__PURE__ */ jsx("div", {
332
+ className: "flex flex-1 items-center gap-0.5 px-2",
333
+ children: [true, false].map((option) => /* @__PURE__ */ jsx("button", {
334
+ type: "button",
335
+ "aria-pressed": value === option,
336
+ onClick: () => setValue(option),
337
+ className: "h-6 rounded px-2 font-mono text-xs text-fd-muted-foreground transition-colors hover:text-fd-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fd-ring aria-pressed:bg-fd-background aria-pressed:text-fd-foreground aria-pressed:shadow-sm aria-pressed:ring-1 aria-pressed:ring-fd-border dark:aria-pressed:bg-fd-accent",
338
+ children: String(option)
339
+ }, String(option)))
340
+ });
341
+ const isNumber = field.type === "integer" || field.type === "number";
342
+ const hint = field.default ?? field.examples?.[0];
343
+ return /* @__PURE__ */ jsx("input", {
344
+ id,
345
+ type: isNumber ? "number" : field.format === "password" ? "password" : "text",
346
+ step: isNumber ? "any" : void 0,
347
+ autoComplete: "off",
348
+ spellCheck: false,
349
+ placeholder: value === "" ? "\"\"" : hint !== void 0 && typeof hint !== "object" ? String(hint) : t("Enter value"),
350
+ value: value == null ? "" : String(value),
351
+ onChange: (e) => {
352
+ if (!isNumber) setValue(e.target.value);
353
+ else setValue(Number.isNaN(e.target.valueAsNumber) ? void 0 : e.target.valueAsNumber);
354
+ },
355
+ className: "min-w-0 flex-1 bg-transparent px-3 text-fd-foreground outline-none placeholder:text-fd-muted-foreground/70"
356
+ });
357
+ }
358
+ /** an object or array, opened in its own panel */
359
+ function NavRow({ fieldName, field, type, required, marked = required, name, description, onNavigate }) {
360
+ const t = useTranslations({ note: "playground" });
361
+ const id = stringifyFieldKey(fieldName);
362
+ const [preview] = useFieldValue(fieldName, { compute(value) {
363
+ if (Array.isArray(value)) {
364
+ if (value.length === 0) return "[]";
365
+ return value.length === 1 ? t("1 item") : t("{count} items", { variables: { count: String(value.length) } });
366
+ }
367
+ if (!isPlainObject(value)) return null;
368
+ const keys = Object.keys(value);
369
+ if (keys.length === 0) return "{}";
370
+ return `{ ${keys.slice(0, 3).join(", ")}${keys.length > 3 ? ", …" : ""} }`;
371
+ } });
372
+ return /* @__PURE__ */ jsx(Row, {
373
+ name,
374
+ required: marked,
375
+ htmlFor: id,
376
+ info: /* @__PURE__ */ jsx(FieldInfo, {
377
+ name,
378
+ field,
379
+ description
380
+ }),
381
+ children: /* @__PURE__ */ jsx(NavButton, {
382
+ id,
383
+ type,
384
+ onClick: onNavigate,
385
+ children: /* @__PURE__ */ jsx("span", {
386
+ className: cn("font-mono text-xs", preview === null ? "text-fd-muted-foreground/70" : "text-fd-muted-foreground"),
387
+ children: preview ?? t("Not set")
388
+ })
389
+ })
390
+ });
391
+ }
392
+ /** fills a cell, opens something else on click */
393
+ function NavButton({ id, type, onClick, children }) {
394
+ return /* @__PURE__ */ jsxs("button", {
395
+ id,
396
+ type: "button",
397
+ onClick,
398
+ className: "group/nav flex min-w-0 flex-1 items-center ps-3 text-start outline-none",
399
+ children: [
400
+ /* @__PURE__ */ jsx("span", {
401
+ className: "min-w-0 flex-1 truncate pe-2",
402
+ children
403
+ }),
404
+ type && /* @__PURE__ */ jsx("span", {
405
+ className: typeClassName,
406
+ children: type
407
+ }),
408
+ /* @__PURE__ */ jsx("span", {
409
+ className: slotClassName,
410
+ children: /* @__PURE__ */ jsx(ChevronRight, { className: "size-3.5 text-fd-muted-foreground transition-transform duration-200 group-hover/nav:translate-x-0.5 motion-reduce:transition-none" })
411
+ })
412
+ ]
413
+ });
414
+ }
415
+ /** a select filling a cell */
416
+ function CellSelect({ id, items, value, onValueChange, placeholder }) {
417
+ return /* @__PURE__ */ jsxs(Select$1, {
418
+ items,
419
+ value,
420
+ onValueChange: (v) => v !== null && onValueChange(v),
421
+ children: [/* @__PURE__ */ jsx(SelectTrigger, {
422
+ id,
423
+ className: "h-auto min-w-0 flex-1 rounded-none border-0 bg-transparent py-0 ps-3 pe-1 text-[0.8125rem] hover:bg-transparent focus:ring-0 focus-visible:outline-none",
424
+ children: /* @__PURE__ */ jsx(SelectValue, {
425
+ className: "truncate",
426
+ placeholder
427
+ })
428
+ }), /* @__PURE__ */ jsx(SelectContent, { children: items.map((item) => /* @__PURE__ */ jsx(SelectItem, {
429
+ value: item.value,
430
+ className: "text-[0.8125rem]",
431
+ children: item.description ? /* @__PURE__ */ jsxs("span", {
432
+ className: "flex flex-col",
433
+ children: [item.label, /* @__PURE__ */ jsx("span", {
434
+ className: "text-xs text-fd-muted-foreground",
435
+ children: item.description
436
+ })]
437
+ }) : item.label
438
+ }, item.value)) })]
439
+ });
440
+ }
441
+ function SelectRow({ name, ...props }) {
442
+ const id = useId();
443
+ return /* @__PURE__ */ jsxs(Row, {
444
+ name,
445
+ htmlFor: id,
446
+ children: [/* @__PURE__ */ jsx(CellSelect, {
447
+ id,
448
+ ...props
449
+ }), /* @__PURE__ */ jsx("span", { className: slotClassName })]
450
+ });
451
+ }
452
+ /** a row of read-only text */
453
+ function TextRow({ name, children }) {
454
+ return /* @__PURE__ */ jsxs(Row, {
455
+ name,
456
+ readOnly: true,
457
+ children: [/* @__PURE__ */ jsx("span", {
458
+ className: "flex min-w-0 flex-1 items-center px-3 text-fd-muted-foreground",
459
+ children: /* @__PURE__ */ jsx("span", {
460
+ className: "truncate",
461
+ children
462
+ })
463
+ }), /* @__PURE__ */ jsx("span", { className: slotClassName })]
464
+ });
465
+ }
466
+ /** a row opening something else, like a panel */
467
+ function LinkRow({ name, onClick, children }) {
468
+ const id = useId();
469
+ return /* @__PURE__ */ jsx(Row, {
470
+ name,
471
+ htmlFor: id,
472
+ children: /* @__PURE__ */ jsx(NavButton, {
473
+ id,
474
+ onClick,
475
+ children: /* @__PURE__ */ jsx("span", {
476
+ className: "text-fd-muted-foreground",
477
+ children
478
+ })
479
+ })
480
+ });
481
+ }
482
+ /** a row toggled on click, its check box takes the slot */
483
+ function CheckRow({ name, checked, onCheckedChange, children }) {
484
+ const id = useId();
485
+ return /* @__PURE__ */ jsx(Row, {
486
+ name,
487
+ htmlFor: id,
488
+ children: /* @__PURE__ */ jsxs("button", {
489
+ id,
490
+ type: "button",
491
+ role: "checkbox",
492
+ "aria-checked": checked,
493
+ onClick: () => onCheckedChange(!checked),
494
+ className: "flex min-w-0 flex-1 items-center ps-3 text-start outline-none",
495
+ children: [/* @__PURE__ */ jsx("span", {
496
+ className: "min-w-0 flex-1 truncate pe-2 text-fd-muted-foreground",
497
+ children
498
+ }), /* @__PURE__ */ jsx("span", {
499
+ className: slotClassName,
500
+ children: /* @__PURE__ */ jsx("span", {
501
+ className: cn("flex size-4 items-center justify-center rounded-[5px] border transition-colors", checked ? "border-fd-primary bg-fd-primary text-fd-primary-foreground" : "border-fd-muted-foreground/50"),
502
+ children: checked && /* @__PURE__ */ jsx(Check, { className: "size-3" })
503
+ })
504
+ })]
505
+ })
506
+ });
507
+ }
508
+ function getRemoval(required, removable) {
509
+ if (removable) return "remove";
510
+ if (!required) return "unset";
511
+ }
512
+ function FieldRow({ entry: { name, fieldName, schema }, required = false, description, onNavigate, onRemove }) {
513
+ const { readOnly, writeOnly } = useSchemaContext();
514
+ const resolved = useResolvedSchema(schema);
515
+ const t = useTranslations({ note: "playground" });
516
+ if (schema === false) return null;
517
+ return /* @__PURE__ */ jsx(SchemaSwitch, {
518
+ fieldName,
519
+ schema,
520
+ children: (field, selector) => {
521
+ if (field.readOnly && !readOnly || field.writeOnly && !writeOnly) return null;
522
+ const props = {
523
+ name,
524
+ required,
525
+ marked: required && !onRemove,
526
+ description: description ?? resolved.description ?? field.description,
527
+ fieldName,
528
+ field,
529
+ slot: onRemove ? /* @__PURE__ */ jsx(SlotButton, {
530
+ label: t("Remove Item", { note: "aria-label" }),
531
+ onClick: onRemove,
532
+ children: /* @__PURE__ */ jsx(Trash2, {})
533
+ }) : !required && /* @__PURE__ */ jsx(UnsetButton, { fieldName })
534
+ };
535
+ const type = getType(field);
536
+ if (type === "object" || type === "array") return /* @__PURE__ */ jsx(NavRow, {
537
+ ...props,
538
+ type: typeLabel(field, true),
539
+ onNavigate
540
+ });
541
+ return /* @__PURE__ */ jsx(ValueRow, {
542
+ ...props,
543
+ type: cellType(field, selector)
544
+ });
545
+ }
546
+ });
547
+ }
548
+ function ObjectRows({ fieldName, field, onNavigate }) {
549
+ const t = useTranslations({ note: "playground" });
550
+ const { generateDefault } = useSchemaUtils();
551
+ const schemaKeys = field.properties ? Object.keys(field.properties) : [];
552
+ const { patternProperties = {}, additionalProperties, "x-playground-lazy": isLazy = schemaKeys.length > 100 } = field;
553
+ const isDynamic = Object.keys(patternProperties).length > 0 || Boolean(additionalProperties);
554
+ const { properties, onAppend, onDelete, _objectKeys } = useObject(fieldName, {
555
+ lazy: isLazy,
556
+ defaultValue: () => generateDefault(field),
557
+ properties: field.properties ?? {},
558
+ fallback: additionalProperties,
559
+ patternProperties
560
+ });
561
+ const hiddenKeys = isLazy ? schemaKeys.filter((key) => !_objectKeys.includes(key)) : [];
562
+ const entries = properties.map((child) => ({
563
+ fieldName: child.field,
564
+ name: child.key,
565
+ schema: child.info,
566
+ removal: getRemoval(field.required?.includes(child.key) ?? false, child.kind !== "fixed")
567
+ }));
568
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [
569
+ properties.map((child, i) => /* @__PURE__ */ jsx(FieldRow, {
570
+ entry: entries[i],
571
+ required: field.required?.includes(child.key),
572
+ onNavigate: () => onNavigate(entries[i], entries),
573
+ onRemove: child.kind === "fixed" ? void 0 : () => onDelete(child.key)
574
+ }, child.key)),
575
+ hiddenKeys.length > 0 && /* @__PURE__ */ jsxs(Select$1, {
576
+ value: null,
577
+ onValueChange: (v) => v !== null && onAppend(v),
578
+ children: [/* @__PURE__ */ jsxs(SelectTrigger, {
579
+ className: cn(rowButtonClassName, "rounded-none border-0 border-b bg-transparent p-0 ps-4 last:border-b-0 focus:ring-0"),
580
+ children: [/* @__PURE__ */ jsx(Plus, { className: "size-3.5" }), /* @__PURE__ */ jsx(SelectValue, { placeholder: t("Show Property") })]
581
+ }), /* @__PURE__ */ jsx(SelectContent, { children: hiddenKeys.map((key) => /* @__PURE__ */ jsx(SelectItem, {
582
+ value: key,
583
+ className: "font-mono text-[0.8125rem]",
584
+ children: key
585
+ }, key)) })]
586
+ }),
587
+ isDynamic && /* @__PURE__ */ jsx(NewPropertyRow, { onAppend })
588
+ ] });
589
+ }
590
+ function NewPropertyRow({ onAppend }) {
591
+ const t = useTranslations({ note: "playground" });
592
+ const [name, setName] = useState("");
593
+ function add() {
594
+ onAppend(name);
595
+ setName("");
596
+ }
597
+ return /* @__PURE__ */ jsxs("div", {
598
+ className: "flex min-h-10 items-center gap-2 border-b ps-4 pe-1.5 last:border-b-0",
599
+ children: [
600
+ /* @__PURE__ */ jsx(Plus, { className: "size-3.5 shrink-0 text-fd-muted-foreground" }),
601
+ /* @__PURE__ */ jsx("input", {
602
+ value: name,
603
+ "aria-label": t("Property Name"),
604
+ placeholder: t("Enter Property Name"),
605
+ onChange: (e) => setName(e.target.value),
606
+ onKeyDown: (e) => {
607
+ if (e.key !== "Enter") return;
608
+ e.preventDefault();
609
+ add();
610
+ },
611
+ className: "h-10 min-w-0 flex-1 bg-transparent font-mono text-[0.8125rem] outline-none placeholder:text-fd-muted-foreground/70"
612
+ }),
613
+ /* @__PURE__ */ jsx("button", {
614
+ type: "button",
615
+ disabled: name.trim().length === 0,
616
+ onClick: add,
617
+ className: cn(buttonVariants({
618
+ variant: "secondary",
619
+ size: "sm"
620
+ }), "px-2.5"),
621
+ children: t("Add")
622
+ })
623
+ ]
624
+ });
625
+ }
626
+ function ArrayRows({ fieldName, field, onNavigate }) {
627
+ const t = useTranslations({ note: "playground" });
628
+ const { generateDefault } = useSchemaUtils();
629
+ const { items, insertItem, removeItem } = useArray(fieldName);
630
+ const itemSchema = field.items ?? anyFields;
631
+ const entries = items.map((item) => ({
632
+ fieldName: item.field,
633
+ name: `[${item.index}]`,
634
+ schema: itemSchema,
635
+ removal: "remove"
636
+ }));
637
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [entries.map((entry, i) => /* @__PURE__ */ jsx(FieldRow, {
638
+ entry,
639
+ required: true,
640
+ onNavigate: () => onNavigate(entry, entries),
641
+ onRemove: () => removeItem(i)
642
+ }, i)), /* @__PURE__ */ jsxs("button", {
643
+ type: "button",
644
+ onClick: () => insertItem(generateDefault(itemSchema)),
645
+ className: rowButtonClassName,
646
+ children: [/* @__PURE__ */ jsx(Plus, { className: "size-3.5" }), t("Add Item")]
647
+ })] });
648
+ }
649
+ /** the rows of a field: properties for objects, items for arrays, an input otherwise */
650
+ function FieldRows({ entry, onNavigate, showDescription = false }) {
651
+ const t = useTranslations({ note: "playground" });
652
+ return /* @__PURE__ */ jsx(SchemaSwitch, {
653
+ fieldName: entry.fieldName,
654
+ schema: entry.schema,
655
+ children: (field, selector) => {
656
+ const type = getType(field);
657
+ let rows;
658
+ if (type === "object") rows = /* @__PURE__ */ jsx(ObjectRows, {
659
+ fieldName: entry.fieldName,
660
+ field,
661
+ onNavigate
662
+ });
663
+ else if (type === "array") rows = /* @__PURE__ */ jsx(ArrayRows, {
664
+ fieldName: entry.fieldName,
665
+ field,
666
+ onNavigate
667
+ });
668
+ else rows = /* @__PURE__ */ jsx(ValueRow, {
669
+ name: entry.name,
670
+ type: cellType(field, null),
671
+ required: true,
672
+ fieldName: entry.fieldName,
673
+ field
674
+ });
675
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [
676
+ showDescription && field.description && /* @__PURE__ */ jsx("div", {
677
+ className: "border-b px-4 py-2.5 text-xs text-fd-muted-foreground [&_a]:underline",
678
+ children: /* @__PURE__ */ jsx(Markdown, { md: field.description })
679
+ }),
680
+ selector && /* @__PURE__ */ jsxs("div", {
681
+ className: "flex min-h-10 items-center gap-2 border-b ps-4 pe-4 text-xs text-fd-muted-foreground @sm:pe-8",
682
+ children: [t("Schema"), /* @__PURE__ */ jsx("span", {
683
+ className: "ms-auto",
684
+ children: selector
685
+ })]
686
+ }),
687
+ rows
688
+ ] });
689
+ }
690
+ });
691
+ }
692
+ //#endregion
693
+ export { CellSelect, CheckRow, FieldRow, FieldRows, LinkRow, Row, SelectRow, TextRow, ValueRow, getNestedFields, getRemoval, resolveField };