medusa-product-helper 0.0.3 → 0.0.5
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.
- package/.medusa/server/src/admin/index.js +1007 -1
- package/.medusa/server/src/admin/index.mjs +1007 -1
- package/.medusa/server/src/api/admin/product-metadata-config/route.js +21 -0
- package/.medusa/server/src/config/product-helper-options.js +33 -12
- package/.medusa/server/src/shared/product-metadata/utils.js +5 -1
- package/.medusa/server/src/utils/query-builders/product-filters.js +4 -2
- package/README.md +76 -73
- package/package.json +1 -1
|
@@ -1,5 +1,1011 @@
|
|
|
1
|
+
import { jsxs, jsx, Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { defineWidgetConfig } from "@medusajs/admin-sdk";
|
|
3
|
+
import { Container, Heading, Badge, Text, Skeleton, InlineTip, Button, Switch, Textarea, Input, toast } from "@medusajs/ui";
|
|
4
|
+
import { useState, useEffect, useMemo, useRef } from "react";
|
|
5
|
+
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
|
6
|
+
const METADATA_FIELD_TYPES = ["number", "text", "file", "bool"];
|
|
7
|
+
const VALID_FIELD_TYPES = new Set(METADATA_FIELD_TYPES);
|
|
8
|
+
function normalizeMetadataDescriptors(input) {
|
|
9
|
+
if (!Array.isArray(input)) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
const seenKeys = /* @__PURE__ */ new Set();
|
|
13
|
+
const normalized = [];
|
|
14
|
+
for (const item of input) {
|
|
15
|
+
if (!item || typeof item !== "object") {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const key = getNormalizedKey(item.key);
|
|
19
|
+
if (!key || seenKeys.has(key)) {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const type = getNormalizedType(item.type);
|
|
23
|
+
if (!type) {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const label = getNormalizedLabel(item.label);
|
|
27
|
+
const filterable = typeof item.filterable === "boolean" ? item.filterable : Boolean(item.filterable);
|
|
28
|
+
normalized.push({
|
|
29
|
+
key,
|
|
30
|
+
type,
|
|
31
|
+
...label ? { label } : {},
|
|
32
|
+
...filterable ? { filterable: true } : {}
|
|
33
|
+
});
|
|
34
|
+
seenKeys.add(key);
|
|
35
|
+
}
|
|
36
|
+
return normalized;
|
|
37
|
+
}
|
|
38
|
+
function buildInitialFormState(descriptors, metadata) {
|
|
39
|
+
return descriptors.reduce(
|
|
40
|
+
(acc, descriptor) => {
|
|
41
|
+
const currentValue = metadata && typeof metadata === "object" ? metadata[descriptor.key] : void 0;
|
|
42
|
+
acc[descriptor.key] = normalizeFormValue(descriptor, currentValue);
|
|
43
|
+
return acc;
|
|
44
|
+
},
|
|
45
|
+
{}
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
function buildMetadataPayload({
|
|
49
|
+
descriptors,
|
|
50
|
+
values,
|
|
51
|
+
originalMetadata
|
|
52
|
+
}) {
|
|
53
|
+
const base = originalMetadata && typeof originalMetadata === "object" ? { ...originalMetadata } : {};
|
|
54
|
+
descriptors.forEach((descriptor) => {
|
|
55
|
+
const rawValue = values[descriptor.key];
|
|
56
|
+
const coerced = coerceMetadataValue(descriptor, rawValue);
|
|
57
|
+
if (typeof coerced === "undefined") {
|
|
58
|
+
delete base[descriptor.key];
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
base[descriptor.key] = coerced;
|
|
62
|
+
});
|
|
63
|
+
return base;
|
|
64
|
+
}
|
|
65
|
+
function hasMetadataChanges({
|
|
66
|
+
descriptors,
|
|
67
|
+
values,
|
|
68
|
+
originalMetadata
|
|
69
|
+
}) {
|
|
70
|
+
const next = buildMetadataPayload({ descriptors, values, originalMetadata });
|
|
71
|
+
const prev = originalMetadata && typeof originalMetadata === "object" ? originalMetadata : {};
|
|
72
|
+
return descriptors.some((descriptor) => {
|
|
73
|
+
const prevValue = prev[descriptor.key];
|
|
74
|
+
const nextValue = next[descriptor.key];
|
|
75
|
+
return !isDeepEqual(prevValue, nextValue);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
function validateValueForDescriptor(descriptor, value) {
|
|
79
|
+
if (descriptor.type === "number") {
|
|
80
|
+
if (value === "" || value === null || typeof value === "undefined") {
|
|
81
|
+
return void 0;
|
|
82
|
+
}
|
|
83
|
+
const numericValue = typeof value === "number" ? value : Number(String(value).trim());
|
|
84
|
+
if (Number.isNaN(numericValue)) {
|
|
85
|
+
return "Enter a valid number";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (descriptor.type === "file") {
|
|
89
|
+
if (!value) {
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
new URL(String(value).trim());
|
|
94
|
+
} catch (err) {
|
|
95
|
+
return "Enter a valid URL";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return void 0;
|
|
99
|
+
}
|
|
100
|
+
function normalizeFormValue(descriptor, currentValue) {
|
|
101
|
+
if (descriptor.type === "bool") {
|
|
102
|
+
return Boolean(currentValue);
|
|
103
|
+
}
|
|
104
|
+
if ((descriptor.type === "number" || descriptor.type === "text") && typeof currentValue === "number") {
|
|
105
|
+
return currentValue.toString();
|
|
106
|
+
}
|
|
107
|
+
if (typeof currentValue === "string" || typeof currentValue === "number") {
|
|
108
|
+
return String(currentValue);
|
|
109
|
+
}
|
|
110
|
+
return "";
|
|
111
|
+
}
|
|
112
|
+
function coerceMetadataValue(descriptor, value) {
|
|
113
|
+
if (value === "" || value === null || typeof value === "undefined") {
|
|
114
|
+
return void 0;
|
|
115
|
+
}
|
|
116
|
+
if (descriptor.type === "bool") {
|
|
117
|
+
if (typeof value === "boolean") {
|
|
118
|
+
return value;
|
|
119
|
+
}
|
|
120
|
+
if (typeof value === "number") {
|
|
121
|
+
return value !== 0;
|
|
122
|
+
}
|
|
123
|
+
if (typeof value === "string") {
|
|
124
|
+
const normalized = value.trim().toLowerCase();
|
|
125
|
+
if (!normalized) {
|
|
126
|
+
return void 0;
|
|
127
|
+
}
|
|
128
|
+
if (["true", "1", "yes", "y", "on"].includes(normalized)) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
if (["false", "0", "no", "n", "off"].includes(normalized)) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return Boolean(value);
|
|
136
|
+
}
|
|
137
|
+
if (descriptor.type === "number") {
|
|
138
|
+
if (typeof value === "number") {
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
const parsed = Number(String(value).trim());
|
|
142
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
143
|
+
}
|
|
144
|
+
return String(value).trim();
|
|
145
|
+
}
|
|
146
|
+
function getNormalizedKey(value) {
|
|
147
|
+
if (typeof value !== "string") {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const trimmed = value.trim();
|
|
151
|
+
return trimmed.length ? trimmed : null;
|
|
152
|
+
}
|
|
153
|
+
function getNormalizedType(value) {
|
|
154
|
+
if (typeof value !== "string") {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
const type = value.trim().toLowerCase();
|
|
158
|
+
return VALID_FIELD_TYPES.has(type) ? type : null;
|
|
159
|
+
}
|
|
160
|
+
function getNormalizedLabel(value) {
|
|
161
|
+
if (typeof value !== "string") {
|
|
162
|
+
return void 0;
|
|
163
|
+
}
|
|
164
|
+
const trimmed = value.trim();
|
|
165
|
+
return trimmed.length ? trimmed : void 0;
|
|
166
|
+
}
|
|
167
|
+
function isDeepEqual(a, b) {
|
|
168
|
+
if (a === b) {
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
if (typeof a === "object" && typeof b === "object" && a !== null && b !== null) {
|
|
172
|
+
const aKeys = Object.keys(a);
|
|
173
|
+
const bKeys = Object.keys(b);
|
|
174
|
+
if (aKeys.length !== bKeys.length) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return aKeys.every(
|
|
178
|
+
(key) => isDeepEqual(
|
|
179
|
+
a[key],
|
|
180
|
+
b[key]
|
|
181
|
+
)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
const CONFIG_ENDPOINT = "/admin/product-metadata-config";
|
|
187
|
+
const QUERY_KEY = ["medusa-product-helper", "metadata-config"];
|
|
188
|
+
const useMetadataConfig = (entity) => {
|
|
189
|
+
return useQuery({
|
|
190
|
+
queryKey: [...QUERY_KEY, entity],
|
|
191
|
+
queryFn: async () => {
|
|
192
|
+
const response = await fetch(`${CONFIG_ENDPOINT}?entity=${entity}`, {
|
|
193
|
+
credentials: "include"
|
|
194
|
+
});
|
|
195
|
+
if (!response.ok) {
|
|
196
|
+
throw new Error("Unable to load metadata configuration");
|
|
197
|
+
}
|
|
198
|
+
const payload = await response.json();
|
|
199
|
+
return normalizeMetadataDescriptors(payload.metadataDescriptors);
|
|
200
|
+
},
|
|
201
|
+
staleTime: 5 * 60 * 1e3
|
|
202
|
+
});
|
|
203
|
+
};
|
|
204
|
+
const useProductMetadataConfig = () => useMetadataConfig("product");
|
|
205
|
+
const useCategoryMetadataConfig = () => useMetadataConfig("category");
|
|
206
|
+
const CONFIG_DOCS_URL$1 = "https://docs.medusajs.com/admin/extension-points/widgets#product-category-details";
|
|
207
|
+
const CategoryMetadataTableWidget = ({ data }) => {
|
|
208
|
+
const { data: descriptors = [], isPending, isError } = useCategoryMetadataConfig();
|
|
209
|
+
const metadata = (data == null ? void 0 : data.metadata) ?? {};
|
|
210
|
+
const [baselineMetadata, setBaselineMetadata] = useState(metadata);
|
|
211
|
+
const queryClient = useQueryClient();
|
|
212
|
+
useEffect(() => {
|
|
213
|
+
setBaselineMetadata(metadata);
|
|
214
|
+
}, [metadata]);
|
|
215
|
+
const initialState = useMemo(
|
|
216
|
+
() => buildInitialFormState(descriptors, baselineMetadata),
|
|
217
|
+
[descriptors, baselineMetadata]
|
|
218
|
+
);
|
|
219
|
+
const [values, setValues] = useState(
|
|
220
|
+
initialState
|
|
221
|
+
);
|
|
222
|
+
const [isSaving, setIsSaving] = useState(false);
|
|
223
|
+
useEffect(() => {
|
|
224
|
+
setValues(initialState);
|
|
225
|
+
}, [initialState]);
|
|
226
|
+
const errors = useMemo(() => {
|
|
227
|
+
return descriptors.reduce((acc, descriptor) => {
|
|
228
|
+
const error = validateValueForDescriptor(descriptor, values[descriptor.key]);
|
|
229
|
+
if (error) {
|
|
230
|
+
acc[descriptor.key] = error;
|
|
231
|
+
}
|
|
232
|
+
return acc;
|
|
233
|
+
}, {});
|
|
234
|
+
}, [descriptors, values]);
|
|
235
|
+
const hasErrors = Object.keys(errors).length > 0;
|
|
236
|
+
const isDirty = useMemo(() => {
|
|
237
|
+
return hasMetadataChanges({
|
|
238
|
+
descriptors,
|
|
239
|
+
values,
|
|
240
|
+
originalMetadata: baselineMetadata
|
|
241
|
+
});
|
|
242
|
+
}, [descriptors, values, baselineMetadata]);
|
|
243
|
+
const handleStringChange = (key, nextValue) => {
|
|
244
|
+
setValues((prev) => ({
|
|
245
|
+
...prev,
|
|
246
|
+
[key]: nextValue
|
|
247
|
+
}));
|
|
248
|
+
};
|
|
249
|
+
const handleBooleanChange = (key, nextValue) => {
|
|
250
|
+
setValues((prev) => ({
|
|
251
|
+
...prev,
|
|
252
|
+
[key]: nextValue
|
|
253
|
+
}));
|
|
254
|
+
};
|
|
255
|
+
const handleReset = () => {
|
|
256
|
+
setValues(initialState);
|
|
257
|
+
};
|
|
258
|
+
const handleSubmit = async () => {
|
|
259
|
+
if (!(data == null ? void 0 : data.id) || !descriptors.length) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
setIsSaving(true);
|
|
263
|
+
try {
|
|
264
|
+
const metadataPayload = buildMetadataPayload({
|
|
265
|
+
descriptors,
|
|
266
|
+
values,
|
|
267
|
+
originalMetadata: baselineMetadata
|
|
268
|
+
});
|
|
269
|
+
const response = await fetch(`/admin/product-categories/${data.id}`, {
|
|
270
|
+
method: "POST",
|
|
271
|
+
credentials: "include",
|
|
272
|
+
headers: {
|
|
273
|
+
"Content-Type": "application/json"
|
|
274
|
+
},
|
|
275
|
+
body: JSON.stringify({
|
|
276
|
+
metadata: metadataPayload
|
|
277
|
+
})
|
|
278
|
+
});
|
|
279
|
+
if (!response.ok) {
|
|
280
|
+
const payload = await response.json().catch(() => null);
|
|
281
|
+
throw new Error((payload == null ? void 0 : payload.message) ?? "Unable to save metadata");
|
|
282
|
+
}
|
|
283
|
+
const updated = await response.json();
|
|
284
|
+
const nextMetadata = updated.product_category.metadata;
|
|
285
|
+
setBaselineMetadata(nextMetadata);
|
|
286
|
+
setValues(buildInitialFormState(descriptors, nextMetadata));
|
|
287
|
+
toast.success("Metadata saved");
|
|
288
|
+
await queryClient.invalidateQueries({
|
|
289
|
+
queryKey: ["product-categories"]
|
|
290
|
+
});
|
|
291
|
+
} catch (error) {
|
|
292
|
+
toast.error(error instanceof Error ? error.message : "Save failed");
|
|
293
|
+
} finally {
|
|
294
|
+
setIsSaving(false);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
return /* @__PURE__ */ jsxs(Container, { className: "flex flex-col gap-y-4", children: [
|
|
298
|
+
/* @__PURE__ */ jsxs("header", { className: "flex flex-col gap-y-1", children: [
|
|
299
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
|
|
300
|
+
/* @__PURE__ */ jsx(Heading, { level: "h2", children: "Metadata" }),
|
|
301
|
+
/* @__PURE__ */ jsx(Badge, { size: "2xsmall", rounded: "full", children: descriptors.length })
|
|
302
|
+
] }),
|
|
303
|
+
/* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", children: "Structured metadata mapped to the keys you configured in the plugin options." })
|
|
304
|
+
] }),
|
|
305
|
+
isPending ? /* @__PURE__ */ jsx(Skeleton, { className: "h-[160px] w-full" }) : isError ? /* @__PURE__ */ jsxs(InlineTip, { variant: "error", label: "Configuration unavailable", children: [
|
|
306
|
+
"Unable to load metadata configuration for this plugin. Confirm that the plugin is registered with options in ",
|
|
307
|
+
/* @__PURE__ */ jsx("code", { children: "medusa-config.ts" }),
|
|
308
|
+
"."
|
|
309
|
+
] }) : !descriptors.length ? /* @__PURE__ */ jsxs(InlineTip, { variant: "info", label: "No configured metadata keys", children: [
|
|
310
|
+
"Provide a ",
|
|
311
|
+
/* @__PURE__ */ jsx("code", { children: "metadataDescriptors" }),
|
|
312
|
+
" array in the plugin options to control which keys show up here.",
|
|
313
|
+
" ",
|
|
314
|
+
/* @__PURE__ */ jsx(
|
|
315
|
+
"a",
|
|
316
|
+
{
|
|
317
|
+
className: "text-ui-fg-interactive underline",
|
|
318
|
+
href: CONFIG_DOCS_URL$1,
|
|
319
|
+
target: "_blank",
|
|
320
|
+
rel: "noreferrer",
|
|
321
|
+
children: "Learn how to configure it."
|
|
322
|
+
}
|
|
323
|
+
)
|
|
324
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
325
|
+
/* @__PURE__ */ jsx("div", { className: "overflow-hidden rounded-lg border border-ui-border-base", children: /* @__PURE__ */ jsxs("table", { className: "min-w-full divide-y divide-ui-border-base", children: [
|
|
326
|
+
/* @__PURE__ */ jsx("thead", { className: "bg-ui-bg-subtle", children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
327
|
+
/* @__PURE__ */ jsx(
|
|
328
|
+
"th",
|
|
329
|
+
{
|
|
330
|
+
scope: "col",
|
|
331
|
+
className: "txt-compact-xsmall-plus text-left uppercase tracking-wide text-ui-fg-muted px-4 py-3",
|
|
332
|
+
children: "Label"
|
|
333
|
+
}
|
|
334
|
+
),
|
|
335
|
+
/* @__PURE__ */ jsx(
|
|
336
|
+
"th",
|
|
337
|
+
{
|
|
338
|
+
scope: "col",
|
|
339
|
+
className: "txt-compact-xsmall-plus text-left uppercase tracking-wide text-ui-fg-muted px-4 py-3",
|
|
340
|
+
children: "Value"
|
|
341
|
+
}
|
|
342
|
+
)
|
|
343
|
+
] }) }),
|
|
344
|
+
/* @__PURE__ */ jsx("tbody", { className: "divide-y divide-ui-border-subtle bg-ui-bg-base", children: descriptors.map((descriptor) => {
|
|
345
|
+
const value = values[descriptor.key];
|
|
346
|
+
const error = errors[descriptor.key];
|
|
347
|
+
return /* @__PURE__ */ jsxs("tr", { children: [
|
|
348
|
+
/* @__PURE__ */ jsx(
|
|
349
|
+
"th",
|
|
350
|
+
{
|
|
351
|
+
scope: "row",
|
|
352
|
+
className: "txt-compact-medium text-ui-fg-base align-top px-4 py-4",
|
|
353
|
+
children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-1", children: [
|
|
354
|
+
/* @__PURE__ */ jsx("span", { children: descriptor.label ?? descriptor.key }),
|
|
355
|
+
/* @__PURE__ */ jsx("span", { className: "txt-compact-xsmall-plus text-ui-fg-muted uppercase tracking-wide", children: descriptor.type })
|
|
356
|
+
] })
|
|
357
|
+
}
|
|
358
|
+
),
|
|
359
|
+
/* @__PURE__ */ jsx("td", { className: "align-top px-4 py-4", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
360
|
+
/* @__PURE__ */ jsx(
|
|
361
|
+
ValueField$1,
|
|
362
|
+
{
|
|
363
|
+
descriptor,
|
|
364
|
+
value,
|
|
365
|
+
onStringChange: handleStringChange,
|
|
366
|
+
onBooleanChange: handleBooleanChange
|
|
367
|
+
}
|
|
368
|
+
),
|
|
369
|
+
error && /* @__PURE__ */ jsx(Text, { className: "txt-compact-small text-ui-fg-error", children: error })
|
|
370
|
+
] }) })
|
|
371
|
+
] }, descriptor.key);
|
|
372
|
+
}) })
|
|
373
|
+
] }) }),
|
|
374
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-3 border-t border-ui-border-subtle pt-3 md:flex-row md:items-center md:justify-between", children: [
|
|
375
|
+
/* @__PURE__ */ jsx(Text, { className: "text-ui-fg-muted", children: "Changes are stored on the category metadata object. Clearing a field removes the corresponding key on save." }),
|
|
376
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
|
|
377
|
+
/* @__PURE__ */ jsx(
|
|
378
|
+
Button,
|
|
379
|
+
{
|
|
380
|
+
variant: "secondary",
|
|
381
|
+
size: "small",
|
|
382
|
+
disabled: !isDirty || isSaving,
|
|
383
|
+
onClick: handleReset,
|
|
384
|
+
children: "Reset"
|
|
385
|
+
}
|
|
386
|
+
),
|
|
387
|
+
/* @__PURE__ */ jsx(
|
|
388
|
+
Button,
|
|
389
|
+
{
|
|
390
|
+
size: "small",
|
|
391
|
+
onClick: handleSubmit,
|
|
392
|
+
disabled: !isDirty || hasErrors || isSaving,
|
|
393
|
+
isLoading: isSaving,
|
|
394
|
+
children: "Save metadata"
|
|
395
|
+
}
|
|
396
|
+
)
|
|
397
|
+
] })
|
|
398
|
+
] })
|
|
399
|
+
] })
|
|
400
|
+
] });
|
|
401
|
+
};
|
|
402
|
+
const ValueField$1 = ({
|
|
403
|
+
descriptor,
|
|
404
|
+
value,
|
|
405
|
+
onStringChange,
|
|
406
|
+
onBooleanChange
|
|
407
|
+
}) => {
|
|
408
|
+
const fileInputRef = useRef(null);
|
|
409
|
+
const [isUploading, setIsUploading] = useState(false);
|
|
410
|
+
const handleFileUpload = async (event) => {
|
|
411
|
+
var _a;
|
|
412
|
+
const file = (_a = event.target.files) == null ? void 0 : _a[0];
|
|
413
|
+
if (!file) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
setIsUploading(true);
|
|
417
|
+
try {
|
|
418
|
+
const formData = new FormData();
|
|
419
|
+
formData.append("files", file);
|
|
420
|
+
const response = await fetch("/admin/uploads", {
|
|
421
|
+
method: "POST",
|
|
422
|
+
credentials: "include",
|
|
423
|
+
body: formData
|
|
424
|
+
});
|
|
425
|
+
if (!response.ok) {
|
|
426
|
+
const payload = await response.json().catch(() => null);
|
|
427
|
+
throw new Error((payload == null ? void 0 : payload.message) ?? "File upload failed");
|
|
428
|
+
}
|
|
429
|
+
const result = await response.json();
|
|
430
|
+
if (result.files && result.files.length > 0) {
|
|
431
|
+
const uploadedFile = result.files[0];
|
|
432
|
+
const fileUrl = uploadedFile.url || uploadedFile.key;
|
|
433
|
+
if (fileUrl) {
|
|
434
|
+
onStringChange(descriptor.key, fileUrl);
|
|
435
|
+
toast.success("File uploaded successfully");
|
|
436
|
+
} else {
|
|
437
|
+
throw new Error("File upload succeeded but no URL returned");
|
|
438
|
+
}
|
|
439
|
+
} else {
|
|
440
|
+
throw new Error("File upload failed - no files returned");
|
|
441
|
+
}
|
|
442
|
+
} catch (error) {
|
|
443
|
+
toast.error(
|
|
444
|
+
error instanceof Error ? error.message : "Failed to upload file"
|
|
445
|
+
);
|
|
446
|
+
} finally {
|
|
447
|
+
setIsUploading(false);
|
|
448
|
+
if (fileInputRef.current) {
|
|
449
|
+
fileInputRef.current.value = "";
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
if (descriptor.type === "bool") {
|
|
454
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
|
|
455
|
+
/* @__PURE__ */ jsx(
|
|
456
|
+
Switch,
|
|
457
|
+
{
|
|
458
|
+
checked: Boolean(value),
|
|
459
|
+
onCheckedChange: (checked) => onBooleanChange(descriptor.key, Boolean(checked)),
|
|
460
|
+
"aria-label": `Toggle ${descriptor.label ?? descriptor.key}`
|
|
461
|
+
}
|
|
462
|
+
),
|
|
463
|
+
/* @__PURE__ */ jsx(Text, { className: "txt-compact-small text-ui-fg-muted", children: Boolean(value) ? "True" : "False" })
|
|
464
|
+
] });
|
|
465
|
+
}
|
|
466
|
+
if (descriptor.type === "text") {
|
|
467
|
+
return /* @__PURE__ */ jsx(
|
|
468
|
+
Textarea,
|
|
469
|
+
{
|
|
470
|
+
value: value ?? "",
|
|
471
|
+
placeholder: "Enter text",
|
|
472
|
+
rows: 3,
|
|
473
|
+
onChange: (event) => onStringChange(descriptor.key, event.target.value)
|
|
474
|
+
}
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
if (descriptor.type === "number") {
|
|
478
|
+
return /* @__PURE__ */ jsx(
|
|
479
|
+
Input,
|
|
480
|
+
{
|
|
481
|
+
type: "text",
|
|
482
|
+
inputMode: "decimal",
|
|
483
|
+
placeholder: "0.00",
|
|
484
|
+
value: value ?? "",
|
|
485
|
+
onChange: (event) => onStringChange(descriptor.key, event.target.value)
|
|
486
|
+
}
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
490
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
|
|
491
|
+
/* @__PURE__ */ jsx(
|
|
492
|
+
Input,
|
|
493
|
+
{
|
|
494
|
+
type: "url",
|
|
495
|
+
placeholder: "https://example.com/file",
|
|
496
|
+
value: value ?? "",
|
|
497
|
+
onChange: (event) => onStringChange(descriptor.key, event.target.value),
|
|
498
|
+
className: "flex-1"
|
|
499
|
+
}
|
|
500
|
+
),
|
|
501
|
+
/* @__PURE__ */ jsx(
|
|
502
|
+
"input",
|
|
503
|
+
{
|
|
504
|
+
ref: fileInputRef,
|
|
505
|
+
type: "file",
|
|
506
|
+
className: "hidden",
|
|
507
|
+
onChange: handleFileUpload,
|
|
508
|
+
disabled: isUploading,
|
|
509
|
+
"aria-label": `Upload file for ${descriptor.label ?? descriptor.key}`
|
|
510
|
+
}
|
|
511
|
+
),
|
|
512
|
+
/* @__PURE__ */ jsx(
|
|
513
|
+
Button,
|
|
514
|
+
{
|
|
515
|
+
type: "button",
|
|
516
|
+
variant: "secondary",
|
|
517
|
+
size: "small",
|
|
518
|
+
onClick: () => {
|
|
519
|
+
var _a;
|
|
520
|
+
return (_a = fileInputRef.current) == null ? void 0 : _a.click();
|
|
521
|
+
},
|
|
522
|
+
disabled: isUploading,
|
|
523
|
+
isLoading: isUploading,
|
|
524
|
+
children: isUploading ? "Uploading..." : "Upload"
|
|
525
|
+
}
|
|
526
|
+
)
|
|
527
|
+
] }),
|
|
528
|
+
typeof value === "string" && value && /* @__PURE__ */ jsx(
|
|
529
|
+
"a",
|
|
530
|
+
{
|
|
531
|
+
className: "txt-compact-small-plus text-ui-fg-interactive underline",
|
|
532
|
+
href: value,
|
|
533
|
+
target: "_blank",
|
|
534
|
+
rel: "noreferrer",
|
|
535
|
+
children: "View file"
|
|
536
|
+
}
|
|
537
|
+
)
|
|
538
|
+
] });
|
|
539
|
+
};
|
|
540
|
+
defineWidgetConfig({
|
|
541
|
+
zone: "product_category.details.after"
|
|
542
|
+
});
|
|
543
|
+
const HideCategoryDefaultMetadataWidget = () => {
|
|
544
|
+
useEffect(() => {
|
|
545
|
+
const hideMetadataSection = () => {
|
|
546
|
+
const headings = document.querySelectorAll("h2");
|
|
547
|
+
headings.forEach((heading) => {
|
|
548
|
+
var _a;
|
|
549
|
+
if (((_a = heading.textContent) == null ? void 0 : _a.trim()) === "Metadata") {
|
|
550
|
+
let container = heading.parentElement;
|
|
551
|
+
while (container && container !== document.body) {
|
|
552
|
+
const hasContainerClass = container.classList.toString().includes("Container");
|
|
553
|
+
const isInSidebar = container.closest('[class*="Sidebar"]') || container.closest('[class*="sidebar"]');
|
|
554
|
+
if (hasContainerClass || isInSidebar) {
|
|
555
|
+
const editLink = container.querySelector('a[href*="metadata/edit"]');
|
|
556
|
+
const badge = container.querySelector('div[class*="Badge"]');
|
|
557
|
+
if (editLink && badge) {
|
|
558
|
+
container.style.display = "none";
|
|
559
|
+
container.setAttribute("data-category-metadata-hidden", "true");
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
container = container.parentElement;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
});
|
|
567
|
+
};
|
|
568
|
+
const runHide = () => {
|
|
569
|
+
setTimeout(hideMetadataSection, 100);
|
|
570
|
+
};
|
|
571
|
+
runHide();
|
|
572
|
+
const observer = new MutationObserver(() => {
|
|
573
|
+
const alreadyHidden = document.querySelector(
|
|
574
|
+
'[data-category-metadata-hidden="true"]'
|
|
575
|
+
);
|
|
576
|
+
if (!alreadyHidden) {
|
|
577
|
+
runHide();
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
observer.observe(document.body, {
|
|
581
|
+
childList: true,
|
|
582
|
+
subtree: true
|
|
583
|
+
});
|
|
584
|
+
return () => {
|
|
585
|
+
observer.disconnect();
|
|
586
|
+
const hidden = document.querySelector(
|
|
587
|
+
'[data-category-metadata-hidden="true"]'
|
|
588
|
+
);
|
|
589
|
+
if (hidden) {
|
|
590
|
+
hidden.style.display = "";
|
|
591
|
+
hidden.removeAttribute("data-category-metadata-hidden");
|
|
592
|
+
}
|
|
593
|
+
};
|
|
594
|
+
}, []);
|
|
595
|
+
return null;
|
|
596
|
+
};
|
|
597
|
+
defineWidgetConfig({
|
|
598
|
+
zone: "product_category.details.side.before"
|
|
599
|
+
});
|
|
600
|
+
const HideDefaultMetadataWidget = () => {
|
|
601
|
+
useEffect(() => {
|
|
602
|
+
const hideMetadataSection = () => {
|
|
603
|
+
const headings = document.querySelectorAll("h2");
|
|
604
|
+
headings.forEach((heading) => {
|
|
605
|
+
var _a;
|
|
606
|
+
if (((_a = heading.textContent) == null ? void 0 : _a.trim()) === "Metadata") {
|
|
607
|
+
let container = heading.parentElement;
|
|
608
|
+
while (container && container !== document.body) {
|
|
609
|
+
const hasContainerClass = container.classList.toString().includes("Container");
|
|
610
|
+
const isInSidebar = container.closest('[class*="Sidebar"]') || container.closest('[class*="sidebar"]');
|
|
611
|
+
if (hasContainerClass || isInSidebar) {
|
|
612
|
+
const editLink = container.querySelector('a[href*="metadata/edit"]');
|
|
613
|
+
const badge = container.querySelector('div[class*="Badge"]');
|
|
614
|
+
if (editLink && badge) {
|
|
615
|
+
container.style.display = "none";
|
|
616
|
+
container.setAttribute("data-metadata-hidden", "true");
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
container = container.parentElement;
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
});
|
|
624
|
+
};
|
|
625
|
+
const runHide = () => {
|
|
626
|
+
setTimeout(hideMetadataSection, 100);
|
|
627
|
+
};
|
|
628
|
+
runHide();
|
|
629
|
+
const observer = new MutationObserver(() => {
|
|
630
|
+
const alreadyHidden = document.querySelector('[data-metadata-hidden="true"]');
|
|
631
|
+
if (!alreadyHidden) {
|
|
632
|
+
runHide();
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
observer.observe(document.body, {
|
|
636
|
+
childList: true,
|
|
637
|
+
subtree: true
|
|
638
|
+
});
|
|
639
|
+
return () => {
|
|
640
|
+
observer.disconnect();
|
|
641
|
+
const hidden = document.querySelector('[data-metadata-hidden="true"]');
|
|
642
|
+
if (hidden) {
|
|
643
|
+
hidden.style.display = "";
|
|
644
|
+
hidden.removeAttribute("data-metadata-hidden");
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
}, []);
|
|
648
|
+
return null;
|
|
649
|
+
};
|
|
650
|
+
defineWidgetConfig({
|
|
651
|
+
zone: "product.details.side.before"
|
|
652
|
+
});
|
|
653
|
+
const CONFIG_DOCS_URL = "https://docs.medusajs.com/admin/extension-points/widgets#product-details";
|
|
654
|
+
const ProductMetadataTableWidget = ({ data }) => {
|
|
655
|
+
const { data: descriptors = [], isPending, isError } = useProductMetadataConfig();
|
|
656
|
+
const metadata = (data == null ? void 0 : data.metadata) ?? {};
|
|
657
|
+
const [baselineMetadata, setBaselineMetadata] = useState(metadata);
|
|
658
|
+
const queryClient = useQueryClient();
|
|
659
|
+
useEffect(() => {
|
|
660
|
+
setBaselineMetadata(metadata);
|
|
661
|
+
}, [metadata]);
|
|
662
|
+
const initialState = useMemo(
|
|
663
|
+
() => buildInitialFormState(descriptors, baselineMetadata),
|
|
664
|
+
[descriptors, baselineMetadata]
|
|
665
|
+
);
|
|
666
|
+
const [values, setValues] = useState(
|
|
667
|
+
initialState
|
|
668
|
+
);
|
|
669
|
+
const [isSaving, setIsSaving] = useState(false);
|
|
670
|
+
useEffect(() => {
|
|
671
|
+
setValues(initialState);
|
|
672
|
+
}, [initialState]);
|
|
673
|
+
const errors = useMemo(() => {
|
|
674
|
+
return descriptors.reduce((acc, descriptor) => {
|
|
675
|
+
const error = validateValueForDescriptor(descriptor, values[descriptor.key]);
|
|
676
|
+
if (error) {
|
|
677
|
+
acc[descriptor.key] = error;
|
|
678
|
+
}
|
|
679
|
+
return acc;
|
|
680
|
+
}, {});
|
|
681
|
+
}, [descriptors, values]);
|
|
682
|
+
const hasErrors = Object.keys(errors).length > 0;
|
|
683
|
+
const isDirty = useMemo(() => {
|
|
684
|
+
return hasMetadataChanges({
|
|
685
|
+
descriptors,
|
|
686
|
+
values,
|
|
687
|
+
originalMetadata: baselineMetadata
|
|
688
|
+
});
|
|
689
|
+
}, [descriptors, values, baselineMetadata]);
|
|
690
|
+
const handleStringChange = (key, nextValue) => {
|
|
691
|
+
setValues((prev) => ({
|
|
692
|
+
...prev,
|
|
693
|
+
[key]: nextValue
|
|
694
|
+
}));
|
|
695
|
+
};
|
|
696
|
+
const handleBooleanChange = (key, nextValue) => {
|
|
697
|
+
setValues((prev) => ({
|
|
698
|
+
...prev,
|
|
699
|
+
[key]: nextValue
|
|
700
|
+
}));
|
|
701
|
+
};
|
|
702
|
+
const handleReset = () => {
|
|
703
|
+
setValues(initialState);
|
|
704
|
+
};
|
|
705
|
+
const handleSubmit = async () => {
|
|
706
|
+
if (!(data == null ? void 0 : data.id) || !descriptors.length) {
|
|
707
|
+
return;
|
|
708
|
+
}
|
|
709
|
+
setIsSaving(true);
|
|
710
|
+
try {
|
|
711
|
+
const metadataPayload = buildMetadataPayload({
|
|
712
|
+
descriptors,
|
|
713
|
+
values,
|
|
714
|
+
originalMetadata: baselineMetadata
|
|
715
|
+
});
|
|
716
|
+
const response = await fetch(`/admin/products/${data.id}`, {
|
|
717
|
+
method: "POST",
|
|
718
|
+
credentials: "include",
|
|
719
|
+
headers: {
|
|
720
|
+
"Content-Type": "application/json"
|
|
721
|
+
},
|
|
722
|
+
body: JSON.stringify({
|
|
723
|
+
metadata: metadataPayload
|
|
724
|
+
})
|
|
725
|
+
});
|
|
726
|
+
if (!response.ok) {
|
|
727
|
+
const payload = await response.json().catch(() => null);
|
|
728
|
+
throw new Error((payload == null ? void 0 : payload.message) ?? "Unable to save metadata");
|
|
729
|
+
}
|
|
730
|
+
const updated = await response.json();
|
|
731
|
+
const nextMetadata = updated.product.metadata;
|
|
732
|
+
setBaselineMetadata(nextMetadata);
|
|
733
|
+
setValues(buildInitialFormState(descriptors, nextMetadata));
|
|
734
|
+
toast.success("Metadata saved");
|
|
735
|
+
await queryClient.invalidateQueries({
|
|
736
|
+
queryKey: ["products"]
|
|
737
|
+
});
|
|
738
|
+
} catch (error) {
|
|
739
|
+
toast.error(error instanceof Error ? error.message : "Save failed");
|
|
740
|
+
} finally {
|
|
741
|
+
setIsSaving(false);
|
|
742
|
+
}
|
|
743
|
+
};
|
|
744
|
+
return /* @__PURE__ */ jsxs(Container, { className: "flex flex-col gap-y-4", children: [
|
|
745
|
+
/* @__PURE__ */ jsxs("header", { className: "flex flex-col gap-y-1", children: [
|
|
746
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
|
|
747
|
+
/* @__PURE__ */ jsx(Heading, { level: "h2", children: "Metadata" }),
|
|
748
|
+
/* @__PURE__ */ jsx(Badge, { size: "2xsmall", rounded: "full", children: descriptors.length })
|
|
749
|
+
] }),
|
|
750
|
+
/* @__PURE__ */ jsx(Text, { className: "text-ui-fg-subtle", children: "Structured metadata mapped to the keys you configured in the plugin options." })
|
|
751
|
+
] }),
|
|
752
|
+
isPending ? /* @__PURE__ */ jsx(Skeleton, { className: "h-[160px] w-full" }) : isError ? /* @__PURE__ */ jsxs(InlineTip, { variant: "error", label: "Configuration unavailable", children: [
|
|
753
|
+
"Unable to load metadata configuration for this plugin. Confirm that the plugin is registered with options in ",
|
|
754
|
+
/* @__PURE__ */ jsx("code", { children: "medusa-config.ts" }),
|
|
755
|
+
"."
|
|
756
|
+
] }) : !descriptors.length ? /* @__PURE__ */ jsxs(InlineTip, { variant: "info", label: "No configured metadata keys", children: [
|
|
757
|
+
"Provide a ",
|
|
758
|
+
/* @__PURE__ */ jsx("code", { children: "metadataDescriptors" }),
|
|
759
|
+
" array in the plugin options to control which keys show up here.",
|
|
760
|
+
" ",
|
|
761
|
+
/* @__PURE__ */ jsx(
|
|
762
|
+
"a",
|
|
763
|
+
{
|
|
764
|
+
className: "text-ui-fg-interactive underline",
|
|
765
|
+
href: CONFIG_DOCS_URL,
|
|
766
|
+
target: "_blank",
|
|
767
|
+
rel: "noreferrer",
|
|
768
|
+
children: "Learn how to configure it."
|
|
769
|
+
}
|
|
770
|
+
)
|
|
771
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
772
|
+
/* @__PURE__ */ jsx("div", { className: "overflow-hidden rounded-lg border border-ui-border-base", children: /* @__PURE__ */ jsxs("table", { className: "min-w-full divide-y divide-ui-border-base", children: [
|
|
773
|
+
/* @__PURE__ */ jsx("thead", { className: "bg-ui-bg-subtle", children: /* @__PURE__ */ jsxs("tr", { children: [
|
|
774
|
+
/* @__PURE__ */ jsx(
|
|
775
|
+
"th",
|
|
776
|
+
{
|
|
777
|
+
scope: "col",
|
|
778
|
+
className: "txt-compact-xsmall-plus text-left uppercase tracking-wide text-ui-fg-muted px-4 py-3",
|
|
779
|
+
children: "Label"
|
|
780
|
+
}
|
|
781
|
+
),
|
|
782
|
+
/* @__PURE__ */ jsx(
|
|
783
|
+
"th",
|
|
784
|
+
{
|
|
785
|
+
scope: "col",
|
|
786
|
+
className: "txt-compact-xsmall-plus text-left uppercase tracking-wide text-ui-fg-muted px-4 py-3",
|
|
787
|
+
children: "Value"
|
|
788
|
+
}
|
|
789
|
+
)
|
|
790
|
+
] }) }),
|
|
791
|
+
/* @__PURE__ */ jsx("tbody", { className: "divide-y divide-ui-border-subtle bg-ui-bg-base", children: descriptors.map((descriptor) => {
|
|
792
|
+
const value = values[descriptor.key];
|
|
793
|
+
const error = errors[descriptor.key];
|
|
794
|
+
return /* @__PURE__ */ jsxs("tr", { children: [
|
|
795
|
+
/* @__PURE__ */ jsx(
|
|
796
|
+
"th",
|
|
797
|
+
{
|
|
798
|
+
scope: "row",
|
|
799
|
+
className: "txt-compact-medium text-ui-fg-base align-top px-4 py-4",
|
|
800
|
+
children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-1", children: [
|
|
801
|
+
/* @__PURE__ */ jsx("span", { children: descriptor.label ?? descriptor.key }),
|
|
802
|
+
/* @__PURE__ */ jsx("span", { className: "txt-compact-xsmall-plus text-ui-fg-muted uppercase tracking-wide", children: descriptor.type })
|
|
803
|
+
] })
|
|
804
|
+
}
|
|
805
|
+
),
|
|
806
|
+
/* @__PURE__ */ jsx("td", { className: "align-top px-4 py-4", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
807
|
+
/* @__PURE__ */ jsx(
|
|
808
|
+
ValueField,
|
|
809
|
+
{
|
|
810
|
+
descriptor,
|
|
811
|
+
value,
|
|
812
|
+
onStringChange: handleStringChange,
|
|
813
|
+
onBooleanChange: handleBooleanChange
|
|
814
|
+
}
|
|
815
|
+
),
|
|
816
|
+
error && /* @__PURE__ */ jsx(Text, { className: "txt-compact-small text-ui-fg-error", children: error })
|
|
817
|
+
] }) })
|
|
818
|
+
] }, descriptor.key);
|
|
819
|
+
}) })
|
|
820
|
+
] }) }),
|
|
821
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-3 border-t border-ui-border-subtle pt-3 md:flex-row md:items-center md:justify-between", children: [
|
|
822
|
+
/* @__PURE__ */ jsx(Text, { className: "text-ui-fg-muted", children: "Changes are stored on the product metadata object. Clearing a field removes the corresponding key on save." }),
|
|
823
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
|
|
824
|
+
/* @__PURE__ */ jsx(
|
|
825
|
+
Button,
|
|
826
|
+
{
|
|
827
|
+
variant: "secondary",
|
|
828
|
+
size: "small",
|
|
829
|
+
disabled: !isDirty || isSaving,
|
|
830
|
+
onClick: handleReset,
|
|
831
|
+
children: "Reset"
|
|
832
|
+
}
|
|
833
|
+
),
|
|
834
|
+
/* @__PURE__ */ jsx(
|
|
835
|
+
Button,
|
|
836
|
+
{
|
|
837
|
+
size: "small",
|
|
838
|
+
onClick: handleSubmit,
|
|
839
|
+
disabled: !isDirty || hasErrors || isSaving,
|
|
840
|
+
isLoading: isSaving,
|
|
841
|
+
children: "Save metadata"
|
|
842
|
+
}
|
|
843
|
+
)
|
|
844
|
+
] })
|
|
845
|
+
] })
|
|
846
|
+
] })
|
|
847
|
+
] });
|
|
848
|
+
};
|
|
849
|
+
const ValueField = ({
|
|
850
|
+
descriptor,
|
|
851
|
+
value,
|
|
852
|
+
onStringChange,
|
|
853
|
+
onBooleanChange
|
|
854
|
+
}) => {
|
|
855
|
+
const fileInputRef = useRef(null);
|
|
856
|
+
const [isUploading, setIsUploading] = useState(false);
|
|
857
|
+
const handleFileUpload = async (event) => {
|
|
858
|
+
var _a;
|
|
859
|
+
const file = (_a = event.target.files) == null ? void 0 : _a[0];
|
|
860
|
+
if (!file) {
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
setIsUploading(true);
|
|
864
|
+
try {
|
|
865
|
+
const formData = new FormData();
|
|
866
|
+
formData.append("files", file);
|
|
867
|
+
const response = await fetch("/admin/uploads", {
|
|
868
|
+
method: "POST",
|
|
869
|
+
credentials: "include",
|
|
870
|
+
body: formData
|
|
871
|
+
});
|
|
872
|
+
if (!response.ok) {
|
|
873
|
+
const payload = await response.json().catch(() => null);
|
|
874
|
+
throw new Error((payload == null ? void 0 : payload.message) ?? "File upload failed");
|
|
875
|
+
}
|
|
876
|
+
const result = await response.json();
|
|
877
|
+
if (result.files && result.files.length > 0) {
|
|
878
|
+
const uploadedFile = result.files[0];
|
|
879
|
+
const fileUrl = uploadedFile.url || uploadedFile.key;
|
|
880
|
+
if (fileUrl) {
|
|
881
|
+
onStringChange(descriptor.key, fileUrl);
|
|
882
|
+
toast.success("File uploaded successfully");
|
|
883
|
+
} else {
|
|
884
|
+
throw new Error("File upload succeeded but no URL returned");
|
|
885
|
+
}
|
|
886
|
+
} else {
|
|
887
|
+
throw new Error("File upload failed - no files returned");
|
|
888
|
+
}
|
|
889
|
+
} catch (error) {
|
|
890
|
+
toast.error(
|
|
891
|
+
error instanceof Error ? error.message : "Failed to upload file"
|
|
892
|
+
);
|
|
893
|
+
} finally {
|
|
894
|
+
setIsUploading(false);
|
|
895
|
+
if (fileInputRef.current) {
|
|
896
|
+
fileInputRef.current.value = "";
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
};
|
|
900
|
+
if (descriptor.type === "bool") {
|
|
901
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
|
|
902
|
+
/* @__PURE__ */ jsx(
|
|
903
|
+
Switch,
|
|
904
|
+
{
|
|
905
|
+
checked: Boolean(value),
|
|
906
|
+
onCheckedChange: (checked) => onBooleanChange(descriptor.key, Boolean(checked)),
|
|
907
|
+
"aria-label": `Toggle ${descriptor.label ?? descriptor.key}`
|
|
908
|
+
}
|
|
909
|
+
),
|
|
910
|
+
/* @__PURE__ */ jsx(Text, { className: "txt-compact-small text-ui-fg-muted", children: Boolean(value) ? "True" : "False" })
|
|
911
|
+
] });
|
|
912
|
+
}
|
|
913
|
+
if (descriptor.type === "text") {
|
|
914
|
+
return /* @__PURE__ */ jsx(
|
|
915
|
+
Textarea,
|
|
916
|
+
{
|
|
917
|
+
value: value ?? "",
|
|
918
|
+
placeholder: "Enter text",
|
|
919
|
+
rows: 3,
|
|
920
|
+
onChange: (event) => onStringChange(descriptor.key, event.target.value)
|
|
921
|
+
}
|
|
922
|
+
);
|
|
923
|
+
}
|
|
924
|
+
if (descriptor.type === "number") {
|
|
925
|
+
return /* @__PURE__ */ jsx(
|
|
926
|
+
Input,
|
|
927
|
+
{
|
|
928
|
+
type: "text",
|
|
929
|
+
inputMode: "decimal",
|
|
930
|
+
placeholder: "0.00",
|
|
931
|
+
value: value ?? "",
|
|
932
|
+
onChange: (event) => onStringChange(descriptor.key, event.target.value)
|
|
933
|
+
}
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
937
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
|
|
938
|
+
/* @__PURE__ */ jsx(
|
|
939
|
+
Input,
|
|
940
|
+
{
|
|
941
|
+
type: "url",
|
|
942
|
+
placeholder: "https://example.com/file",
|
|
943
|
+
value: value ?? "",
|
|
944
|
+
onChange: (event) => onStringChange(descriptor.key, event.target.value),
|
|
945
|
+
className: "flex-1"
|
|
946
|
+
}
|
|
947
|
+
),
|
|
948
|
+
/* @__PURE__ */ jsx(
|
|
949
|
+
"input",
|
|
950
|
+
{
|
|
951
|
+
ref: fileInputRef,
|
|
952
|
+
type: "file",
|
|
953
|
+
className: "hidden",
|
|
954
|
+
onChange: handleFileUpload,
|
|
955
|
+
disabled: isUploading,
|
|
956
|
+
"aria-label": `Upload file for ${descriptor.label ?? descriptor.key}`
|
|
957
|
+
}
|
|
958
|
+
),
|
|
959
|
+
/* @__PURE__ */ jsx(
|
|
960
|
+
Button,
|
|
961
|
+
{
|
|
962
|
+
type: "button",
|
|
963
|
+
variant: "secondary",
|
|
964
|
+
size: "small",
|
|
965
|
+
onClick: () => {
|
|
966
|
+
var _a;
|
|
967
|
+
return (_a = fileInputRef.current) == null ? void 0 : _a.click();
|
|
968
|
+
},
|
|
969
|
+
disabled: isUploading,
|
|
970
|
+
isLoading: isUploading,
|
|
971
|
+
children: isUploading ? "Uploading..." : "Upload"
|
|
972
|
+
}
|
|
973
|
+
)
|
|
974
|
+
] }),
|
|
975
|
+
typeof value === "string" && value && /* @__PURE__ */ jsx(
|
|
976
|
+
"a",
|
|
977
|
+
{
|
|
978
|
+
className: "txt-compact-small-plus text-ui-fg-interactive underline",
|
|
979
|
+
href: value,
|
|
980
|
+
target: "_blank",
|
|
981
|
+
rel: "noreferrer",
|
|
982
|
+
children: "View file"
|
|
983
|
+
}
|
|
984
|
+
)
|
|
985
|
+
] });
|
|
986
|
+
};
|
|
987
|
+
defineWidgetConfig({
|
|
988
|
+
zone: "product.details.after"
|
|
989
|
+
});
|
|
1
990
|
const i18nTranslations0 = {};
|
|
2
|
-
const widgetModule = { widgets: [
|
|
991
|
+
const widgetModule = { widgets: [
|
|
992
|
+
{
|
|
993
|
+
Component: CategoryMetadataTableWidget,
|
|
994
|
+
zone: ["product_category.details.after"]
|
|
995
|
+
},
|
|
996
|
+
{
|
|
997
|
+
Component: HideCategoryDefaultMetadataWidget,
|
|
998
|
+
zone: ["product_category.details.side.before"]
|
|
999
|
+
},
|
|
1000
|
+
{
|
|
1001
|
+
Component: HideDefaultMetadataWidget,
|
|
1002
|
+
zone: ["product.details.side.before"]
|
|
1003
|
+
},
|
|
1004
|
+
{
|
|
1005
|
+
Component: ProductMetadataTableWidget,
|
|
1006
|
+
zone: ["product.details.after"]
|
|
1007
|
+
}
|
|
1008
|
+
] };
|
|
3
1009
|
const routeModule = {
|
|
4
1010
|
routes: []
|
|
5
1011
|
};
|