@drawbridge/components 0.1.16 → 0.1.18

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.
@@ -4,6 +4,7 @@ import Link from "./link.mjs";
4
4
  import { Fragment, useCallback, useEffect, useRef, useState } from "react";
5
5
  import { constants } from "@drawbridge/drawbridge-utils";
6
6
  import { request } from "@drawbridge/drawbridge-utils/fetch";
7
+ import { escapeHtml } from "@drawbridge/drawbridge-utils/html";
7
8
  import { useForm, useWatch, Controller } from "../shared/lib/form.mjs";
8
9
  import { keepPreviousData, useQuery, useQueryClient } from "@tanstack/react-query";
9
10
  import yup, { resolver } from "../shared/lib/yup.mjs";
@@ -16,6 +17,7 @@ import InputUrl from "../shared/input-url.mjs";
16
17
  import InputResource from "./input-resource.mjs";
17
18
  import InputCombobox from "./input-combobox.mjs";
18
19
  import { FormNotification } from "./form-notification.mjs";
20
+ import InputEditor, { EditorView, plainLine } from "./input-editor.mjs";
19
21
  import { InputArrayButton, InputArrayControl, InputArraySortable } from "./input-array.mjs";
20
22
  import Badge from "../shared/badge.mjs";
21
23
  import InputSwitch from "../shared/input-switch.mjs";
@@ -31,9 +33,10 @@ const emailSteps = ["step.email.send"];
31
33
  const teamSteps = ["step.email.notify"];
32
34
  const messageSteps = [...emailSteps, "step.sms.send"];
33
35
  const renderedSteps = [...emailSteps, ...teamSteps];
36
+ const RESPONSE = "step.response.send";
34
37
  const firstEntryOnly = ({ campaign, event, type }) => {
35
38
  var _a, _b;
36
- if (event !== "lead.insert" || !type || emailSteps.includes(type)) return false;
39
+ if (event !== "lead.insert" || !type || emailSteps.includes(type) || type === RESPONSE) return false;
37
40
  if (type === "step.sms.send") return (((_a = campaign == null ? void 0 : campaign.form) == null ? void 0 : _a.submissionLeadPrimaryKey) ?? ((_b = campaign == null ? void 0 : campaign.settings) == null ? void 0 : _b.submissionLeadPrimaryKey)) !== "phone";
38
41
  return true;
39
42
  };
@@ -56,20 +59,23 @@ const memberParams = {
56
59
  createdAt: -1
57
60
  }
58
61
  };
62
+ const memberOptions = (organization) => ({
63
+ queryKey: ["organization-members", organization, "options"],
64
+ queryFn: async () => {
65
+ const { result } = await request({
66
+ endpoint: "/organization/" + organization + "/members",
67
+ // limit / skip / sort are required by the route's query validation.
68
+ params: memberParams
69
+ });
70
+ return result;
71
+ },
72
+ staleTime: Infinity
73
+ });
59
74
  const useOrganizationMembers = (organization) => {
60
75
  var _a;
61
76
  const members = useQuery({
62
- queryKey: ["organization-members", organization, "options"],
63
- queryFn: async () => {
64
- const { result } = await request({
65
- endpoint: "/organization/" + organization + "/members",
66
- // limit / skip / sort are required by the route's query validation.
67
- params: memberParams
68
- });
69
- return result;
70
- },
71
- enabled: Boolean(organization),
72
- staleTime: Infinity
77
+ ...memberOptions(organization),
78
+ enabled: Boolean(organization)
73
79
  });
74
80
  return {
75
81
  items: ((_a = members.data) == null ? void 0 : _a.items) || [],
@@ -111,8 +117,108 @@ const stepReason = ({ entry, saved = [], type }) => {
111
117
  var _a, _b;
112
118
  return ((_a = ((entry == null ? void 0 : entry.steps) || []).find((step) => (step == null ? void 0 : step.value) === type && (step == null ? void 0 : step.reason))) == null ? void 0 : _a.reason) || ((_b = saved.find((step) => (step == null ? void 0 : step.type) === type && (step == null ? void 0 : step.reason))) == null ? void 0 : _b.reason) || null;
113
119
  };
120
+ const responseStart = (number) => ({
121
+ email: {
122
+ enabled: true,
123
+ message: constants.campaign.confirmation.message,
124
+ score: null,
125
+ subject: constants.campaign.confirmation.subject,
126
+ text: constants.campaign.confirmation.text
127
+ },
128
+ sms: {
129
+ enabled: number === "verified",
130
+ message: constants.campaign.confirmation.sms.giveaway
131
+ }
132
+ });
133
+ const responseParts = ({ autoResponse, campaign, organization, settings }) => {
134
+ var _a, _b;
135
+ const { fields, number } = autoResponse || {};
136
+ const base = "/organizations/" + organization;
137
+ const addField = {
138
+ href: base + "/campaigns/" + campaign + "/form",
139
+ text: "Add field"
140
+ };
141
+ const numberTask = () => {
142
+ if (number === "verified") return null;
143
+ if (number === "error") return {
144
+ button: {
145
+ href: base + "/settings",
146
+ text: "View number"
147
+ },
148
+ message: "Your SMS number wasn't approved.",
149
+ type: "error"
150
+ };
151
+ if (number) return {
152
+ message: "Your SMS number is waiting for approval.",
153
+ type: "warning"
154
+ };
155
+ return {
156
+ button: {
157
+ href: base + "/settings",
158
+ text: "Get a number"
159
+ },
160
+ message: "Texts need an SMS number."
161
+ };
162
+ };
163
+ const emailCan = Boolean(fields == null ? void 0 : fields.email);
164
+ const smsCan = Boolean(fields == null ? void 0 : fields.phone) && number === "verified";
165
+ return {
166
+ can: emailCan || smsCan,
167
+ email: {
168
+ can: emailCan,
169
+ hidden: !(fields == null ? void 0 : fields.email),
170
+ on: emailCan && ((_a = settings == null ? void 0 : settings.email) == null ? void 0 : _a.enabled) === true,
171
+ task: (fields == null ? void 0 : fields.email) ? null : {
172
+ button: addField,
173
+ message: "Emails need an Email field on the form."
174
+ }
175
+ },
176
+ sms: {
177
+ can: smsCan,
178
+ hidden: !(fields == null ? void 0 : fields.phone),
179
+ on: smsCan && ((_b = settings == null ? void 0 : settings.sms) == null ? void 0 : _b.enabled) === true,
180
+ task: (fields == null ? void 0 : fields.phone) ? numberTask() : {
181
+ button: addField,
182
+ message: "Texts need a Phone field on the form."
183
+ }
184
+ }
185
+ };
186
+ };
187
+ const stepAfterPart = ({ enabled, next, other }) => next || enabled !== false && other === true;
188
+ const partToStart = (parts) => {
189
+ if (parts.email.on || parts.sms.on) return null;
190
+ return parts.email.can ? "email" : "sms";
191
+ };
192
+ const issueRow = ({ issue, steps = [] }) => (issue == null ? void 0 : issue.step) ? steps.findIndex((step) => (step == null ? void 0 : step.type) === issue.step || (step == null ? void 0 : step.type) === RESPONSE && messageSteps.includes(issue.step)) : -1;
193
+ const scoreHolds = ({ message, score, subject }) => Boolean(score) && score.subject === subject && score.message === message;
194
+ const responseSave = ({ email, sms }) => ({
195
+ email: {
196
+ enabled: email.enabled,
197
+ message: email.message,
198
+ ...scoreHolds(email) && {
199
+ score: email.score
200
+ },
201
+ subject: email.subject,
202
+ ...email.text && {
203
+ text: email.text
204
+ }
205
+ },
206
+ sms: {
207
+ enabled: sms.enabled,
208
+ message: sms.message
209
+ }
210
+ });
211
+ const plainHtml = (text, variables) => String(text || "").split("\n").map((line) => {
212
+ const marked = line.match(/^\[(.+)\]$/);
213
+ return "<p>" + (marked ? '<span data-variable="placeholder" key="placeholder" label="' + escapeHtml(marked[1]) + '"></span>' : plainLine(line, variables)) + "</p>";
214
+ }).join("");
215
+ const taggedScore = (settings) => (settings == null ? void 0 : settings.score) ? {
216
+ ...settings.score,
217
+ message: (settings == null ? void 0 : settings.message) || "",
218
+ subject: (settings == null ? void 0 : settings.subject) || ""
219
+ } : null;
114
220
  const stepRow = (step = {}, members = []) => {
115
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
221
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s;
116
222
  return {
117
223
  connectionId: step.connectionId || "",
118
224
  discount: ((_a = step.settings) == null ? void 0 : _a.discount) || null,
@@ -125,28 +231,32 @@ const stepRow = (step = {}, members = []) => {
125
231
  // render as an unlabelled chip nobody could identify.
126
232
  members: (((_b = step.settings) == null ? void 0 : _b.members) || []).map((id) => members.find((member) => (member == null ? void 0 : member.id) === id)).filter(Boolean),
127
233
  message: ((_c = step.settings) == null ? void 0 : _c.message) || "",
128
- // THE LAST SPAM CHECK, RE-TAGGED WITH THE COPY IT MEASURED.
129
- //
130
- // The api stores a score without that copy and tests the tag again on the
131
- // way back in, so seeding a bare score would fail the test and clear the
132
- // stored result on every save. Putting the pair back on is the other half of
133
- // a round trip the api already expects — and it is what lets the step panel
134
- // open showing the last result instead of spending a request on a free
135
- // service to be told what is already in the fields.
136
- score: ((_d = step.settings) == null ? void 0 : _d.score) ? {
137
- ...step.settings.score,
138
- message: ((_e = step.settings) == null ? void 0 : _e.message) || "",
139
- subject: ((_f = step.settings) == null ? void 0 : _f.subject) || ""
234
+ score: taggedScore(step.settings),
235
+ // @story workflows/auto-response
236
+ // Its two parts; null on every other row, so every row holds the same keys.
237
+ settings: step.type === RESPONSE ? {
238
+ email: {
239
+ enabled: ((_e = (_d = step.settings) == null ? void 0 : _d.email) == null ? void 0 : _e.enabled) === true,
240
+ message: ((_g = (_f = step.settings) == null ? void 0 : _f.email) == null ? void 0 : _g.message) || "",
241
+ score: taggedScore((_h = step.settings) == null ? void 0 : _h.email),
242
+ subject: ((_j = (_i = step.settings) == null ? void 0 : _i.email) == null ? void 0 : _j.subject) || "",
243
+ text: ((_l = (_k = step.settings) == null ? void 0 : _k.email) == null ? void 0 : _l.text) || ""
244
+ },
245
+ sms: {
246
+ enabled: ((_n = (_m = step.settings) == null ? void 0 : _m.sms) == null ? void 0 : _n.enabled) === true,
247
+ message: ((_p = (_o = step.settings) == null ? void 0 : _o.sms) == null ? void 0 : _p.message) || ""
248
+ }
140
249
  } : null,
141
250
  slug: step.slug || "",
142
- subject: ((_g = step.settings) == null ? void 0 : _g.subject) || "",
251
+ subject: ((_q = step.settings) == null ? void 0 : _q.subject) || "",
143
252
  // The editor writes this only when the merchant touches it, so without a
144
253
  // seed an untouched save dropped the plain rendering of the message.
145
- text: ((_h = step.settings) == null ? void 0 : _h.text) || "",
254
+ text: ((_r = step.settings) == null ? void 0 : _r.text) || "",
146
255
  type: step.type || "",
147
- url: ((_i = step.settings) == null ? void 0 : _i.url) || ""
256
+ url: ((_s = step.settings) == null ? void 0 : _s.url) || ""
148
257
  };
149
258
  };
259
+ const hasWords = (value) => Boolean(String(value || "").replace(/<[^>]*>/g, "").trim());
150
260
  const workflowSchema = yup.object({
151
261
  steps: yup.array().of(
152
262
  yup.object({
@@ -164,11 +274,9 @@ const workflowSchema = yup.object({
164
274
  return schema;
165
275
  }),
166
276
  // @story workflows/edit
167
- // The editor writes an empty message as "<p></p>", which a plain
168
- // required() accepts: a message needs words, not only markup.
169
277
  message: yup.string().test("words", "Message is required", function(value) {
170
278
  var _a;
171
- return !messageSteps.includes((_a = this.parent) == null ? void 0 : _a.type) || Boolean(String(value || "").replace(/<[^>]*>/g, "").trim());
279
+ return !messageSteps.includes((_a = this.parent) == null ? void 0 : _a.type) || hasWords(value);
172
280
  }).when("type", ([type], schema) => {
173
281
  if (type === "step.sms.send") {
174
282
  return schema.required("Message is required").max(1500).label("Message");
@@ -178,6 +286,28 @@ const workflowSchema = yup.object({
178
286
  }
179
287
  return schema;
180
288
  }),
289
+ // @story workflows/auto-response
290
+ // A part's fields are required only while that part is on, with the
291
+ // limits the step declares in utils, so the api never refuses a save
292
+ // the form allowed.
293
+ settings: yup.object().nullable().when("type", ([type], schema) => type === RESPONSE ? schema.shape({
294
+ email: yup.object({
295
+ message: yup.string().when("enabled", {
296
+ is: true,
297
+ then: (part) => part.test("words", "Message is required", hasWords).max(2e3).label("Message")
298
+ }),
299
+ subject: yup.string().when("enabled", {
300
+ is: true,
301
+ then: (part) => part.required("Subject is required").max(150).label("Subject")
302
+ })
303
+ }),
304
+ sms: yup.object({
305
+ message: yup.string().when("enabled", {
306
+ is: true,
307
+ then: (part) => part.required("Message is required").max(1500).label("Message")
308
+ })
309
+ })
310
+ }) : schema),
181
311
  // No minimum. The organization owner is always a recipient, so a
182
312
  // step with no members chosen is complete — requiring one here
183
313
  // made the step unsaveable for a solo merchant, who has no
@@ -343,14 +473,340 @@ const StepPreview = ({ campaign, control, image, name, organization, type }) =>
343
473
  /* @__PURE__ */ jsx("p", { children: "No preview available" })
344
474
  ] });
345
475
  };
476
+ const LeadEmailNote = () => /* @__PURE__ */ jsx(
477
+ "p",
478
+ {
479
+ ...{
480
+ className: "text-sm text-gray-dark"
481
+ },
482
+ children: "Drawbridge adds the entrant's order and missions under your message. You can't edit or remove them."
483
+ }
484
+ );
485
+ const RESPONSE_STATES = [
486
+ {
487
+ key: "Email",
488
+ value: "email"
489
+ },
490
+ {
491
+ key: "Text",
492
+ value: "sms"
493
+ }
494
+ ];
495
+ const ResponseText = ({ className, id, text, variables }) => /* @__PURE__ */ jsx(
496
+ "div",
497
+ {
498
+ ...{
499
+ className,
500
+ id
501
+ },
502
+ children: /* @__PURE__ */ jsx(
503
+ EditorView,
504
+ {
505
+ ...{
506
+ value: plainHtml(text, variables)
507
+ }
508
+ }
509
+ )
510
+ }
511
+ );
512
+ const ResponseStepCard = ({
513
+ card,
514
+ children,
515
+ control,
516
+ disabled,
517
+ formState,
518
+ off,
519
+ onChecking,
520
+ response: {
521
+ autoResponse,
522
+ campaign,
523
+ enabled,
524
+ event,
525
+ id,
526
+ index,
527
+ organization,
528
+ settings,
529
+ title,
530
+ variables
531
+ },
532
+ setValue
533
+ }) => {
534
+ var _a, _b, _c, _d, _e;
535
+ const [picked, setPicked] = useState("email");
536
+ const name = `steps.${index}`;
537
+ const parts = responseParts({ autoResponse, campaign, organization, settings });
538
+ const shown = parts.email.hidden ? "sms" : parts.sms.hidden ? "email" : picked;
539
+ const placeholders = autoResponse == null ? void 0 : autoResponse.placeholders;
540
+ const preview = useEmailPreview({
541
+ campaign,
542
+ event: parts.email.hidden ? null : event,
543
+ message: (_a = settings == null ? void 0 : settings.email) == null ? void 0 : _a.message,
544
+ organization,
545
+ subject: (_b = settings == null ? void 0 : settings.email) == null ? void 0 : _b.subject,
546
+ text: (_c = settings == null ? void 0 : settings.email) == null ? void 0 : _c.text
547
+ });
548
+ const brand = smsBrandLine(title, (_d = settings == null ? void 0 : settings.sms) == null ? void 0 : _d.message);
549
+ const partSwitch = ({ label, message, other, part }) => /* @__PURE__ */ jsx(
550
+ Controller,
551
+ {
552
+ ...{
553
+ control,
554
+ name: name + ".settings." + part + ".enabled",
555
+ render: ({ field: { onChange, value } }) => /* @__PURE__ */ jsx(
556
+ InputGroup,
557
+ {
558
+ ...{
559
+ input: /* @__PURE__ */ jsx(
560
+ InputSwitch,
561
+ {
562
+ ...{
563
+ "aria-label": label,
564
+ disabled: disabled || !parts[part].can,
565
+ onChange: (next) => {
566
+ var _a2;
567
+ onChange(next);
568
+ setValue(name + ".enabled", stepAfterPart({ enabled, next, other: (_a2 = settings == null ? void 0 : settings[other]) == null ? void 0 : _a2.enabled }));
569
+ setPicked(part);
570
+ },
571
+ value: parts[part].can && value === true
572
+ }
573
+ }
574
+ ),
575
+ label,
576
+ message
577
+ }
578
+ }
579
+ )
580
+ }
581
+ }
582
+ );
583
+ return /* @__PURE__ */ jsxs(
584
+ CardSection,
585
+ {
586
+ ...{
587
+ ...card,
588
+ columns: 2
589
+ },
590
+ children: [
591
+ /* @__PURE__ */ jsxs(
592
+ "div",
593
+ {
594
+ ...{
595
+ className: "space-y-6" + (off ? " opacity-50" : "")
596
+ },
597
+ children: [
598
+ children,
599
+ /* @__PURE__ */ jsxs(
600
+ "div",
601
+ {
602
+ ...{
603
+ className: "pt-6 border-t border-gray space-y-6"
604
+ },
605
+ children: [
606
+ partSwitch({ label: "Email", other: "sms", part: "email" }),
607
+ parts.email.task && /* @__PURE__ */ jsx(CardTask, { ...parts.email.task }),
608
+ !parts.email.hidden && /* @__PURE__ */ jsxs(Fragment, { children: [
609
+ /* @__PURE__ */ jsxs(
610
+ "div",
611
+ {
612
+ ...{
613
+ className: "space-y-3" + (parts.email.on ? "" : " opacity-50")
614
+ },
615
+ children: [
616
+ /* @__PURE__ */ jsx(
617
+ FormNotification,
618
+ {
619
+ ...{
620
+ control,
621
+ disabled: disabled || !parts.email.on,
622
+ formState,
623
+ onChecking,
624
+ setValue,
625
+ values: {
626
+ id,
627
+ name: name + ".settings.email",
628
+ organization,
629
+ variables
630
+ }
631
+ }
632
+ }
633
+ ),
634
+ /* @__PURE__ */ jsx(LeadEmailNote, {})
635
+ ]
636
+ }
637
+ ),
638
+ /* @__PURE__ */ jsx(
639
+ InputGroup,
640
+ {
641
+ ...{
642
+ input: /* @__PURE__ */ jsx(
643
+ ResponseText,
644
+ {
645
+ ...{
646
+ className: "w-full border border-inherit rounded px-3 py-2 disabled",
647
+ text: preview == null ? void 0 : preview.text,
648
+ variables
649
+ }
650
+ }
651
+ ),
652
+ label: "Plain-text version"
653
+ }
654
+ }
655
+ )
656
+ ] })
657
+ ]
658
+ }
659
+ ),
660
+ /* @__PURE__ */ jsxs(
661
+ "div",
662
+ {
663
+ ...{
664
+ className: "mt-10 pt-10 border-t border-gray-dark space-y-6"
665
+ },
666
+ children: [
667
+ partSwitch({
668
+ label: "Text",
669
+ // The line goes with the part's fields when the form lacks the field.
670
+ message: !parts.sms.hidden && "Sends to entrants who agree to texts. Each text costs 2 actions, and a long text costs more.",
671
+ other: "email",
672
+ part: "sms"
673
+ }),
674
+ parts.sms.task && /* @__PURE__ */ jsx(CardTask, { ...parts.sms.task }),
675
+ !parts.sms.hidden && /* @__PURE__ */ jsx(
676
+ Controller,
677
+ {
678
+ ...{
679
+ control,
680
+ name: name + ".settings.sms.message",
681
+ render: ({ field: { onChange, value } }) => {
682
+ var _a2, _b2, _c2, _d2, _e2, _f;
683
+ return /* @__PURE__ */ jsx(
684
+ "div",
685
+ {
686
+ ...{
687
+ className: parts.sms.on ? "" : "opacity-50"
688
+ },
689
+ children: /* @__PURE__ */ jsx(
690
+ InputGroup,
691
+ {
692
+ ...{
693
+ error: (_f = (_e2 = (_d2 = (_c2 = (_b2 = (_a2 = formState == null ? void 0 : formState.errors) == null ? void 0 : _a2.steps) == null ? void 0 : _b2[index]) == null ? void 0 : _c2.settings) == null ? void 0 : _d2.sms) == null ? void 0 : _e2.message) == null ? void 0 : _f.message,
694
+ input: /* @__PURE__ */ jsx(
695
+ InputEditor,
696
+ {
697
+ ...{
698
+ disabled: disabled || !parts.sms.on,
699
+ ...(placeholders == null ? void 0 : placeholders.missions) && {
700
+ locked: /* @__PURE__ */ jsx(ResponseText, { ...{ text: "[" + placeholders.missions + "]" } })
701
+ },
702
+ onChange: ({ text }) => onChange(text),
703
+ plain: true,
704
+ value: value || "",
705
+ variables
706
+ }
707
+ }
708
+ ),
709
+ label: "Message"
710
+ }
711
+ }
712
+ )
713
+ }
714
+ );
715
+ }
716
+ }
717
+ }
718
+ )
719
+ ]
720
+ }
721
+ )
722
+ ]
723
+ }
724
+ ),
725
+ /* @__PURE__ */ jsx(
726
+ "div",
727
+ {
728
+ ...{
729
+ className: off || !parts[shown].on ? "opacity-50" : ""
730
+ },
731
+ children: /* @__PURE__ */ jsx(
732
+ Preview,
733
+ {
734
+ ...{
735
+ ...!parts.email.hidden && !parts.sms.hidden && {
736
+ states: {
737
+ onChange: setPicked,
738
+ options: RESPONSE_STATES,
739
+ value: shown
740
+ }
741
+ }
742
+ },
743
+ children: shown === "email" ? /* @__PURE__ */ jsx(
744
+ "iframe",
745
+ {
746
+ ...{
747
+ className: "block w-full h-full border-0 bg-white",
748
+ srcDoc: (preview == null ? void 0 : preview.html) || "",
749
+ title: "Auto response"
750
+ }
751
+ }
752
+ ) : /* @__PURE__ */ jsxs(TextPreview, { children: [
753
+ brand && /* @__PURE__ */ jsx("p", { children: brand }),
754
+ /* @__PURE__ */ jsx(
755
+ ResponseText,
756
+ {
757
+ ...{
758
+ text: (_e = settings == null ? void 0 : settings.sms) == null ? void 0 : _e.message,
759
+ variables
760
+ }
761
+ }
762
+ ),
763
+ (placeholders == null ? void 0 : placeholders.missions) && /* @__PURE__ */ jsx(
764
+ ResponseText,
765
+ {
766
+ ...{
767
+ className: "mt-2",
768
+ text: "[" + placeholders.missions + "]"
769
+ }
770
+ }
771
+ ),
772
+ /* @__PURE__ */ jsx("p", { className: "mt-2", children: "Reply STOP to opt out" })
773
+ ] })
774
+ }
775
+ )
776
+ }
777
+ )
778
+ ]
779
+ }
780
+ );
781
+ };
346
782
  const StepCard = ({
347
783
  children,
348
784
  control,
785
+ disabled,
349
786
  email,
787
+ formState,
350
788
  off,
789
+ onChecking,
351
790
  preview,
791
+ response,
792
+ setValue,
352
793
  ...card
353
- }) => email ? /* @__PURE__ */ jsx(
794
+ }) => response ? /* @__PURE__ */ jsx(
795
+ ResponseStepCard,
796
+ {
797
+ ...{
798
+ card,
799
+ control,
800
+ disabled,
801
+ formState,
802
+ off,
803
+ onChecking,
804
+ response,
805
+ setValue
806
+ },
807
+ children
808
+ }
809
+ ) : email ? /* @__PURE__ */ jsx(
354
810
  EmailStepCard,
355
811
  {
356
812
  ...{
@@ -397,7 +853,7 @@ const FormOrganizationWorkflowEdit = ({
397
853
  onSuccess,
398
854
  values
399
855
  }) => {
400
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
856
+ var _a, _b, _c, _d, _e, _f, _g, _h;
401
857
  const event = (_a = values == null ? void 0 : values.trigger) == null ? void 0 : _a.event;
402
858
  const { items: memberItems, loading: membersLoading } = useOrganizationMembers(values == null ? void 0 : values.organization);
403
859
  const queryClient = useQueryClient();
@@ -439,7 +895,7 @@ const FormOrganizationWorkflowEdit = ({
439
895
  control,
440
896
  name: "steps"
441
897
  });
442
- const issueIndex = ((_g = values == null ? void 0 : values.issue) == null ? void 0 : _g.step) ? (watchedSteps || []).findIndex((step) => (step == null ? void 0 : step.type) === values.issue.step) : -1;
898
+ const issueIndex = issueRow({ issue: values == null ? void 0 : values.issue, steps: watchedSteps || [] });
443
899
  const getAvailableVariables = (index) => stepVariables(catalog, watchedSteps, index);
444
900
  const loading = membersLoading || commerceConnection.isLoading;
445
901
  const prefilled = useRef(false);
@@ -573,13 +1029,14 @@ const FormOrganizationWorkflowEdit = ({
573
1029
  }) => {
574
1030
  try {
575
1031
  const body = {
576
- steps: steps.map(({ connectionId, discount, enabled, members, message, score, slug, subject, text, type, url }, index) => {
577
- const settings = {};
1032
+ steps: steps.map(({ connectionId, discount, enabled, members, message, score, settings: parts, slug, subject, text, type, url }, index) => {
1033
+ const settings = type === RESPONSE ? responseSave(parts) : {};
1034
+ if (type === RESPONSE && parts.email.score && !settings.email.score) setValue("steps." + index + ".settings.email.score", null);
578
1035
  if (type === "step.webhook.send") settings.url = url;
579
1036
  if (emailSteps.includes(type)) settings.subject = subject;
580
1037
  if (messageSteps.includes(type)) settings.message = message;
581
1038
  if (emailSteps.includes(type)) {
582
- if (score && score.subject === subject && score.message === message) settings.score = score;
1039
+ if (scoreHolds({ message, score, subject })) settings.score = score;
583
1040
  else if (score) setValue("steps." + index + ".score", null);
584
1041
  if (text) settings.text = text;
585
1042
  }
@@ -660,7 +1117,7 @@ const FormOrganizationWorkflowEdit = ({
660
1117
  InputGroup,
661
1118
  {
662
1119
  ...{
663
- error: (_i = (_h = formState == null ? void 0 : formState.errors) == null ? void 0 : _h.steps) == null ? void 0 : _i.message,
1120
+ error: (_h = (_g = formState == null ? void 0 : formState.errors) == null ? void 0 : _g.steps) == null ? void 0 : _h.message,
664
1121
  input: /* @__PURE__ */ jsx(
665
1122
  InputArraySortable,
666
1123
  {
@@ -681,6 +1138,12 @@ const FormOrganizationWorkflowEdit = ({
681
1138
  children: ({ field, handle, index, remove }) => {
682
1139
  var _a2;
683
1140
  const step = (watchedSteps == null ? void 0 : watchedSteps[index]) || field;
1141
+ const parts = step.type === RESPONSE && responseParts({
1142
+ autoResponse: values == null ? void 0 : values.autoResponse,
1143
+ campaign: values == null ? void 0 : values.campaign,
1144
+ organization: values == null ? void 0 : values.organization,
1145
+ settings: step.settings
1146
+ });
684
1147
  return (
685
1148
  // @story workflows/edit
686
1149
  // EACH STEP IS ITS OWN CARD in the flow under the Trigger card,
@@ -692,6 +1155,23 @@ const FormOrganizationWorkflowEdit = ({
692
1155
  {
693
1156
  ...{
694
1157
  control,
1158
+ disabled: disabled || (formState == null ? void 0 : formState.isSubmitting),
1159
+ formState,
1160
+ onChecking,
1161
+ setValue,
1162
+ // @story workflows/auto-response
1163
+ response: step.type === RESPONSE && {
1164
+ autoResponse: values == null ? void 0 : values.autoResponse,
1165
+ campaign: values == null ? void 0 : values.campaign,
1166
+ enabled: step.enabled,
1167
+ event,
1168
+ id: field.id,
1169
+ index,
1170
+ organization: values == null ? void 0 : values.organization,
1171
+ settings: step.settings,
1172
+ title: organizationData == null ? void 0 : organizationData.title,
1173
+ variables: getAvailableVariables(index)
1174
+ },
695
1175
  // @story workflows/send-email
696
1176
  // A Send email step shows its email beside its form.
697
1177
  off: step.enabled === false,
@@ -743,10 +1223,13 @@ const FormOrganizationWorkflowEdit = ({
743
1223
  {
744
1224
  ...{
745
1225
  "aria-label": ((_a3 = ((catalog == null ? void 0 : catalog.steps) || []).find((entry) => entry.value === step.type)) == null ? void 0 : _a3.key) || "Step " + (index + 1),
746
- disabled: disabled || (formState == null ? void 0 : formState.isSubmitting),
747
- onChange,
1226
+ disabled: disabled || (formState == null ? void 0 : formState.isSubmitting) || parts && !parts.can,
1227
+ onChange: (next) => {
1228
+ onChange(next);
1229
+ if (parts && next && partToStart(parts)) setValue(`steps.${index}.settings.${partToStart(parts)}.enabled`, true);
1230
+ },
748
1231
  small: true,
749
- value: value !== false
1232
+ value: value !== false && (!parts || parts.can)
750
1233
  }
751
1234
  }
752
1235
  );
@@ -776,7 +1259,8 @@ const FormOrganizationWorkflowEdit = ({
776
1259
  {
777
1260
  ...{
778
1261
  // A step switched off is greyed; its settings are kept.
779
- className: "space-y-3" + (step.enabled === false ? " opacity-50" : "")
1262
+ // Auto response's whole card greys, its parts as well.
1263
+ className: "space-y-3" + (step.enabled === false && !parts ? " opacity-50" : "")
780
1264
  },
781
1265
  children: [
782
1266
  issueIndex === index && /* @__PURE__ */ jsx(
@@ -805,9 +1289,11 @@ const FormOrganizationWorkflowEdit = ({
805
1289
  {
806
1290
  ...{
807
1291
  onChange: (val) => {
1292
+ var _a4;
808
1293
  const [type] = val.split("::");
809
1294
  const option = getStepOptions(index).find((o) => o.value === val);
810
1295
  const copy = pickedCopy({ row: getValues(`steps.${index}`), type });
1296
+ setValue(`steps.${index}.settings`, type === RESPONSE ? responseStart((_a4 = values == null ? void 0 : values.autoResponse) == null ? void 0 : _a4.number) : null);
811
1297
  onChange(type);
812
1298
  setValue(`steps.${index}.connectionId`, (option == null ? void 0 : option.connectionId) || "");
813
1299
  setValue(`steps.${index}.slug`, (option == null ? void 0 : option.slug) || "");
@@ -938,15 +1424,7 @@ const FormOrganizationWorkflowEdit = ({
938
1424
  }
939
1425
  }
940
1426
  ),
941
- event === "lead.insert" && emailSteps.includes(step.type) && /* @__PURE__ */ jsx(
942
- "p",
943
- {
944
- ...{
945
- className: "text-sm text-gray-dark"
946
- },
947
- children: "Drawbridge adds the entrant's order and missions under your message. You can't edit or remove them."
948
- }
949
- ),
1427
+ event === "lead.insert" && emailSteps.includes(step.type) && /* @__PURE__ */ jsx(LeadEmailNote, {}),
950
1428
  step.type === "step.sms.send" && /* @__PURE__ */ jsx(
951
1429
  Controller,
952
1430
  {
@@ -1033,9 +1511,7 @@ const FormOrganizationWorkflowEdit = ({
1033
1511
  // QA read "Issue a discount code", built the one-step workflow that
1034
1512
  // describes, and reported the codes as never arriving.
1035
1513
  message: /* @__PURE__ */ jsxs(Fragment, { children: [
1036
- "Creates the code and saves it to the lead. To send it, add an email step after this one with ",
1037
- "{{shopifyDiscountCode}}",
1038
- " in the message.",
1514
+ "Creates the code and saves it to the lead. To send it, keep this step before Auto response and add Shopify Discount Code to its message.",
1039
1515
  commerceDomain ? /* @__PURE__ */ jsxs(Fragment, { children: [
1040
1516
  " ",
1041
1517
  /* @__PURE__ */ jsx("a", { href: "https://" + commerceDomain + "/admin/discounts", rel: "noopener noreferrer", target: "_blank", children: "Manage discounts" })
@@ -1080,11 +1556,20 @@ const FormOrganizationWorkflowEdit = ({
1080
1556
  };
1081
1557
  export {
1082
1558
  FormOrganizationWorkflowEdit,
1559
+ RESPONSE,
1083
1560
  SAMPLE,
1084
1561
  TextPreview,
1085
1562
  firstEntryOnly,
1563
+ issueRow,
1564
+ memberOptions,
1565
+ partToStart,
1086
1566
  pickedCopy,
1567
+ plainHtml,
1087
1568
  prefillRow,
1569
+ responseParts,
1570
+ responseSave,
1571
+ responseStart,
1572
+ stepAfterPart,
1088
1573
  stepReason,
1089
1574
  stepRow,
1090
1575
  stepVariables,