@medusajs/draft-order 2.21.2 → 2.22.0-preview-20261002101246

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.
@@ -7269,356 +7269,6 @@ const customItemSchema = z.object({
7269
7269
  quantity: z.number(),
7270
7270
  unit_price: z.union([z.number(), z.string()])
7271
7271
  });
7272
- const InlineTip = forwardRef(
7273
- ({ variant = "tip", label, className, children, ...props }, ref) => {
7274
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
7275
- return /* @__PURE__ */ jsxs(
7276
- "div",
7277
- {
7278
- ref,
7279
- className: clx(
7280
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
7281
- className
7282
- ),
7283
- ...props,
7284
- children: [
7285
- /* @__PURE__ */ jsx(
7286
- "div",
7287
- {
7288
- role: "presentation",
7289
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
7290
- "bg-ui-tag-orange-icon": variant === "warning"
7291
- })
7292
- }
7293
- ),
7294
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
7295
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
7296
- labelValue,
7297
- ":"
7298
- ] }),
7299
- " ",
7300
- children
7301
- ] })
7302
- ]
7303
- }
7304
- );
7305
- }
7306
- );
7307
- InlineTip.displayName = "InlineTip";
7308
- const MetadataFieldSchema = z.object({
7309
- key: z.string(),
7310
- disabled: z.boolean().optional(),
7311
- value: z.any()
7312
- });
7313
- const MetadataSchema = z.object({
7314
- metadata: z.array(MetadataFieldSchema)
7315
- });
7316
- const Metadata = () => {
7317
- const { id } = useParams();
7318
- const { order, isPending, isError, error } = useOrder(id, {
7319
- fields: "metadata"
7320
- });
7321
- if (isError) {
7322
- throw error;
7323
- }
7324
- const isReady = !isPending && !!order;
7325
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
7326
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
7327
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
7328
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
7329
- ] }),
7330
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order?.metadata })
7331
- ] });
7332
- };
7333
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
7334
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
7335
- const MetadataForm = ({ orderId, metadata }) => {
7336
- const { handleSuccess } = useRouteModal();
7337
- const hasUneditableRows = getHasUneditableRows(metadata);
7338
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
7339
- const form = useForm({
7340
- defaultValues: {
7341
- metadata: getDefaultValues(metadata)
7342
- },
7343
- resolver: zodResolver(MetadataSchema)
7344
- });
7345
- const handleSubmit = form.handleSubmit(async (data) => {
7346
- const parsedData = parseValues(data);
7347
- await mutateAsync(
7348
- {
7349
- metadata: parsedData
7350
- },
7351
- {
7352
- onSuccess: () => {
7353
- toast.success("Metadata updated");
7354
- handleSuccess();
7355
- },
7356
- onError: (error) => {
7357
- toast.error(error.message);
7358
- }
7359
- }
7360
- );
7361
- });
7362
- const { fields, insert, remove } = useFieldArray({
7363
- control: form.control,
7364
- name: "metadata"
7365
- });
7366
- function deleteRow(index) {
7367
- remove(index);
7368
- if (fields.length === 1) {
7369
- insert(0, {
7370
- key: "",
7371
- value: "",
7372
- disabled: false
7373
- });
7374
- }
7375
- }
7376
- function insertRow(index, position) {
7377
- insert(index + (position === "above" ? 0 : 1), {
7378
- key: "",
7379
- value: "",
7380
- disabled: false
7381
- });
7382
- }
7383
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
7384
- KeyboundForm,
7385
- {
7386
- onSubmit: handleSubmit,
7387
- className: "flex flex-1 flex-col overflow-hidden",
7388
- children: [
7389
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
7390
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
7391
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
7392
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
7393
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
7394
- ] }),
7395
- fields.map((field, index) => {
7396
- const isDisabled = field.disabled || false;
7397
- let placeholder = "-";
7398
- if (typeof field.value === "object") {
7399
- placeholder = "{ ... }";
7400
- }
7401
- if (Array.isArray(field.value)) {
7402
- placeholder = "[ ... ]";
7403
- }
7404
- return /* @__PURE__ */ jsx(
7405
- ConditionalTooltip,
7406
- {
7407
- showTooltip: isDisabled,
7408
- content: "This row is disabled because it contains non-primitive data.",
7409
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
7410
- /* @__PURE__ */ jsxs(
7411
- "div",
7412
- {
7413
- className: clx("grid grid-cols-2 divide-x", {
7414
- "overflow-hidden rounded-b-lg": index === fields.length - 1
7415
- }),
7416
- children: [
7417
- /* @__PURE__ */ jsx(
7418
- Form$2.Field,
7419
- {
7420
- control: form.control,
7421
- name: `metadata.${index}.key`,
7422
- render: ({ field: field2 }) => {
7423
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7424
- GridInput,
7425
- {
7426
- "aria-labelledby": METADATA_KEY_LABEL_ID,
7427
- ...field2,
7428
- disabled: isDisabled,
7429
- placeholder: "Key"
7430
- }
7431
- ) }) });
7432
- }
7433
- }
7434
- ),
7435
- /* @__PURE__ */ jsx(
7436
- Form$2.Field,
7437
- {
7438
- control: form.control,
7439
- name: `metadata.${index}.value`,
7440
- render: ({ field: { value, ...field2 } }) => {
7441
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7442
- GridInput,
7443
- {
7444
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
7445
- ...field2,
7446
- value: isDisabled ? placeholder : value,
7447
- disabled: isDisabled,
7448
- placeholder: "Value"
7449
- }
7450
- ) }) });
7451
- }
7452
- }
7453
- )
7454
- ]
7455
- }
7456
- ),
7457
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
7458
- /* @__PURE__ */ jsx(
7459
- DropdownMenu.Trigger,
7460
- {
7461
- className: clx(
7462
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
7463
- {
7464
- hidden: isDisabled
7465
- }
7466
- ),
7467
- disabled: isDisabled,
7468
- asChild: true,
7469
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
7470
- }
7471
- ),
7472
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
7473
- /* @__PURE__ */ jsxs(
7474
- DropdownMenu.Item,
7475
- {
7476
- className: "gap-x-2",
7477
- onClick: () => insertRow(index, "above"),
7478
- children: [
7479
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
7480
- "Insert row above"
7481
- ]
7482
- }
7483
- ),
7484
- /* @__PURE__ */ jsxs(
7485
- DropdownMenu.Item,
7486
- {
7487
- className: "gap-x-2",
7488
- onClick: () => insertRow(index, "below"),
7489
- children: [
7490
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
7491
- "Insert row below"
7492
- ]
7493
- }
7494
- ),
7495
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
7496
- /* @__PURE__ */ jsxs(
7497
- DropdownMenu.Item,
7498
- {
7499
- className: "gap-x-2",
7500
- onClick: () => deleteRow(index),
7501
- children: [
7502
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
7503
- "Delete row"
7504
- ]
7505
- }
7506
- )
7507
- ] })
7508
- ] })
7509
- ] })
7510
- },
7511
- field.id
7512
- );
7513
- })
7514
- ] }),
7515
- hasUneditableRows && /* @__PURE__ */ jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
7516
- ] }),
7517
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
7518
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
7519
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
7520
- ] }) })
7521
- ]
7522
- }
7523
- ) });
7524
- };
7525
- const GridInput = forwardRef(({ className, ...props }, ref) => {
7526
- return /* @__PURE__ */ jsx(
7527
- "input",
7528
- {
7529
- ref,
7530
- ...props,
7531
- autoComplete: "off",
7532
- className: clx(
7533
- "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
7534
- className
7535
- )
7536
- }
7537
- );
7538
- });
7539
- GridInput.displayName = "MetadataForm.GridInput";
7540
- const PlaceholderInner = () => {
7541
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
7542
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
7543
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
7544
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
7545
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
7546
- ] }) })
7547
- ] });
7548
- };
7549
- const EDITABLE_TYPES = ["string", "number", "boolean"];
7550
- function getDefaultValues(metadata) {
7551
- if (!metadata || !Object.keys(metadata).length) {
7552
- return [
7553
- {
7554
- key: "",
7555
- value: "",
7556
- disabled: false
7557
- }
7558
- ];
7559
- }
7560
- return Object.entries(metadata).map(([key, value]) => {
7561
- if (!EDITABLE_TYPES.includes(typeof value)) {
7562
- return {
7563
- key,
7564
- value,
7565
- disabled: true
7566
- };
7567
- }
7568
- let stringValue = value;
7569
- if (typeof value !== "string") {
7570
- stringValue = JSON.stringify(value);
7571
- }
7572
- return {
7573
- key,
7574
- value: stringValue,
7575
- original_key: key
7576
- };
7577
- });
7578
- }
7579
- function parseValues(values) {
7580
- const metadata = values.metadata;
7581
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
7582
- if (isEmpty) {
7583
- return null;
7584
- }
7585
- const update = {};
7586
- metadata.forEach((field) => {
7587
- let key = field.key;
7588
- let value = field.value;
7589
- const disabled = field.disabled;
7590
- if (!key || !value) {
7591
- return;
7592
- }
7593
- if (disabled) {
7594
- update[key] = value;
7595
- return;
7596
- }
7597
- key = key.trim();
7598
- value = value.trim();
7599
- if (value === "true") {
7600
- update[key] = true;
7601
- } else if (value === "false") {
7602
- update[key] = false;
7603
- } else {
7604
- const parsedNumber = parseFloat(value);
7605
- if (!isNaN(parsedNumber)) {
7606
- update[key] = parsedNumber;
7607
- } else {
7608
- update[key] = value;
7609
- }
7610
- }
7611
- });
7612
- return update;
7613
- }
7614
- function getHasUneditableRows(metadata) {
7615
- if (!metadata) {
7616
- return false;
7617
- }
7618
- return Object.values(metadata).some(
7619
- (value) => !EDITABLE_TYPES.includes(typeof value)
7620
- );
7621
- }
7622
7272
  const PROMOTION_QUERY_KEY = "promotions";
7623
7273
  const promotionsQueryKeys = {
7624
7274
  list: (query2) => [
@@ -7891,9 +7541,115 @@ function getPromotionIds(items, shippingMethods) {
7891
7541
  }
7892
7542
  }
7893
7543
  }
7894
- }
7895
- return Array.from(promotionIds);
7896
- }
7544
+ }
7545
+ return Array.from(promotionIds);
7546
+ }
7547
+ const SalesChannel = () => {
7548
+ const { id } = useParams();
7549
+ const { draft_order, isPending, isError, error } = useDraftOrder(
7550
+ id,
7551
+ {
7552
+ fields: "+sales_channel_id"
7553
+ },
7554
+ {
7555
+ enabled: !!id
7556
+ }
7557
+ );
7558
+ if (isError) {
7559
+ throw error;
7560
+ }
7561
+ const ISrEADY = !!draft_order && !isPending;
7562
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
7563
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
7564
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
7565
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
7566
+ ] }),
7567
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
7568
+ ] });
7569
+ };
7570
+ const SalesChannelForm = ({ order }) => {
7571
+ const form = useForm({
7572
+ defaultValues: {
7573
+ sales_channel_id: order.sales_channel_id || ""
7574
+ },
7575
+ resolver: zodResolver(schema$2)
7576
+ });
7577
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
7578
+ const { handleSuccess } = useRouteModal();
7579
+ const onSubmit = form.handleSubmit(async (data) => {
7580
+ await mutateAsync(
7581
+ {
7582
+ sales_channel_id: data.sales_channel_id
7583
+ },
7584
+ {
7585
+ onSuccess: () => {
7586
+ toast.success("Sales channel updated");
7587
+ handleSuccess();
7588
+ },
7589
+ onError: (error) => {
7590
+ toast.error(error.message);
7591
+ }
7592
+ }
7593
+ );
7594
+ });
7595
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
7596
+ KeyboundForm,
7597
+ {
7598
+ className: "flex flex-1 flex-col overflow-hidden",
7599
+ onSubmit,
7600
+ children: [
7601
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
7602
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
7603
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7604
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
7605
+ ] }) })
7606
+ ]
7607
+ }
7608
+ ) });
7609
+ };
7610
+ const SalesChannelField = ({ control, order }) => {
7611
+ const salesChannels = useComboboxData({
7612
+ queryFn: async (params) => {
7613
+ return await sdk.admin.salesChannel.list(params);
7614
+ },
7615
+ queryKey: ["sales-channels"],
7616
+ getOptions: (data) => {
7617
+ return data.sales_channels.map((salesChannel) => ({
7618
+ label: salesChannel.name,
7619
+ value: salesChannel.id
7620
+ }));
7621
+ },
7622
+ defaultValue: order.sales_channel_id || void 0
7623
+ });
7624
+ return /* @__PURE__ */ jsx(
7625
+ Form$2.Field,
7626
+ {
7627
+ control,
7628
+ name: "sales_channel_id",
7629
+ render: ({ field }) => {
7630
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
7631
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
7632
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7633
+ Combobox,
7634
+ {
7635
+ options: salesChannels.options,
7636
+ fetchNextPage: salesChannels.fetchNextPage,
7637
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
7638
+ searchValue: salesChannels.searchValue,
7639
+ onSearchValueChange: salesChannels.onSearchValueChange,
7640
+ placeholder: "Select sales channel",
7641
+ ...field
7642
+ }
7643
+ ) }),
7644
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
7645
+ ] });
7646
+ }
7647
+ }
7648
+ );
7649
+ };
7650
+ const schema$2 = z.object({
7651
+ sales_channel_id: z.string().min(1)
7652
+ });
7897
7653
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
7898
7654
  const Shipping = () => {
7899
7655
  const { id } = useParams();
@@ -8719,7 +8475,7 @@ const ShippingAddressForm = ({ order }) => {
8719
8475
  postal_code: order.shipping_address?.postal_code ?? "",
8720
8476
  phone: order.shipping_address?.phone ?? ""
8721
8477
  },
8722
- resolver: zodResolver(schema$2)
8478
+ resolver: zodResolver(schema$1)
8723
8479
  });
8724
8480
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
8725
8481
  const { handleSuccess } = useRouteModal();
@@ -8889,113 +8645,7 @@ const ShippingAddressForm = ({ order }) => {
8889
8645
  }
8890
8646
  ) });
8891
8647
  };
8892
- const schema$2 = addressSchema;
8893
- const SalesChannel = () => {
8894
- const { id } = useParams();
8895
- const { draft_order, isPending, isError, error } = useDraftOrder(
8896
- id,
8897
- {
8898
- fields: "+sales_channel_id"
8899
- },
8900
- {
8901
- enabled: !!id
8902
- }
8903
- );
8904
- if (isError) {
8905
- throw error;
8906
- }
8907
- const ISrEADY = !!draft_order && !isPending;
8908
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
8909
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
8910
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
8911
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
8912
- ] }),
8913
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
8914
- ] });
8915
- };
8916
- const SalesChannelForm = ({ order }) => {
8917
- const form = useForm({
8918
- defaultValues: {
8919
- sales_channel_id: order.sales_channel_id || ""
8920
- },
8921
- resolver: zodResolver(schema$1)
8922
- });
8923
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
8924
- const { handleSuccess } = useRouteModal();
8925
- const onSubmit = form.handleSubmit(async (data) => {
8926
- await mutateAsync(
8927
- {
8928
- sales_channel_id: data.sales_channel_id
8929
- },
8930
- {
8931
- onSuccess: () => {
8932
- toast.success("Sales channel updated");
8933
- handleSuccess();
8934
- },
8935
- onError: (error) => {
8936
- toast.error(error.message);
8937
- }
8938
- }
8939
- );
8940
- });
8941
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
8942
- KeyboundForm,
8943
- {
8944
- className: "flex flex-1 flex-col overflow-hidden",
8945
- onSubmit,
8946
- children: [
8947
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
8948
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
8949
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
8950
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
8951
- ] }) })
8952
- ]
8953
- }
8954
- ) });
8955
- };
8956
- const SalesChannelField = ({ control, order }) => {
8957
- const salesChannels = useComboboxData({
8958
- queryFn: async (params) => {
8959
- return await sdk.admin.salesChannel.list(params);
8960
- },
8961
- queryKey: ["sales-channels"],
8962
- getOptions: (data) => {
8963
- return data.sales_channels.map((salesChannel) => ({
8964
- label: salesChannel.name,
8965
- value: salesChannel.id
8966
- }));
8967
- },
8968
- defaultValue: order.sales_channel_id || void 0
8969
- });
8970
- return /* @__PURE__ */ jsx(
8971
- Form$2.Field,
8972
- {
8973
- control,
8974
- name: "sales_channel_id",
8975
- render: ({ field }) => {
8976
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
8977
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
8978
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
8979
- Combobox,
8980
- {
8981
- options: salesChannels.options,
8982
- fetchNextPage: salesChannels.fetchNextPage,
8983
- isFetchingNextPage: salesChannels.isFetchingNextPage,
8984
- searchValue: salesChannels.searchValue,
8985
- onSearchValueChange: salesChannels.onSearchValueChange,
8986
- placeholder: "Select sales channel",
8987
- ...field
8988
- }
8989
- ) }),
8990
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
8991
- ] });
8992
- }
8993
- }
8994
- );
8995
- };
8996
- const schema$1 = z.object({
8997
- sales_channel_id: z.string().min(1)
8998
- });
8648
+ const schema$1 = addressSchema;
8999
8649
  const TransferOwnership = () => {
9000
8650
  const { id } = useParams();
9001
8651
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -9466,11 +9116,361 @@ const Illustration = () => {
9466
9116
  ] })
9467
9117
  ]
9468
9118
  }
9469
- );
9119
+ );
9120
+ };
9121
+ const schema = z.object({
9122
+ customer_id: z.string().min(1)
9123
+ });
9124
+ const InlineTip = forwardRef(
9125
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9126
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9127
+ return /* @__PURE__ */ jsxs(
9128
+ "div",
9129
+ {
9130
+ ref,
9131
+ className: clx(
9132
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9133
+ className
9134
+ ),
9135
+ ...props,
9136
+ children: [
9137
+ /* @__PURE__ */ jsx(
9138
+ "div",
9139
+ {
9140
+ role: "presentation",
9141
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9142
+ "bg-ui-tag-orange-icon": variant === "warning"
9143
+ })
9144
+ }
9145
+ ),
9146
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9147
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9148
+ labelValue,
9149
+ ":"
9150
+ ] }),
9151
+ " ",
9152
+ children
9153
+ ] })
9154
+ ]
9155
+ }
9156
+ );
9157
+ }
9158
+ );
9159
+ InlineTip.displayName = "InlineTip";
9160
+ const MetadataFieldSchema = z.object({
9161
+ key: z.string(),
9162
+ disabled: z.boolean().optional(),
9163
+ value: z.any()
9164
+ });
9165
+ const MetadataSchema = z.object({
9166
+ metadata: z.array(MetadataFieldSchema)
9167
+ });
9168
+ const Metadata = () => {
9169
+ const { id } = useParams();
9170
+ const { order, isPending, isError, error } = useOrder(id, {
9171
+ fields: "metadata"
9172
+ });
9173
+ if (isError) {
9174
+ throw error;
9175
+ }
9176
+ const isReady = !isPending && !!order;
9177
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9178
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9179
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9180
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9181
+ ] }),
9182
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order?.metadata })
9183
+ ] });
9184
+ };
9185
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9186
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9187
+ const MetadataForm = ({ orderId, metadata }) => {
9188
+ const { handleSuccess } = useRouteModal();
9189
+ const hasUneditableRows = getHasUneditableRows(metadata);
9190
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9191
+ const form = useForm({
9192
+ defaultValues: {
9193
+ metadata: getDefaultValues(metadata)
9194
+ },
9195
+ resolver: zodResolver(MetadataSchema)
9196
+ });
9197
+ const handleSubmit = form.handleSubmit(async (data) => {
9198
+ const parsedData = parseValues(data);
9199
+ await mutateAsync(
9200
+ {
9201
+ metadata: parsedData
9202
+ },
9203
+ {
9204
+ onSuccess: () => {
9205
+ toast.success("Metadata updated");
9206
+ handleSuccess();
9207
+ },
9208
+ onError: (error) => {
9209
+ toast.error(error.message);
9210
+ }
9211
+ }
9212
+ );
9213
+ });
9214
+ const { fields, insert, remove } = useFieldArray({
9215
+ control: form.control,
9216
+ name: "metadata"
9217
+ });
9218
+ function deleteRow(index) {
9219
+ remove(index);
9220
+ if (fields.length === 1) {
9221
+ insert(0, {
9222
+ key: "",
9223
+ value: "",
9224
+ disabled: false
9225
+ });
9226
+ }
9227
+ }
9228
+ function insertRow(index, position) {
9229
+ insert(index + (position === "above" ? 0 : 1), {
9230
+ key: "",
9231
+ value: "",
9232
+ disabled: false
9233
+ });
9234
+ }
9235
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9236
+ KeyboundForm,
9237
+ {
9238
+ onSubmit: handleSubmit,
9239
+ className: "flex flex-1 flex-col overflow-hidden",
9240
+ children: [
9241
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9242
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9243
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9244
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
9245
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
9246
+ ] }),
9247
+ fields.map((field, index) => {
9248
+ const isDisabled = field.disabled || false;
9249
+ let placeholder = "-";
9250
+ if (typeof field.value === "object") {
9251
+ placeholder = "{ ... }";
9252
+ }
9253
+ if (Array.isArray(field.value)) {
9254
+ placeholder = "[ ... ]";
9255
+ }
9256
+ return /* @__PURE__ */ jsx(
9257
+ ConditionalTooltip,
9258
+ {
9259
+ showTooltip: isDisabled,
9260
+ content: "This row is disabled because it contains non-primitive data.",
9261
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9262
+ /* @__PURE__ */ jsxs(
9263
+ "div",
9264
+ {
9265
+ className: clx("grid grid-cols-2 divide-x", {
9266
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9267
+ }),
9268
+ children: [
9269
+ /* @__PURE__ */ jsx(
9270
+ Form$2.Field,
9271
+ {
9272
+ control: form.control,
9273
+ name: `metadata.${index}.key`,
9274
+ render: ({ field: field2 }) => {
9275
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9276
+ GridInput,
9277
+ {
9278
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9279
+ ...field2,
9280
+ disabled: isDisabled,
9281
+ placeholder: "Key"
9282
+ }
9283
+ ) }) });
9284
+ }
9285
+ }
9286
+ ),
9287
+ /* @__PURE__ */ jsx(
9288
+ Form$2.Field,
9289
+ {
9290
+ control: form.control,
9291
+ name: `metadata.${index}.value`,
9292
+ render: ({ field: { value, ...field2 } }) => {
9293
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9294
+ GridInput,
9295
+ {
9296
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
9297
+ ...field2,
9298
+ value: isDisabled ? placeholder : value,
9299
+ disabled: isDisabled,
9300
+ placeholder: "Value"
9301
+ }
9302
+ ) }) });
9303
+ }
9304
+ }
9305
+ )
9306
+ ]
9307
+ }
9308
+ ),
9309
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
9310
+ /* @__PURE__ */ jsx(
9311
+ DropdownMenu.Trigger,
9312
+ {
9313
+ className: clx(
9314
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9315
+ {
9316
+ hidden: isDisabled
9317
+ }
9318
+ ),
9319
+ disabled: isDisabled,
9320
+ asChild: true,
9321
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
9322
+ }
9323
+ ),
9324
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
9325
+ /* @__PURE__ */ jsxs(
9326
+ DropdownMenu.Item,
9327
+ {
9328
+ className: "gap-x-2",
9329
+ onClick: () => insertRow(index, "above"),
9330
+ children: [
9331
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
9332
+ "Insert row above"
9333
+ ]
9334
+ }
9335
+ ),
9336
+ /* @__PURE__ */ jsxs(
9337
+ DropdownMenu.Item,
9338
+ {
9339
+ className: "gap-x-2",
9340
+ onClick: () => insertRow(index, "below"),
9341
+ children: [
9342
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
9343
+ "Insert row below"
9344
+ ]
9345
+ }
9346
+ ),
9347
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
9348
+ /* @__PURE__ */ jsxs(
9349
+ DropdownMenu.Item,
9350
+ {
9351
+ className: "gap-x-2",
9352
+ onClick: () => deleteRow(index),
9353
+ children: [
9354
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
9355
+ "Delete row"
9356
+ ]
9357
+ }
9358
+ )
9359
+ ] })
9360
+ ] })
9361
+ ] })
9362
+ },
9363
+ field.id
9364
+ );
9365
+ })
9366
+ ] }),
9367
+ hasUneditableRows && /* @__PURE__ */ jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
9368
+ ] }),
9369
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9370
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
9371
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9372
+ ] }) })
9373
+ ]
9374
+ }
9375
+ ) });
9470
9376
  };
9471
- const schema = z.object({
9472
- customer_id: z.string().min(1)
9377
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
9378
+ return /* @__PURE__ */ jsx(
9379
+ "input",
9380
+ {
9381
+ ref,
9382
+ ...props,
9383
+ autoComplete: "off",
9384
+ className: clx(
9385
+ "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
9386
+ className
9387
+ )
9388
+ }
9389
+ );
9473
9390
  });
9391
+ GridInput.displayName = "MetadataForm.GridInput";
9392
+ const PlaceholderInner = () => {
9393
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
9394
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
9395
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9396
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
9397
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
9398
+ ] }) })
9399
+ ] });
9400
+ };
9401
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
9402
+ function getDefaultValues(metadata) {
9403
+ if (!metadata || !Object.keys(metadata).length) {
9404
+ return [
9405
+ {
9406
+ key: "",
9407
+ value: "",
9408
+ disabled: false
9409
+ }
9410
+ ];
9411
+ }
9412
+ return Object.entries(metadata).map(([key, value]) => {
9413
+ if (!EDITABLE_TYPES.includes(typeof value)) {
9414
+ return {
9415
+ key,
9416
+ value,
9417
+ disabled: true
9418
+ };
9419
+ }
9420
+ let stringValue = value;
9421
+ if (typeof value !== "string") {
9422
+ stringValue = JSON.stringify(value);
9423
+ }
9424
+ return {
9425
+ key,
9426
+ value: stringValue,
9427
+ original_key: key
9428
+ };
9429
+ });
9430
+ }
9431
+ function parseValues(values) {
9432
+ const metadata = values.metadata;
9433
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
9434
+ if (isEmpty) {
9435
+ return null;
9436
+ }
9437
+ const update = {};
9438
+ metadata.forEach((field) => {
9439
+ let key = field.key;
9440
+ let value = field.value;
9441
+ const disabled = field.disabled;
9442
+ if (!key || !value) {
9443
+ return;
9444
+ }
9445
+ if (disabled) {
9446
+ update[key] = value;
9447
+ return;
9448
+ }
9449
+ key = key.trim();
9450
+ value = value.trim();
9451
+ if (value === "true") {
9452
+ update[key] = true;
9453
+ } else if (value === "false") {
9454
+ update[key] = false;
9455
+ } else {
9456
+ const parsedNumber = parseFloat(value);
9457
+ if (!isNaN(parsedNumber)) {
9458
+ update[key] = parsedNumber;
9459
+ } else {
9460
+ update[key] = value;
9461
+ }
9462
+ }
9463
+ });
9464
+ return update;
9465
+ }
9466
+ function getHasUneditableRows(metadata) {
9467
+ if (!metadata) {
9468
+ return false;
9469
+ }
9470
+ return Object.values(metadata).some(
9471
+ (value) => !EDITABLE_TYPES.includes(typeof value)
9472
+ );
9473
+ }
9474
9474
  const widgetModule = { widgets: [] };
9475
9475
  const routeModule = {
9476
9476
  routes: [
@@ -9507,14 +9507,14 @@ const routeModule = {
9507
9507
  Component: Items,
9508
9508
  path: "/draft-orders/:id/items"
9509
9509
  },
9510
- {
9511
- Component: Metadata,
9512
- path: "/draft-orders/:id/metadata"
9513
- },
9514
9510
  {
9515
9511
  Component: Promotions,
9516
9512
  path: "/draft-orders/:id/promotions"
9517
9513
  },
9514
+ {
9515
+ Component: SalesChannel,
9516
+ path: "/draft-orders/:id/sales-channel"
9517
+ },
9518
9518
  {
9519
9519
  Component: Shipping,
9520
9520
  path: "/draft-orders/:id/shipping"
@@ -9523,13 +9523,13 @@ const routeModule = {
9523
9523
  Component: ShippingAddress,
9524
9524
  path: "/draft-orders/:id/shipping-address"
9525
9525
  },
9526
- {
9527
- Component: SalesChannel,
9528
- path: "/draft-orders/:id/sales-channel"
9529
- },
9530
9526
  {
9531
9527
  Component: TransferOwnership,
9532
9528
  path: "/draft-orders/:id/transfer-ownership"
9529
+ },
9530
+ {
9531
+ Component: Metadata,
9532
+ path: "/draft-orders/:id/metadata"
9533
9533
  }
9534
9534
  ]
9535
9535
  }