@vicacha-devs/strapi-plugin-broadcasts 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -2
  3. package/dist/admin/BroadcastsHomePage-CM_N_spo.mjs +1210 -0
  4. package/dist/admin/BroadcastsHomePage-DQueM7nj.js +1228 -0
  5. package/dist/admin/SettingsPage-TawvAAiv.js +1187 -0
  6. package/dist/admin/SettingsPage-ikcb0cfh.mjs +1169 -0
  7. package/dist/admin/ar-vwekVqfp.js +232 -0
  8. package/dist/admin/ar-wZTCd3Za.mjs +232 -0
  9. package/dist/admin/de-IZicxuvv.mjs +232 -0
  10. package/dist/admin/de-Qa2SQQfY.js +232 -0
  11. package/dist/admin/en-B-HnZo8U.mjs +232 -0
  12. package/dist/admin/en-D1byrK0s.js +232 -0
  13. package/dist/admin/es-Co7PmAj8.mjs +232 -0
  14. package/dist/admin/es-Dgylqagv.js +232 -0
  15. package/dist/admin/fr-B9zZn3z1.mjs +232 -0
  16. package/dist/admin/fr-C7-MIWnw.js +232 -0
  17. package/dist/admin/index-CApLw9Uu.mjs +1036 -0
  18. package/dist/admin/index-Dw6J7U1v.js +1053 -0
  19. package/dist/admin/index.js +6 -0
  20. package/dist/admin/index.mjs +6 -0
  21. package/dist/admin/it-DJU9iBqT.mjs +232 -0
  22. package/dist/admin/it-D_mulZtx.js +232 -0
  23. package/dist/admin/pt-B9swwjPu.js +232 -0
  24. package/dist/admin/pt-Dr8u5we0.mjs +232 -0
  25. package/dist/admin/ru-B0NNiMNA.js +232 -0
  26. package/dist/admin/ru-Bd4cEKDV.mjs +232 -0
  27. package/dist/admin/selectOptions-BlvNrhvc.mjs +150 -0
  28. package/dist/admin/selectOptions-DuHQ0KK3.js +149 -0
  29. package/dist/admin/src/components/AutomationAndLogsTabs.d.ts +16 -0
  30. package/dist/admin/src/components/BridgesSandboxTab.d.ts +8 -0
  31. package/dist/admin/src/components/CampaignComposerForm.d.ts +26 -0
  32. package/dist/admin/src/components/CampaignsTableSection.d.ts +27 -0
  33. package/dist/admin/src/components/DeliveryLedgerSection.d.ts +17 -0
  34. package/dist/admin/src/components/Initializer.d.ts +6 -0
  35. package/dist/admin/src/components/OrganizationEmailTab.d.ts +10 -0
  36. package/dist/admin/src/components/PluginIcon.d.ts +3 -0
  37. package/dist/admin/src/components/WhatsAppTransportTab.d.ts +11 -0
  38. package/dist/admin/src/index.d.ts +24 -0
  39. package/dist/admin/src/pages/BroadcastsHomePage.d.ts +2 -0
  40. package/dist/admin/src/pages/SettingsPage.d.ts +3 -0
  41. package/dist/admin/src/permissions.d.ts +30 -0
  42. package/dist/admin/src/pluginId.d.ts +2 -0
  43. package/dist/admin/src/translations/cmLabels.d.ts +1 -0
  44. package/dist/admin/src/translations/cmLabelsExtra.d.ts +1 -0
  45. package/dist/admin/src/translations/cmPlaceholders.d.ts +1 -0
  46. package/dist/admin/src/translations/cmPlaceholdersExtra.d.ts +1 -0
  47. package/dist/admin/src/utils/extractApiError.d.ts +1 -0
  48. package/dist/admin/src/utils/selectOptions.d.ts +21 -0
  49. package/dist/admin/tr-Cc-ItZUP.js +232 -0
  50. package/dist/admin/tr-DlyHa2Vk.mjs +232 -0
  51. package/dist/admin/zh-CLzJ7x3y.mjs +232 -0
  52. package/dist/admin/zh-CaqwZk-p.js +232 -0
  53. package/dist/server/index.js +3399 -0
  54. package/dist/server/index.mjs +3399 -0
  55. package/dist/server/src/bootstrap.d.ts +5 -0
  56. package/dist/server/src/config/index.d.ts +5 -0
  57. package/dist/server/src/content-types/automation-rule/index.d.ts +90 -0
  58. package/dist/server/src/content-types/automation-rule/schema.json.d.ts +90 -0
  59. package/dist/server/src/content-types/campaign/index.d.ts +118 -0
  60. package/dist/server/src/content-types/campaign/schema.json.d.ts +118 -0
  61. package/dist/server/src/content-types/delivery-log/index.d.ts +71 -0
  62. package/dist/server/src/content-types/delivery-log/schema.json.d.ts +71 -0
  63. package/dist/server/src/content-types/index.d.ts +279 -0
  64. package/dist/server/src/controllers/broadcastController.d.ts +32 -0
  65. package/dist/server/src/controllers/index.d.ts +22 -0
  66. package/dist/server/src/destroy.d.ts +5 -0
  67. package/dist/server/src/eventHubRegistry.d.ts +5 -0
  68. package/dist/server/src/index.d.ts +463 -0
  69. package/dist/server/src/register.d.ts +5 -0
  70. package/dist/server/src/routes/admin.d.ts +17 -0
  71. package/dist/server/src/routes/content-api.d.ts +13 -0
  72. package/dist/server/src/routes/index.d.ts +31 -0
  73. package/dist/server/src/services/automationService.d.ts +33 -0
  74. package/dist/server/src/services/campaignChannelDispatch.d.ts +20 -0
  75. package/dist/server/src/services/campaignHelpers.d.ts +20 -0
  76. package/dist/server/src/services/campaignLifecycle.d.ts +23 -0
  77. package/dist/server/src/services/campaignService.d.ts +54 -0
  78. package/dist/server/src/services/deliveryLedger.d.ts +18 -0
  79. package/dist/server/src/services/emailDesignerBridge.d.ts +30 -0
  80. package/dist/server/src/services/idempotency.d.ts +5 -0
  81. package/dist/server/src/services/index.d.ts +119 -0
  82. package/dist/server/src/services/settings.d.ts +12 -0
  83. package/dist/server/src/services/transportService.d.ts +70 -0
  84. package/dist/shared/index.d.ts +1 -0
  85. package/dist/shared/index.js +825 -0
  86. package/dist/shared/index.mjs +823 -0
  87. package/package.json +109 -3
@@ -0,0 +1,1210 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { Table, Thead, Tr, Th, Typography, Tbody, Td, Flex, Badge, Button, Field, TextInput, SingleSelect, SingleSelectOption, Box, MultiSelect, MultiSelectOption, Main, Divider } from "@strapi/design-system";
3
+ import { useFetchClient } from "@strapi/strapi/admin";
4
+ import * as React from "react";
5
+ import { useIntl } from "react-intl";
6
+ import { useNavigate } from "react-router-dom";
7
+ import { C as CAMPAIGN_UID, A as AUTOMATION_RULE_UID, m as mergeSelectOptions, S as STARTER_AUTOMATION_RULES, P as PLUGIN_ID, a as STARTER_CAMPAIGN_TEMPLATES } from "./index-CApLw9Uu.mjs";
8
+ import { g as getBroadcastTopicOptions, a as getBroadcastChannelOptions, e as extractApiErrorMessage } from "./selectOptions-BlvNrhvc.mjs";
9
+ const RequiredTextField = ({ name, label, hint, placeholder, value, onChange }) => /* @__PURE__ */ jsxs(Field.Root, { name, required: true, hint, children: [
10
+ /* @__PURE__ */ jsx(Field.Label, { children: label }),
11
+ /* @__PURE__ */ jsx(
12
+ TextInput,
13
+ {
14
+ name,
15
+ placeholder,
16
+ value,
17
+ onChange: (e) => onChange(e.target.value)
18
+ }
19
+ ),
20
+ /* @__PURE__ */ jsx(Field.Hint, {})
21
+ ] });
22
+ const EmailTemplateSelectorField = ({
23
+ t,
24
+ emailTemplates,
25
+ value,
26
+ onChange
27
+ }) => /* @__PURE__ */ jsxs(
28
+ Field.Root,
29
+ {
30
+ name: "emailTemplateRef",
31
+ hint: t(
32
+ "page.ui.email_designer_template_hint",
33
+ "Wraps email broadcasts in an Email Designer 5 layout (injects '{{subject}}', '{{bodyHtml}}', '{{recipientName}}', '{{secondaryDate}}')."
34
+ ),
35
+ children: [
36
+ /* @__PURE__ */ jsx(Field.Label, { children: t(
37
+ "field.email_designer_html_template_optional",
38
+ "Email Designer HTML Template (optional)"
39
+ ) }),
40
+ emailTemplates.length > 0 ? /* @__PURE__ */ jsxs(
41
+ SingleSelect,
42
+ {
43
+ placeholder: t(
44
+ "placeholder.none_use_inline_campaign_body",
45
+ "None (use inline campaign body)"
46
+ ),
47
+ value: value || "__none__",
48
+ onChange: (val) => onChange(val === "__none__" ? "" : String(val)),
49
+ children: [
50
+ /* @__PURE__ */ jsx(SingleSelectOption, { value: "__none__", children: t(
51
+ "page.ui.none_use_inline_campaign_html",
52
+ "None (use inline campaign HTML / text)"
53
+ ) }),
54
+ /* @__PURE__ */ jsx(SingleSelectOption, { value: "broadcastNewsletter", children: t(
55
+ "page.ui.broadcast_newsletter_auto_localized",
56
+ "broadcastNewsletter (Auto-localized es / en / ar / tr)"
57
+ ) }),
58
+ emailTemplates.map((tpl) => /* @__PURE__ */ jsxs(
59
+ SingleSelectOption,
60
+ {
61
+ value: String(tpl.templateReferenceId),
62
+ children: [
63
+ "#",
64
+ tpl.templateReferenceId,
65
+ " — ",
66
+ tpl.name
67
+ ]
68
+ },
69
+ `${tpl.templateReferenceId}-${tpl.name}`
70
+ ))
71
+ ]
72
+ }
73
+ ) : /* @__PURE__ */ jsx(
74
+ TextInput,
75
+ {
76
+ name: "emailTemplateRef",
77
+ placeholder: t(
78
+ "placeholder.optional_e_g_401_or_broadcastnewsletter",
79
+ "Optional: e.g. 401 or broadcastNewsletter"
80
+ ),
81
+ value,
82
+ onChange: (e) => onChange(e.target.value)
83
+ }
84
+ ),
85
+ /* @__PURE__ */ jsx(Field.Hint, {})
86
+ ]
87
+ }
88
+ );
89
+ const CampaignsTableSection = ({
90
+ t,
91
+ campaigns,
92
+ onEdit,
93
+ onDispatch,
94
+ navigate,
95
+ isBusy = false
96
+ }) => /* @__PURE__ */ jsxs(Table, { colCount: 7, rowCount: campaigns.length + 1, children: [
97
+ /* @__PURE__ */ jsx(Thead, { children: /* @__PURE__ */ jsxs(Tr, { children: [
98
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.slug_title", "Slug / Title") }) }),
99
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.topic", "Topic") }) }),
100
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.channels", "Channels") }) }),
101
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.status", "Status") }) }),
102
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.hijri_secondary_date", "Hijri / Secondary Date") }) }),
103
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.delivery_stats", "Delivery Stats") }) }),
104
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.actions", "Actions") }) })
105
+ ] }) }),
106
+ /* @__PURE__ */ jsx(Tbody, { children: campaigns.map((camp) => {
107
+ const sentTotal = (camp.stats?.sentCount || 0) + (camp.stats?.simulatedCount || 0);
108
+ const failedTotal = camp.stats?.failedCount || 0;
109
+ return /* @__PURE__ */ jsxs(Tr, { children: [
110
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsxs(Flex, { direction: "column", alignItems: "flex-start", gap: 1, children: [
111
+ /* @__PURE__ */ jsx(Typography, { fontWeight: "bold", children: camp.title }),
112
+ /* @__PURE__ */ jsxs(Flex, { gap: 2, alignItems: "center", wrap: "wrap", children: [
113
+ /* @__PURE__ */ jsx(Typography, { variant: "pi", textColor: "neutral600", children: camp.slug }),
114
+ camp.emailTemplateRef && /* @__PURE__ */ jsxs(Badge, { children: [
115
+ t("page.ui.email_template_badge", "Email Template:"),
116
+ " ",
117
+ camp.emailTemplateRef
118
+ ] })
119
+ ] })
120
+ ] }) }),
121
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Badge, { children: camp.topic }) }),
122
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { children: (camp.channels || []).join(", ") }) }),
123
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Badge, { children: camp.status }) }),
124
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { children: camp.secondaryDateLabel || "-" }) }),
125
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsxs(Typography, { children: [
126
+ t("page.ui.sent_label", "Sent:"),
127
+ " ",
128
+ sentTotal,
129
+ " /",
130
+ " ",
131
+ t("page.ui.failed_label", "Failed:"),
132
+ " ",
133
+ failedTotal
134
+ ] }) }),
135
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsxs(Flex, { gap: 2, wrap: "wrap", children: [
136
+ /* @__PURE__ */ jsx(
137
+ Button,
138
+ {
139
+ variant: "tertiary",
140
+ size: "S",
141
+ disabled: isBusy,
142
+ onClick: () => onEdit(camp),
143
+ children: t("common.edit", "Edit")
144
+ }
145
+ ),
146
+ camp.documentId && /* @__PURE__ */ jsx(
147
+ Button,
148
+ {
149
+ variant: "tertiary",
150
+ size: "S",
151
+ disabled: isBusy,
152
+ onClick: () => navigate(
153
+ `/content-manager/collection-types/${CAMPAIGN_UID}/${camp.documentId}`
154
+ ),
155
+ children: t("common.content_manager", "Content Manager")
156
+ }
157
+ ),
158
+ /* @__PURE__ */ jsx(
159
+ Button,
160
+ {
161
+ variant: "secondary",
162
+ size: "S",
163
+ disabled: isBusy,
164
+ onClick: () => onDispatch(camp.documentId || camp.slug),
165
+ children: t("page.ui.dispatch_now", "Dispatch Now")
166
+ }
167
+ )
168
+ ] }) })
169
+ ] }, camp.slug);
170
+ }) })
171
+ ] });
172
+ const DeliveryLedgerSection = ({
173
+ t,
174
+ logs
175
+ }) => /* @__PURE__ */ jsxs(Box, { paddingTop: 6, children: [
176
+ /* @__PURE__ */ jsxs(
177
+ Box,
178
+ {
179
+ padding: 4,
180
+ marginBottom: 4,
181
+ background: "neutral100",
182
+ hasRadius: true,
183
+ style: { border: "1px solid rgba(128, 128, 128, 0.18)" },
184
+ children: [
185
+ /* @__PURE__ */ jsx(Typography, { variant: "delta", tag: "h2", fontWeight: "bold", children: t(
186
+ "page.ui.what_is_the_delivery_ledger",
187
+ "What is the Delivery Ledger?"
188
+ ) }),
189
+ /* @__PURE__ */ jsx(Box, { paddingTop: 1, children: /* @__PURE__ */ jsx(Typography, { variant: "omega", textColor: "neutral600", children: t(
190
+ "page.ui.delivery_ledger_description",
191
+ "The Delivery Ledger is an immutable audit and idempotency log of every message dispatched across Email, Telegram, and WhatsApp. Each delivery records a deterministic idempotency key (campaignSlug + channel + recipient) so retrying a campaign never sends duplicate messages, masks recipient PII (e.g. a***@domain.org, +57***4567), and tracks whether the message was sent live or simulated in Dry-Run mode."
192
+ ) }) })
193
+ ]
194
+ }
195
+ ),
196
+ /* @__PURE__ */ jsxs(Table, { colCount: 6, rowCount: Math.max(logs.length, 1) + 1, children: [
197
+ /* @__PURE__ */ jsx(Thead, { children: /* @__PURE__ */ jsxs(Tr, { children: [
198
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.campaign", "Campaign") }) }),
199
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.channel_driver", "Channel / Driver") }) }),
200
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.masked_recipient", "Masked Recipient") }) }),
201
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.locale", "Locale") }) }),
202
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.status", "Status") }) }),
203
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.sent_at", "Sent At") }) })
204
+ ] }) }),
205
+ /* @__PURE__ */ jsx(Tbody, { children: logs.length === 0 ? /* @__PURE__ */ jsx(Tr, { children: /* @__PURE__ */ jsx(Td, { colSpan: 6, children: /* @__PURE__ */ jsx(Box, { padding: 4, children: /* @__PURE__ */ jsx(Typography, { textColor: "neutral600", children: t(
206
+ "page.ui.no_deliveries_recorded_yet",
207
+ 'No deliveries recorded yet. Click "Dispatch Now" on any campaign in the Campaigns tab to dispatch (or simulate in Dry-Run mode) and populate the ledger.'
208
+ ) }) }) }) }) : logs.map((log) => /* @__PURE__ */ jsxs(Tr, { children: [
209
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { fontWeight: "bold", children: log.campaignSlug }) }),
210
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsxs(Typography, { children: [
211
+ log.channel,
212
+ " (",
213
+ log.driver,
214
+ ")"
215
+ ] }) }),
216
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { children: log.recipientMasked }) }),
217
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Badge, { children: log.locale }) }),
218
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Badge, { children: log.status }) }),
219
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { children: log.sentAt?.slice(0, 19) }) })
220
+ ] }, log.idempotencyKey)) })
221
+ ] })
222
+ ] });
223
+ const AutomationRulesTable = ({
224
+ t,
225
+ rules,
226
+ onEdit,
227
+ onToggleActive,
228
+ navigate,
229
+ isBusy = false
230
+ }) => /* @__PURE__ */ jsxs(Table, { colCount: 7, rowCount: rules.length + 1, children: [
231
+ /* @__PURE__ */ jsx(Thead, { children: /* @__PURE__ */ jsxs(Tr, { children: [
232
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.rule_slug", "Rule Slug") }) }),
233
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.eventhub_trigger", "EventHub Trigger") }) }),
234
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.topic", "Topic") }) }),
235
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.channels", "Channels") }) }),
236
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.trigger_count", "Trigger Count") }) }),
237
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.status", "Status") }) }),
238
+ /* @__PURE__ */ jsx(Th, { children: /* @__PURE__ */ jsx(Typography, { variant: "sigma", children: t("page.ui.actions", "Actions") }) })
239
+ ] }) }),
240
+ /* @__PURE__ */ jsx(Tbody, { children: rules.map((rule) => /* @__PURE__ */ jsxs(Tr, { children: [
241
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsxs(Flex, { direction: "column", alignItems: "flex-start", gap: 1, children: [
242
+ /* @__PURE__ */ jsx(Typography, { fontWeight: "bold", children: rule.name }),
243
+ /* @__PURE__ */ jsx(Typography, { variant: "pi", textColor: "neutral600", children: rule.slug })
244
+ ] }) }),
245
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Badge, { children: rule.eventName }) }),
246
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { children: rule.topic }) }),
247
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { children: (rule.channels || []).join(", ") }) }),
248
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Typography, { children: rule.triggerCount || 0 }) }),
249
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsx(Badge, { active: rule.active, children: rule.active ? t("common.active", "active") : t("common.paused", "paused") }) }),
250
+ /* @__PURE__ */ jsx(Td, { children: /* @__PURE__ */ jsxs(Flex, { gap: 2, wrap: "wrap", children: [
251
+ /* @__PURE__ */ jsx(
252
+ Button,
253
+ {
254
+ variant: "tertiary",
255
+ size: "S",
256
+ disabled: isBusy,
257
+ onClick: () => onEdit(rule),
258
+ children: t("common.edit", "Edit")
259
+ }
260
+ ),
261
+ /* @__PURE__ */ jsx(
262
+ Button,
263
+ {
264
+ variant: "secondary",
265
+ size: "S",
266
+ disabled: isBusy,
267
+ onClick: () => onToggleActive(rule),
268
+ children: rule.active ? t("common.pause", "Pause") : t("common.activate", "Activate")
269
+ }
270
+ ),
271
+ rule.documentId && /* @__PURE__ */ jsx(
272
+ Button,
273
+ {
274
+ variant: "tertiary",
275
+ size: "S",
276
+ disabled: isBusy,
277
+ onClick: () => navigate(
278
+ `/content-manager/collection-types/${AUTOMATION_RULE_UID}/${rule.documentId}`
279
+ ),
280
+ children: t("common.content_manager", "Content Manager")
281
+ }
282
+ )
283
+ ] }) })
284
+ ] }, rule.slug)) })
285
+ ] });
286
+ function parseChannelsInput(raw, fallback = ["email", "telegram"]) {
287
+ const allowed = ["email", "telegram", "whatsapp"];
288
+ const items = Array.isArray(raw) ? raw : raw.split(",");
289
+ const parsed = items.map((item) => item.trim().toLowerCase()).filter((item) => allowed.includes(item));
290
+ return parsed.length > 0 ? Array.from(new Set(parsed)) : fallback;
291
+ }
292
+ const AutomationAndLogsTabs = ({
293
+ activeTab,
294
+ rules,
295
+ logs,
296
+ t,
297
+ navigate,
298
+ post,
299
+ put,
300
+ notifyStatus,
301
+ loadAll
302
+ }) => {
303
+ const [editingRuleId, setEditingRuleId] = React.useState(null);
304
+ const [ruleActive, setRuleActive] = React.useState(true);
305
+ const [rulePriority, setRulePriority] = React.useState("normal");
306
+ const [isBusy, setIsBusy] = React.useState(false);
307
+ const [ruleSlug, setRuleSlug] = React.useState("");
308
+ const [ruleName, setRuleName] = React.useState("");
309
+ const [ruleEventName, setRuleEventName] = React.useState("event.published");
310
+ const [ruleTopic, setRuleTopic] = React.useState("events");
311
+ const [ruleChannels, setRuleChannels] = React.useState([
312
+ "telegram",
313
+ "email"
314
+ ]);
315
+ const [ruleSubjectTemplate, setRuleSubjectTemplate] = React.useState(
316
+ "📅 {{title}} ({{secondaryDate}})"
317
+ );
318
+ const [ruleBodyTextTemplate, setRuleBodyTextTemplate] = React.useState("{{title}}: {{summary}} ({{secondaryDate}})");
319
+ const resetRuleForm = () => {
320
+ setEditingRuleId(null);
321
+ setRuleActive(true);
322
+ setRulePriority("normal");
323
+ setRuleSlug("");
324
+ setRuleName("");
325
+ setRuleEventName("event.published");
326
+ setRuleTopic("events");
327
+ setRuleChannels(["telegram", "email"]);
328
+ setRuleSubjectTemplate("📅 {{title}} ({{secondaryDate}})");
329
+ setRuleBodyTextTemplate("{{title}}: {{summary}} ({{secondaryDate}})");
330
+ };
331
+ const handleStartEditRule = (rule) => {
332
+ setEditingRuleId(rule.documentId || rule.slug);
333
+ setRuleActive(rule.active ?? true);
334
+ setRulePriority(rule.priority || "normal");
335
+ setRuleSlug(rule.slug);
336
+ setRuleName(rule.name);
337
+ setRuleEventName(rule.eventName);
338
+ setRuleTopic(rule.topic || "announcements");
339
+ setRuleChannels(
340
+ Array.isArray(rule.channels) && rule.channels.length > 0 ? rule.channels : ["telegram"]
341
+ );
342
+ setRuleSubjectTemplate(rule.subjectTemplate || "");
343
+ setRuleBodyTextTemplate(rule.bodyTextTemplate || "");
344
+ notifyStatus(`${t("page.ui.editing_rule", "Editing rule")}: ${rule.name}`);
345
+ };
346
+ const handleSaveRule = async () => {
347
+ const isComplete = Boolean(
348
+ ruleSlug && ruleName && ruleEventName && ruleSubjectTemplate && ruleBodyTextTemplate
349
+ );
350
+ if (!isComplete || isBusy) return;
351
+ const payload = {
352
+ slug: ruleSlug.trim(),
353
+ name: ruleName.trim(),
354
+ eventName: ruleEventName.trim(),
355
+ topic: ruleTopic.trim() || "announcements",
356
+ channels: parseChannelsInput(ruleChannels, ["telegram", "email"]),
357
+ active: ruleActive,
358
+ priority: rulePriority,
359
+ subjectTemplate: ruleSubjectTemplate.trim(),
360
+ bodyTextTemplate: ruleBodyTextTemplate.trim()
361
+ };
362
+ setIsBusy(true);
363
+ try {
364
+ if (editingRuleId) {
365
+ await put(`/${PLUGIN_ID}/automation-rules/${editingRuleId}`, payload);
366
+ notifyStatus(
367
+ `${t("page.ui.updated_rule", "Updated automation rule")}: ${payload.name}`
368
+ );
369
+ } else {
370
+ await post(`/${PLUGIN_ID}/automation-rules`, payload);
371
+ notifyStatus(
372
+ `${t("page.ui.created_rule", "Created automation rule")}: ${payload.name}`
373
+ );
374
+ }
375
+ resetRuleForm();
376
+ await loadAll();
377
+ } catch (err) {
378
+ notifyStatus(
379
+ extractApiErrorMessage(
380
+ err,
381
+ t("page.ui.operation_failed", "Broadcast operation failed.")
382
+ ),
383
+ true
384
+ );
385
+ } finally {
386
+ setIsBusy(false);
387
+ }
388
+ };
389
+ const handleToggleRuleActive = async (rule) => {
390
+ if (isBusy) return;
391
+ const targetId = rule.documentId || rule.slug;
392
+ const nextActive = !rule.active;
393
+ setIsBusy(true);
394
+ try {
395
+ await put(`/${PLUGIN_ID}/automation-rules/${targetId}`, {
396
+ ...rule,
397
+ active: nextActive
398
+ });
399
+ const stateLabel = nextActive ? t("page.ui.rule_activated", "activated") : t("page.ui.rule_paused", "paused");
400
+ notifyStatus(`${rule.name}: ${stateLabel}`);
401
+ await loadAll();
402
+ } catch (err) {
403
+ notifyStatus(
404
+ extractApiErrorMessage(
405
+ err,
406
+ t("page.ui.operation_failed", "Broadcast operation failed.")
407
+ ),
408
+ true
409
+ );
410
+ } finally {
411
+ setIsBusy(false);
412
+ }
413
+ };
414
+ const handleSeedStarterRule = async (preset) => {
415
+ if (isBusy) return;
416
+ const existing = rules.find((r) => r.slug === preset.slug);
417
+ if (existing) {
418
+ handleStartEditRule(existing);
419
+ return;
420
+ }
421
+ setIsBusy(true);
422
+ try {
423
+ await post(`/${PLUGIN_ID}/automation-rules`, preset);
424
+ notifyStatus(
425
+ `${t("page.ui.added_starter_rule", "Added starter automation rule")}: ${preset.name}`
426
+ );
427
+ await loadAll();
428
+ } catch (err) {
429
+ notifyStatus(
430
+ extractApiErrorMessage(
431
+ err,
432
+ t("page.ui.operation_failed", "Broadcast operation failed.")
433
+ ),
434
+ true
435
+ );
436
+ } finally {
437
+ setIsBusy(false);
438
+ }
439
+ };
440
+ if (activeTab === "logs") {
441
+ return /* @__PURE__ */ jsx(DeliveryLedgerSection, { t, logs });
442
+ }
443
+ if (activeTab !== "automations") {
444
+ return null;
445
+ }
446
+ const formHeading = editingRuleId ? `${t("page.ui.edit_automation_rule", "Edit Automation Rule")} (${ruleSlug})` : t(
447
+ "page.ui.create_new_eventhub_automation_rule",
448
+ "Create New EventHub Automation Rule"
449
+ );
450
+ const topicOpts = mergeSelectOptions(getBroadcastTopicOptions(t), [
451
+ ruleTopic
452
+ ]);
453
+ const channelOpts = getBroadcastChannelOptions(t);
454
+ return /* @__PURE__ */ jsxs(Box, { paddingTop: 6, children: [
455
+ /* @__PURE__ */ jsxs(
456
+ Flex,
457
+ {
458
+ justifyContent: "space-between",
459
+ alignItems: "center",
460
+ paddingBottom: 4,
461
+ wrap: "wrap",
462
+ gap: 2,
463
+ children: [
464
+ /* @__PURE__ */ jsx(Typography, { variant: "beta", tag: "h2", children: t("page.ui.eventhub_automation_rules", "EventHub Automation Rules") }),
465
+ /* @__PURE__ */ jsx(Flex, { gap: 2, wrap: "wrap", children: STARTER_AUTOMATION_RULES.map((preset) => /* @__PURE__ */ jsxs(
466
+ Button,
467
+ {
468
+ variant: "secondary",
469
+ disabled: isBusy,
470
+ onClick: () => handleSeedStarterRule(preset),
471
+ children: [
472
+ "+ ",
473
+ preset.slug
474
+ ]
475
+ },
476
+ preset.slug
477
+ )) })
478
+ ]
479
+ }
480
+ ),
481
+ /* @__PURE__ */ jsxs(
482
+ Box,
483
+ {
484
+ padding: 5,
485
+ marginBottom: 5,
486
+ background: "neutral0",
487
+ hasRadius: true,
488
+ shadow: "filterShadow",
489
+ style: { border: "1px solid rgba(128, 128, 128, 0.18)" },
490
+ children: [
491
+ /* @__PURE__ */ jsxs(Flex, { justifyContent: "space-between", alignItems: "center", children: [
492
+ /* @__PURE__ */ jsx(Typography, { variant: "delta", tag: "h3", fontWeight: "bold", children: formHeading }),
493
+ editingRuleId && /* @__PURE__ */ jsx(Badge, { active: true, children: t("common.editing_mode", "Editing Mode") })
494
+ ] }),
495
+ /* @__PURE__ */ jsxs(
496
+ Box,
497
+ {
498
+ paddingTop: 3,
499
+ style: {
500
+ display: "grid",
501
+ gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))",
502
+ gap: 14,
503
+ alignItems: "start"
504
+ },
505
+ children: [
506
+ /* @__PURE__ */ jsx(
507
+ RequiredTextField,
508
+ {
509
+ name: "ruleSlug",
510
+ label: t("field.rule_slug", "Rule Slug"),
511
+ hint: t(
512
+ "page.ui.rule_slug_hint",
513
+ "Unique identifier (lowercase & hyphens)."
514
+ ),
515
+ placeholder: t(
516
+ "placeholder.e_g_auto_course_published",
517
+ "e.g., auto-course-published"
518
+ ),
519
+ value: ruleSlug,
520
+ onChange: setRuleSlug
521
+ }
522
+ ),
523
+ /* @__PURE__ */ jsx(
524
+ RequiredTextField,
525
+ {
526
+ name: "ruleName",
527
+ label: t("field.rule_name", "Rule Name"),
528
+ hint: t(
529
+ "page.ui.rule_name_hint",
530
+ "Human-readable rule description."
531
+ ),
532
+ placeholder: t(
533
+ "placeholder.e_g_auto_broadcast_new_courses",
534
+ "e.g., Auto-Broadcast New Courses"
535
+ ),
536
+ value: ruleName,
537
+ onChange: setRuleName
538
+ }
539
+ ),
540
+ /* @__PURE__ */ jsx(
541
+ RequiredTextField,
542
+ {
543
+ name: "ruleEventName",
544
+ label: t("field.eventhub_trigger_name", "EventHub Trigger Name"),
545
+ hint: t(
546
+ "page.ui.rule_event_name_hint",
547
+ "Strapi EventHub event that triggers this rule."
548
+ ),
549
+ placeholder: t(
550
+ "placeholder.e_g_event_published",
551
+ "e.g., event.published or inbox.alert.published"
552
+ ),
553
+ value: ruleEventName,
554
+ onChange: setRuleEventName
555
+ }
556
+ ),
557
+ /* @__PURE__ */ jsxs(
558
+ Field.Root,
559
+ {
560
+ name: "ruleTopic",
561
+ hint: t(
562
+ "page.ui.rule_topic_hint",
563
+ "Subscriber topic segment to notify."
564
+ ),
565
+ children: [
566
+ /* @__PURE__ */ jsx(Field.Label, { children: t("field.topic_segment", "Topic Segment") }),
567
+ /* @__PURE__ */ jsx(
568
+ SingleSelect,
569
+ {
570
+ name: "ruleTopic",
571
+ value: ruleTopic || "events",
572
+ onChange: (val) => setRuleTopic(String(val)),
573
+ children: topicOpts.map((opt) => /* @__PURE__ */ jsx(SingleSelectOption, { value: opt.value, children: opt.label }, opt.value))
574
+ }
575
+ ),
576
+ /* @__PURE__ */ jsx(Field.Hint, {})
577
+ ]
578
+ }
579
+ ),
580
+ /* @__PURE__ */ jsxs(
581
+ Field.Root,
582
+ {
583
+ name: "ruleChannels",
584
+ hint: t(
585
+ "page.ui.rule_channels_hint",
586
+ "Channels dispatched when triggered."
587
+ ),
588
+ children: [
589
+ /* @__PURE__ */ jsx(Field.Label, { children: t("field.channels", "Channels") }),
590
+ /* @__PURE__ */ jsx(
591
+ MultiSelect,
592
+ {
593
+ name: "ruleChannels",
594
+ withTags: true,
595
+ placeholder: t(
596
+ "placeholder.select_channels",
597
+ "Select channels..."
598
+ ),
599
+ value: ruleChannels,
600
+ onChange: (nextValues) => setRuleChannels(
601
+ nextValues.length > 0 ? nextValues : ["telegram"]
602
+ ),
603
+ onClear: () => setRuleChannels(["telegram"]),
604
+ children: channelOpts.map((opt) => /* @__PURE__ */ jsx(MultiSelectOption, { value: opt.value, children: opt.label }, opt.value))
605
+ }
606
+ ),
607
+ /* @__PURE__ */ jsx(Field.Hint, {})
608
+ ]
609
+ }
610
+ ),
611
+ /* @__PURE__ */ jsx(
612
+ RequiredTextField,
613
+ {
614
+ name: "ruleSubjectTemplate",
615
+ label: t("field.subject_template", "Subject Template"),
616
+ hint: t(
617
+ "page.ui.rule_subject_template_hint",
618
+ "Supports '{{title}}', '{{secondaryDate}}', '{{organizationName}}'."
619
+ ),
620
+ placeholder: t(
621
+ "placeholder.rule_subject_template",
622
+ "🚨 '{{title}}' — '{{organizationName}}'"
623
+ ),
624
+ value: ruleSubjectTemplate,
625
+ onChange: setRuleSubjectTemplate
626
+ }
627
+ ),
628
+ /* @__PURE__ */ jsx(Box, { style: { gridColumn: "1 / -1" }, children: /* @__PURE__ */ jsx(
629
+ RequiredTextField,
630
+ {
631
+ name: "ruleBodyTextTemplate",
632
+ label: t("field.body_text_template", "Body Text Template"),
633
+ hint: t(
634
+ "page.ui.rule_body_text_template_hint",
635
+ "Interpolated with event payload fields ('{{title}}', '{{message}}', '{{summary}}', '{{secondaryDate}}')."
636
+ ),
637
+ placeholder: t(
638
+ "placeholder.rule_body_text_template",
639
+ "'{{title}}': '{{message}}' ('{{secondaryDate}}')"
640
+ ),
641
+ value: ruleBodyTextTemplate,
642
+ onChange: setRuleBodyTextTemplate
643
+ }
644
+ ) })
645
+ ]
646
+ }
647
+ ),
648
+ /* @__PURE__ */ jsxs(Flex, { justifyContent: "flex-end", paddingTop: 3, gap: 2, children: [
649
+ editingRuleId && /* @__PURE__ */ jsx(
650
+ Button,
651
+ {
652
+ variant: "tertiary",
653
+ disabled: isBusy,
654
+ onClick: () => resetRuleForm(),
655
+ children: t("common.cancel", "Cancel")
656
+ }
657
+ ),
658
+ /* @__PURE__ */ jsx(Button, { loading: isBusy, disabled: isBusy, onClick: handleSaveRule, children: editingRuleId ? t("page.ui.save_rule_changes", "Save Rule Changes") : t("page.ui.create_automation_rule", "Create Automation Rule") })
659
+ ] })
660
+ ]
661
+ }
662
+ ),
663
+ /* @__PURE__ */ jsx(
664
+ AutomationRulesTable,
665
+ {
666
+ t,
667
+ rules,
668
+ onEdit: handleStartEditRule,
669
+ onToggleActive: handleToggleRuleActive,
670
+ navigate,
671
+ isBusy
672
+ }
673
+ )
674
+ ] });
675
+ };
676
+ const CampaignComposerForm = ({
677
+ t,
678
+ editingCampaignId,
679
+ isBusy,
680
+ newSlug,
681
+ setNewSlug,
682
+ newTitle,
683
+ setNewTitle,
684
+ newTopic,
685
+ setNewTopic,
686
+ newChannels,
687
+ setNewChannels,
688
+ newEmailTemplateRef,
689
+ setNewEmailTemplateRef,
690
+ newSubject,
691
+ setNewSubject,
692
+ newBodyText,
693
+ setNewBodyText,
694
+ emailTemplates,
695
+ onCancelEdit,
696
+ onSave
697
+ }) => {
698
+ const campaignFormTitle = editingCampaignId ? `${t("page.ui.edit_broadcast_campaign", "Edit Broadcast Campaign")} (${newSlug})` : t(
699
+ "page.ui.compose_new_omnichannel_broadcast",
700
+ "Compose New Omnichannel Broadcast"
701
+ );
702
+ const topicOpts = mergeSelectOptions(getBroadcastTopicOptions(t), [newTopic]);
703
+ const channelOpts = getBroadcastChannelOptions(t);
704
+ return /* @__PURE__ */ jsxs(
705
+ Box,
706
+ {
707
+ padding: 5,
708
+ marginBottom: 5,
709
+ background: "neutral0",
710
+ hasRadius: true,
711
+ shadow: "filterShadow",
712
+ style: { border: "1px solid rgba(128, 128, 128, 0.18)" },
713
+ children: [
714
+ /* @__PURE__ */ jsxs(Flex, { justifyContent: "space-between", alignItems: "center", children: [
715
+ /* @__PURE__ */ jsx(Typography, { variant: "delta", tag: "h3", fontWeight: "bold", children: campaignFormTitle }),
716
+ editingCampaignId && /* @__PURE__ */ jsx(Badge, { active: true, children: t("common.editing_mode", "Editing Mode") })
717
+ ] }),
718
+ /* @__PURE__ */ jsxs(
719
+ Box,
720
+ {
721
+ paddingTop: 3,
722
+ style: {
723
+ display: "grid",
724
+ gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))",
725
+ gap: 14,
726
+ alignItems: "start"
727
+ },
728
+ children: [
729
+ /* @__PURE__ */ jsx(
730
+ RequiredTextField,
731
+ {
732
+ name: "slug",
733
+ label: t("field.campaign_slug", "Campaign Slug"),
734
+ hint: t(
735
+ "page.ui.unique_url_safe_identifier_for_idempotency_track",
736
+ "Unique URL-safe identifier for idempotency tracking."
737
+ ),
738
+ placeholder: t(
739
+ "placeholder.e_g_friday_khutbah_bulletin",
740
+ "e.g., friday-khutbah-bulletin"
741
+ ),
742
+ value: newSlug,
743
+ onChange: setNewSlug
744
+ }
745
+ ),
746
+ /* @__PURE__ */ jsx(
747
+ RequiredTextField,
748
+ {
749
+ name: "title",
750
+ label: t("field.campaign_title", "Campaign Title"),
751
+ hint: t(
752
+ "page.ui.internal_display_name_in_the_campaign_table",
753
+ "Internal display name in the campaign table."
754
+ ),
755
+ placeholder: t(
756
+ "placeholder.e_g_bolet_n_semanal_de_jumu_ah",
757
+ "e.g., Boletín Semanal de Jumu'ah"
758
+ ),
759
+ value: newTitle,
760
+ onChange: setNewTitle
761
+ }
762
+ ),
763
+ /* @__PURE__ */ jsxs(
764
+ Field.Root,
765
+ {
766
+ name: "topic",
767
+ hint: t(
768
+ "page.ui.target_subscriber_segment_and_telegram_forum_thr",
769
+ "Target subscriber segment and Telegram forum thread."
770
+ ),
771
+ children: [
772
+ /* @__PURE__ */ jsx(Field.Label, { children: t("field.topic_segment", "Topic Segment") }),
773
+ /* @__PURE__ */ jsx(
774
+ SingleSelect,
775
+ {
776
+ name: "topic",
777
+ value: newTopic || "announcements",
778
+ onChange: (val) => setNewTopic(String(val)),
779
+ children: topicOpts.map((opt) => /* @__PURE__ */ jsx(SingleSelectOption, { value: opt.value, children: opt.label }, opt.value))
780
+ }
781
+ ),
782
+ /* @__PURE__ */ jsx(Field.Hint, {})
783
+ ]
784
+ }
785
+ ),
786
+ /* @__PURE__ */ jsxs(
787
+ Field.Root,
788
+ {
789
+ name: "channels",
790
+ hint: t(
791
+ "page.ui.channels_hint_combination",
792
+ "Any combination of: email, telegram, whatsapp"
793
+ ),
794
+ children: [
795
+ /* @__PURE__ */ jsx(Field.Label, { children: t("field.channels", "Channels") }),
796
+ /* @__PURE__ */ jsx(
797
+ MultiSelect,
798
+ {
799
+ name: "channels",
800
+ withTags: true,
801
+ placeholder: t("placeholder.select_channels", "Select channels..."),
802
+ value: newChannels,
803
+ onChange: (nextValues) => setNewChannels(
804
+ nextValues.length > 0 ? nextValues : ["email"]
805
+ ),
806
+ onClear: () => setNewChannels(["email"]),
807
+ children: channelOpts.map((opt) => /* @__PURE__ */ jsx(MultiSelectOption, { value: opt.value, children: opt.label }, opt.value))
808
+ }
809
+ ),
810
+ /* @__PURE__ */ jsx(Field.Hint, {})
811
+ ]
812
+ }
813
+ ),
814
+ /* @__PURE__ */ jsx(
815
+ EmailTemplateSelectorField,
816
+ {
817
+ t,
818
+ emailTemplates,
819
+ value: newEmailTemplateRef,
820
+ onChange: setNewEmailTemplateRef
821
+ }
822
+ ),
823
+ /* @__PURE__ */ jsx(
824
+ RequiredTextField,
825
+ {
826
+ name: "subject",
827
+ label: t("field.subject_headline", "Subject / Headline"),
828
+ hint: t(
829
+ "page.ui.email_subject_line_and_bold_header_on_telegram_w",
830
+ "Email subject line and bold header on Telegram/WhatsApp."
831
+ ),
832
+ placeholder: t(
833
+ "placeholder.e_g_as_salamu_alaykum_horarios_y_anuncios",
834
+ "e.g., As-salamu alaykum — Prayer Times & Announcements"
835
+ ),
836
+ value: newSubject,
837
+ onChange: setNewSubject
838
+ }
839
+ ),
840
+ /* @__PURE__ */ jsx(Box, { style: { gridColumn: "1 / -1" }, children: /* @__PURE__ */ jsx(
841
+ RequiredTextField,
842
+ {
843
+ name: "bodyText",
844
+ label: t(
845
+ "field.message_body_with_placeholders",
846
+ "Message Body (supports '{{recipientName}}', '{{secondaryDate}}', '{{organizationName}}')"
847
+ ),
848
+ hint: t(
849
+ "page.ui.main_text_dispatched_across_email_telegram_and_w",
850
+ "Main text dispatched across Email, Telegram, and WhatsApp channels."
851
+ ),
852
+ placeholder: t(
853
+ "placeholder.e_g_hola_recipientname_hoy_secondarydate_te_invi",
854
+ "e.g., Hello '{{recipientName}}', today ('{{secondaryDate}}') we invite you to..."
855
+ ),
856
+ value: newBodyText,
857
+ onChange: setNewBodyText
858
+ }
859
+ ) })
860
+ ]
861
+ }
862
+ ),
863
+ /* @__PURE__ */ jsxs(Flex, { justifyContent: "flex-end", paddingTop: 3, gap: 2, children: [
864
+ editingCampaignId && /* @__PURE__ */ jsx(
865
+ Button,
866
+ {
867
+ variant: "tertiary",
868
+ disabled: isBusy,
869
+ onClick: () => onCancelEdit(),
870
+ children: t("common.cancel", "Cancel")
871
+ }
872
+ ),
873
+ /* @__PURE__ */ jsx(Button, { loading: isBusy, disabled: isBusy, onClick: onSave, children: editingCampaignId ? t("page.ui.save_campaign_changes", "Save Campaign Changes") : t("page.ui.create_campaign", "Create Campaign") })
874
+ ] })
875
+ ]
876
+ }
877
+ );
878
+ };
879
+ const DEFAULT_CHANNELS = ["email", "telegram", "whatsapp"];
880
+ const BroadcastsHomePage = () => {
881
+ const { formatMessage } = useIntl();
882
+ const navigate = useNavigate();
883
+ const t = (id, defaultMessage) => formatMessage({ id: `${PLUGIN_ID}.${id}`, defaultMessage });
884
+ const { get, post, put } = useFetchClient();
885
+ const [activeTab, setActiveTab] = React.useState("campaigns");
886
+ const [campaigns, setCampaigns] = React.useState([]);
887
+ const [rules, setRules] = React.useState([]);
888
+ const [logs, setLogs] = React.useState([]);
889
+ const [emailTemplates, setEmailTemplates] = React.useState([]);
890
+ const [statusMessage, setStatusMessage] = React.useState("");
891
+ const [statusIsError, setStatusIsError] = React.useState(false);
892
+ const [isBusy, setIsBusy] = React.useState(false);
893
+ const [editingCampaignId, setEditingCampaignId] = React.useState(null);
894
+ const [newSlug, setNewSlug] = React.useState("");
895
+ const [newTitle, setNewTitle] = React.useState("");
896
+ const [newTopic, setNewTopic] = React.useState("announcements");
897
+ const [newPriority, setNewPriority] = React.useState("normal");
898
+ const [newChannels, setNewChannels] = React.useState(DEFAULT_CHANNELS);
899
+ const [newEmailTemplateRef, setNewEmailTemplateRef] = React.useState("");
900
+ const [newSubject, setNewSubject] = React.useState("");
901
+ const [newBodyText, setNewBodyText] = React.useState("");
902
+ const notifyStatus = (message, isError = false) => {
903
+ setStatusIsError(isError);
904
+ setStatusMessage(message);
905
+ };
906
+ const loadAll = React.useCallback(
907
+ async (isMountedCheck = () => true) => {
908
+ try {
909
+ const [campRes, rulesRes, logsRes] = await Promise.all([
910
+ get(`/${PLUGIN_ID}/campaigns`),
911
+ get(
912
+ `/${PLUGIN_ID}/automation-rules`
913
+ ),
914
+ get(`/${PLUGIN_ID}/delivery-logs`)
915
+ ]);
916
+ if (!isMountedCheck()) return;
917
+ setCampaigns(campRes?.data?.data || []);
918
+ setRules(rulesRes?.data?.data || []);
919
+ setLogs(logsRes?.data?.data || []);
920
+ } catch {
921
+ }
922
+ try {
923
+ const tplRes = await get(
924
+ `/${PLUGIN_ID}/email-templates`
925
+ );
926
+ if (isMountedCheck() && Array.isArray(tplRes?.data?.data)) {
927
+ setEmailTemplates(tplRes.data.data);
928
+ }
929
+ } catch {
930
+ }
931
+ },
932
+ [get]
933
+ );
934
+ React.useEffect(() => {
935
+ let isMounted = true;
936
+ void loadAll(() => isMounted);
937
+ return () => {
938
+ isMounted = false;
939
+ };
940
+ }, [loadAll]);
941
+ const resetCampaignForm = (camp, editId = null, fallbackChannels = DEFAULT_CHANNELS) => {
942
+ setEditingCampaignId(editId);
943
+ setNewSlug(camp?.slug || "");
944
+ setNewTitle(camp?.title || "");
945
+ setNewTopic(camp?.topic || "announcements");
946
+ setNewPriority(camp?.priority || "normal");
947
+ setNewChannels(camp?.channels?.length ? camp.channels : fallbackChannels);
948
+ setNewEmailTemplateRef(camp?.emailTemplateRef || "");
949
+ setNewSubject(camp?.subject || "");
950
+ setNewBodyText(camp?.bodyText || "");
951
+ };
952
+ const handleStartEditCampaign = (camp) => {
953
+ resetCampaignForm(camp, camp.documentId || camp.slug, [
954
+ "email",
955
+ "telegram"
956
+ ]);
957
+ notifyStatus(
958
+ `${t("page.ui.editing_campaign", "Editing campaign")}: ${camp.title}`
959
+ );
960
+ };
961
+ const handleSeedTemplate = async (templateId) => {
962
+ if (isBusy) return;
963
+ setIsBusy(true);
964
+ try {
965
+ await post(`/${PLUGIN_ID}/campaigns/from-template`, { templateId });
966
+ notifyStatus(
967
+ `${t("page.ui.created_from_starter_template", "Created campaign from starter template")}: ${templateId}`
968
+ );
969
+ await loadAll();
970
+ } catch (err) {
971
+ notifyStatus(
972
+ extractApiErrorMessage(
973
+ err,
974
+ t("page.ui.operation_failed", "Broadcast operation failed.")
975
+ ),
976
+ true
977
+ );
978
+ } finally {
979
+ setIsBusy(false);
980
+ }
981
+ };
982
+ const handleDispatchCampaign = async (slugOrDocId) => {
983
+ if (!slugOrDocId || isBusy) return;
984
+ setIsBusy(true);
985
+ try {
986
+ await post(`/${PLUGIN_ID}/campaigns/${slugOrDocId}/send`, {});
987
+ notifyStatus(
988
+ `${t("page.ui.dispatched_campaign", "Dispatched broadcast campaign")}: ${slugOrDocId}`
989
+ );
990
+ await loadAll();
991
+ } catch (err) {
992
+ notifyStatus(
993
+ extractApiErrorMessage(
994
+ err,
995
+ t("page.ui.operation_failed", "Broadcast operation failed.")
996
+ ),
997
+ true
998
+ );
999
+ } finally {
1000
+ setIsBusy(false);
1001
+ }
1002
+ };
1003
+ const handleSaveCampaign = async () => {
1004
+ if (!newSlug || !newTitle || !newSubject || !newBodyText || isBusy) return;
1005
+ const payload = {
1006
+ slug: newSlug.trim(),
1007
+ title: newTitle.trim(),
1008
+ topic: newTopic.trim() || "announcements",
1009
+ channels: parseChannelsInput(newChannels, DEFAULT_CHANNELS),
1010
+ priority: editingCampaignId ? newPriority : "normal",
1011
+ emailTemplateRef: newEmailTemplateRef.trim(),
1012
+ subject: newSubject.trim(),
1013
+ bodyText: newBodyText.trim()
1014
+ };
1015
+ setIsBusy(true);
1016
+ try {
1017
+ if (editingCampaignId) {
1018
+ await put(`/${PLUGIN_ID}/campaigns/${editingCampaignId}`, payload);
1019
+ notifyStatus(
1020
+ `${t("page.ui.updated_campaign", "Updated broadcast campaign")}: ${payload.title}`
1021
+ );
1022
+ } else {
1023
+ await post(`/${PLUGIN_ID}/campaigns`, payload);
1024
+ notifyStatus(
1025
+ t(
1026
+ "page.ui.created_omnichannel_campaign",
1027
+ "Created omnichannel broadcast campaign"
1028
+ )
1029
+ );
1030
+ }
1031
+ resetCampaignForm();
1032
+ await loadAll();
1033
+ } catch (err) {
1034
+ notifyStatus(
1035
+ extractApiErrorMessage(
1036
+ err,
1037
+ t("page.ui.operation_failed", "Broadcast operation failed.")
1038
+ ),
1039
+ true
1040
+ );
1041
+ } finally {
1042
+ setIsBusy(false);
1043
+ }
1044
+ };
1045
+ const tabs = [
1046
+ ["campaigns", t("page.ui.campaigns_tab", "Campaigns"), campaigns.length],
1047
+ [
1048
+ "automations",
1049
+ t("page.ui.automation_rules_tab", "Automation Rules"),
1050
+ rules.length
1051
+ ],
1052
+ ["logs", t("page.ui.delivery_ledger_tab", "Delivery Ledger"), logs.length]
1053
+ ];
1054
+ return /* @__PURE__ */ jsx(Main, { children: /* @__PURE__ */ jsxs(Box, { padding: 8, children: [
1055
+ /* @__PURE__ */ jsxs(
1056
+ Flex,
1057
+ {
1058
+ justifyContent: "space-between",
1059
+ alignItems: "flex-start",
1060
+ paddingBottom: 4,
1061
+ wrap: "wrap",
1062
+ gap: 4,
1063
+ children: [
1064
+ /* @__PURE__ */ jsxs(Box, { children: [
1065
+ /* @__PURE__ */ jsx(Typography, { variant: "alpha", tag: "h1", children: t(
1066
+ "page.ui.omnichannel_community_broadcasts_automations",
1067
+ "Omnichannel Community Broadcasts & Automations"
1068
+ ) }),
1069
+ /* @__PURE__ */ jsx(Typography, { variant: "epsilon", textColor: "neutral600", children: t(
1070
+ "page.ui.email_newsletters_telegram_bot_forum_topics_what",
1071
+ "Email newsletters, Telegram Bot & Forum topics, WhatsApp Cloud API / Gateways, EventHub auto-triggers & Hijri dates"
1072
+ ) })
1073
+ ] }),
1074
+ /* @__PURE__ */ jsx(Flex, { gap: 2, wrap: "wrap", children: tabs.map(([tabKey, label, count]) => /* @__PURE__ */ jsxs(
1075
+ Button,
1076
+ {
1077
+ variant: activeTab === tabKey ? "default" : "tertiary",
1078
+ onClick: () => setActiveTab(tabKey),
1079
+ children: [
1080
+ label,
1081
+ " (",
1082
+ count,
1083
+ ")"
1084
+ ]
1085
+ },
1086
+ tabKey
1087
+ )) })
1088
+ ]
1089
+ }
1090
+ ),
1091
+ statusMessage && /* @__PURE__ */ jsx(
1092
+ Box,
1093
+ {
1094
+ padding: 3,
1095
+ marginBottom: 4,
1096
+ background: statusIsError ? "danger100" : "success100",
1097
+ hasRadius: true,
1098
+ role: statusIsError ? "alert" : "status",
1099
+ children: /* @__PURE__ */ jsx(
1100
+ Typography,
1101
+ {
1102
+ textColor: statusIsError ? "danger700" : "success700",
1103
+ fontWeight: "semiBold",
1104
+ children: statusMessage
1105
+ }
1106
+ )
1107
+ }
1108
+ ),
1109
+ /* @__PURE__ */ jsx(Divider, {}),
1110
+ activeTab === "campaigns" && /* @__PURE__ */ jsxs(Box, { paddingTop: 6, children: [
1111
+ /* @__PURE__ */ jsxs(
1112
+ Flex,
1113
+ {
1114
+ justifyContent: "space-between",
1115
+ alignItems: "center",
1116
+ paddingBottom: 4,
1117
+ wrap: "wrap",
1118
+ gap: 2,
1119
+ children: [
1120
+ /* @__PURE__ */ jsx(Typography, { variant: "beta", tag: "h2", children: t(
1121
+ "page.ui.starter_templates_broadcast_campaigns",
1122
+ "Starter Templates & Broadcast Campaigns"
1123
+ ) }),
1124
+ /* @__PURE__ */ jsxs(Flex, { gap: 2, wrap: "wrap", children: [
1125
+ /* @__PURE__ */ jsxs(
1126
+ Button,
1127
+ {
1128
+ variant: "tertiary",
1129
+ onClick: () => navigate("/plugins/email-designer-5"),
1130
+ children: [
1131
+ t("page.ui.open_email_designer", "Open Email Designer"),
1132
+ " (",
1133
+ emailTemplates.length,
1134
+ ")"
1135
+ ]
1136
+ }
1137
+ ),
1138
+ STARTER_CAMPAIGN_TEMPLATES.map((tpl) => /* @__PURE__ */ jsxs(
1139
+ Button,
1140
+ {
1141
+ variant: "secondary",
1142
+ disabled: isBusy,
1143
+ onClick: () => handleSeedTemplate(tpl.id),
1144
+ children: [
1145
+ "+ ",
1146
+ tpl.slug
1147
+ ]
1148
+ },
1149
+ tpl.id
1150
+ ))
1151
+ ] })
1152
+ ]
1153
+ }
1154
+ ),
1155
+ /* @__PURE__ */ jsx(
1156
+ CampaignComposerForm,
1157
+ {
1158
+ t,
1159
+ editingCampaignId,
1160
+ isBusy,
1161
+ newSlug,
1162
+ setNewSlug,
1163
+ newTitle,
1164
+ setNewTitle,
1165
+ newTopic,
1166
+ setNewTopic,
1167
+ newChannels,
1168
+ setNewChannels,
1169
+ newEmailTemplateRef,
1170
+ setNewEmailTemplateRef,
1171
+ newSubject,
1172
+ setNewSubject,
1173
+ newBodyText,
1174
+ setNewBodyText,
1175
+ emailTemplates,
1176
+ onCancelEdit: () => resetCampaignForm(),
1177
+ onSave: handleSaveCampaign
1178
+ }
1179
+ ),
1180
+ /* @__PURE__ */ jsx(
1181
+ CampaignsTableSection,
1182
+ {
1183
+ t,
1184
+ campaigns,
1185
+ onEdit: handleStartEditCampaign,
1186
+ onDispatch: handleDispatchCampaign,
1187
+ navigate,
1188
+ isBusy
1189
+ }
1190
+ )
1191
+ ] }),
1192
+ /* @__PURE__ */ jsx(
1193
+ AutomationAndLogsTabs,
1194
+ {
1195
+ activeTab,
1196
+ rules,
1197
+ logs,
1198
+ t,
1199
+ navigate,
1200
+ post,
1201
+ put,
1202
+ notifyStatus,
1203
+ loadAll
1204
+ }
1205
+ )
1206
+ ] }) });
1207
+ };
1208
+ export {
1209
+ BroadcastsHomePage
1210
+ };