@krak-stack/notifications-admin 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.
@@ -0,0 +1,419 @@
1
+ import {
2
+ Button,
3
+ Dialog,
4
+ DialogContent,
5
+ DialogHeader,
6
+ DialogTitle
7
+ } from "./chunk-tck9drx2.js";
8
+
9
+ // src/campaigns.tsx
10
+ import { useState } from "react";
11
+ import { Effect, Schema } from "effect";
12
+ import {
13
+ CampaignForm,
14
+ CampaignScheduleForm
15
+ } from "@krak-stack/notifications/admin/schema";
16
+ import {
17
+ DataTable
18
+ } from "@krak-stack/registry/data-table";
19
+ import { useAppForm } from "@krak-stack/registry/form";
20
+ import { jsx, jsxs } from "react/jsx-runtime";
21
+ var metricKeys = [
22
+ "audience",
23
+ "pending",
24
+ "published",
25
+ "skipped",
26
+ "accepted",
27
+ "failed",
28
+ "suppressed",
29
+ "confirmed",
30
+ "hardBounced",
31
+ "complained"
32
+ ];
33
+ var SendCampaignAdmin2 = ({
34
+ page,
35
+ selected,
36
+ report,
37
+ pending,
38
+ error,
39
+ messages: m,
40
+ onSelect,
41
+ onClose,
42
+ onRecipients,
43
+ table,
44
+ onCreate,
45
+ onSchedule,
46
+ onTransition
47
+ }) => {
48
+ const [creating, setCreating] = useState(false);
49
+ const [requestId, setRequestId] = useState("");
50
+ const [confirming, setConfirming] = useState(false);
51
+ const [busy, setBusy] = useState(false);
52
+ const [failure, setFailure] = useState();
53
+ const [initialSchedule] = useState(() => new Date().toISOString());
54
+ const createForm = useAppForm({
55
+ defaultValues: {
56
+ name: "",
57
+ from: "",
58
+ enSubject: "",
59
+ enText: "",
60
+ frSubject: "",
61
+ frText: ""
62
+ },
63
+ validators: { onSubmit: Schema.toStandardSchemaV1(CampaignForm) },
64
+ onSubmit: async ({ value }) => {
65
+ setFailure(undefined);
66
+ try {
67
+ await onCreate({
68
+ requestId,
69
+ name: value.name,
70
+ from: value.from,
71
+ audience: { _tag: "AllSubscribed" },
72
+ content: {
73
+ en: {
74
+ subject: value.enSubject,
75
+ text: value.enText,
76
+ unsubscribeText: m.enUnsubscribe
77
+ },
78
+ fr: {
79
+ subject: value.frSubject,
80
+ text: value.frText,
81
+ unsubscribeText: m.frUnsubscribe
82
+ }
83
+ }
84
+ });
85
+ setCreating(false);
86
+ createForm.reset();
87
+ } catch {
88
+ setFailure(m.failed);
89
+ }
90
+ }
91
+ });
92
+ const scheduleForm = useAppForm({
93
+ defaultValues: { scheduledFor: initialSchedule },
94
+ validators: { onSubmit: Schema.toStandardSchemaV1(CampaignScheduleForm) },
95
+ onSubmit: async ({ value }) => {
96
+ if (!selected)
97
+ return;
98
+ setFailure(undefined);
99
+ try {
100
+ const decoded = await Effect.runPromise(Schema.decodeEffect(CampaignScheduleForm)(value));
101
+ await onSchedule({
102
+ id: selected.id,
103
+ scheduledFor: decoded.scheduledFor
104
+ });
105
+ } catch {
106
+ setFailure(m.failed);
107
+ }
108
+ }
109
+ });
110
+ const transition = async (action) => {
111
+ if (!selected)
112
+ return;
113
+ setBusy(true);
114
+ setFailure(undefined);
115
+ try {
116
+ await onTransition({ id: selected.id, action });
117
+ setConfirming(false);
118
+ } catch {
119
+ setFailure(m.failed);
120
+ } finally {
121
+ setBusy(false);
122
+ }
123
+ };
124
+ const columns = [
125
+ { field: "name", headerName: m.name },
126
+ { field: "from", headerName: m.from },
127
+ { field: "scheduledFor", headerName: m.scheduledFor, type: "date" },
128
+ {
129
+ field: "status",
130
+ headerName: m.status,
131
+ filterOptions: Object.entries(m.statuses).map(([value, label]) => ({
132
+ value,
133
+ label
134
+ })),
135
+ cellRenderer: ({ data }) => m.statuses[data.status]
136
+ },
137
+ {
138
+ colId: "manage",
139
+ sortable: false,
140
+ filterable: false,
141
+ searchable: false,
142
+ hideable: false,
143
+ headerName: m.manage,
144
+ cellRenderer: ({ data }) => /* @__PURE__ */ jsx(Button, {
145
+ variant: "outline",
146
+ onClick: () => {
147
+ setFailure(undefined);
148
+ onSelect(data);
149
+ },
150
+ children: m.manage
151
+ })
152
+ }
153
+ ];
154
+ return /* @__PURE__ */ jsxs("section", {
155
+ className: "space-y-4",
156
+ "aria-busy": pending || busy,
157
+ children: [
158
+ /* @__PURE__ */ jsxs("header", {
159
+ className: "flex items-start justify-between gap-4",
160
+ children: [
161
+ /* @__PURE__ */ jsxs("div", {
162
+ children: [
163
+ /* @__PURE__ */ jsx("h1", {
164
+ className: "text-2xl font-semibold",
165
+ children: m.title
166
+ }),
167
+ /* @__PURE__ */ jsx("p", {
168
+ children: m.description
169
+ })
170
+ ]
171
+ }),
172
+ /* @__PURE__ */ jsx(Button, {
173
+ onClick: () => {
174
+ setRequestId(crypto.randomUUID());
175
+ setFailure(undefined);
176
+ setCreating(true);
177
+ },
178
+ children: m.create
179
+ })
180
+ ]
181
+ }),
182
+ (error || failure && !creating) && /* @__PURE__ */ jsx("p", {
183
+ role: "alert",
184
+ children: error || failure
185
+ }),
186
+ /* @__PURE__ */ jsx(DataTable, {
187
+ columnDefs: columns,
188
+ rowData: page?.items ?? [],
189
+ getRowId: (campaign) => campaign.id,
190
+ features: { pagination: { mode: "client" } },
191
+ status: { loading: pending, error },
192
+ ...table
193
+ }),
194
+ selected && /* @__PURE__ */ jsxs("section", {
195
+ "aria-label": selected.name,
196
+ className: "space-y-4 rounded border p-4",
197
+ children: [
198
+ /* @__PURE__ */ jsx("h2", {
199
+ children: selected.name
200
+ }),
201
+ /* @__PURE__ */ jsx("p", {
202
+ children: m.statuses[selected.status]
203
+ }),
204
+ /* @__PURE__ */ jsxs("dl", {
205
+ children: [
206
+ /* @__PURE__ */ jsx("dt", {
207
+ children: m.from
208
+ }),
209
+ /* @__PURE__ */ jsx("dd", {
210
+ children: selected.from
211
+ }),
212
+ /* @__PURE__ */ jsx("dt", {
213
+ children: m.scheduledFor
214
+ }),
215
+ /* @__PURE__ */ jsx("dd", {
216
+ children: selected.scheduledFor?.toISOString() ?? "—"
217
+ })
218
+ ]
219
+ }),
220
+ ["en", "fr"].map((locale) => /* @__PURE__ */ jsxs("details", {
221
+ children: [
222
+ /* @__PURE__ */ jsx("summary", {
223
+ children: locale === "en" ? m.english : m.french
224
+ }),
225
+ /* @__PURE__ */ jsx("h3", {
226
+ children: selected.content[locale].subject
227
+ }),
228
+ /* @__PURE__ */ jsx("p", {
229
+ className: "whitespace-pre-wrap",
230
+ children: selected.content[locale].text
231
+ })
232
+ ]
233
+ }, locale)),
234
+ onRecipients && /* @__PURE__ */ jsx(Button, {
235
+ variant: "outline",
236
+ onClick: () => onRecipients(selected.id),
237
+ children: m.metrics.audience
238
+ }),
239
+ selected.status === "draft" && /* @__PURE__ */ jsx(scheduleForm.AppForm, {
240
+ children: /* @__PURE__ */ jsxs("form", {
241
+ onSubmit: (event) => {
242
+ event.preventDefault();
243
+ scheduleForm.handleSubmit();
244
+ },
245
+ className: "space-y-2",
246
+ children: [
247
+ /* @__PURE__ */ jsx(scheduleForm.AppField, {
248
+ name: "scheduledFor",
249
+ children: (field) => /* @__PURE__ */ jsx(field.TextField, {
250
+ label: m.scheduledFor
251
+ })
252
+ }),
253
+ /* @__PURE__ */ jsx(scheduleForm.SubmitButton, {
254
+ children: m.schedule
255
+ })
256
+ ]
257
+ })
258
+ }),
259
+ /* @__PURE__ */ jsxs("div", {
260
+ className: "flex gap-2",
261
+ children: [
262
+ ["scheduled", "running", "completed"].includes(selected.status) && /* @__PURE__ */ jsx(Button, {
263
+ disabled: pending || busy,
264
+ onClick: () => void transition("pause"),
265
+ children: m.pause
266
+ }),
267
+ selected.status === "paused" && /* @__PURE__ */ jsx(Button, {
268
+ disabled: pending || busy,
269
+ onClick: () => void transition("resume"),
270
+ children: m.resume
271
+ }),
272
+ selected.status !== "cancelled" && /* @__PURE__ */ jsx(Button, {
273
+ variant: "destructive",
274
+ disabled: pending || busy,
275
+ onClick: () => setConfirming(true),
276
+ children: m.cancel
277
+ }),
278
+ /* @__PURE__ */ jsx(Button, {
279
+ variant: "outline",
280
+ onClick: onClose,
281
+ children: m.close
282
+ })
283
+ ]
284
+ }),
285
+ report && /* @__PURE__ */ jsxs("div", {
286
+ children: [
287
+ /* @__PURE__ */ jsx("h3", {
288
+ children: m.reporting
289
+ }),
290
+ /* @__PURE__ */ jsx("dl", {
291
+ children: metricKeys.map((key) => /* @__PURE__ */ jsxs("div", {
292
+ className: "flex justify-between gap-4",
293
+ children: [
294
+ /* @__PURE__ */ jsx("dt", {
295
+ children: m.metrics[key]
296
+ }),
297
+ /* @__PURE__ */ jsx("dd", {
298
+ children: report[key]
299
+ })
300
+ ]
301
+ }, key))
302
+ })
303
+ ]
304
+ })
305
+ ]
306
+ }),
307
+ /* @__PURE__ */ jsx(Dialog, {
308
+ open: creating,
309
+ onOpenChange: setCreating,
310
+ children: /* @__PURE__ */ jsxs(DialogContent, {
311
+ children: [
312
+ /* @__PURE__ */ jsx(DialogHeader, {
313
+ children: /* @__PURE__ */ jsx(DialogTitle, {
314
+ children: m.create
315
+ })
316
+ }),
317
+ /* @__PURE__ */ jsx(createForm.AppForm, {
318
+ children: /* @__PURE__ */ jsxs("form", {
319
+ className: "space-y-3",
320
+ onSubmit: (event) => {
321
+ event.preventDefault();
322
+ createForm.handleSubmit();
323
+ },
324
+ children: [
325
+ /* @__PURE__ */ jsx(createForm.AppField, {
326
+ name: "name",
327
+ children: (field) => /* @__PURE__ */ jsx(field.TextField, {
328
+ label: m.name
329
+ })
330
+ }),
331
+ /* @__PURE__ */ jsx(createForm.AppField, {
332
+ name: "from",
333
+ children: (field) => /* @__PURE__ */ jsx(field.TextField, {
334
+ label: m.from,
335
+ type: "email"
336
+ })
337
+ }),
338
+ /* @__PURE__ */ jsxs("fieldset", {
339
+ children: [
340
+ /* @__PURE__ */ jsx("legend", {
341
+ children: m.english
342
+ }),
343
+ /* @__PURE__ */ jsx(createForm.AppField, {
344
+ name: "enSubject",
345
+ children: (field) => /* @__PURE__ */ jsx(field.TextField, {
346
+ label: m.subject
347
+ })
348
+ }),
349
+ /* @__PURE__ */ jsx(createForm.AppField, {
350
+ name: "enText",
351
+ children: (field) => /* @__PURE__ */ jsx(field.TextAreaField, {
352
+ label: m.text
353
+ })
354
+ })
355
+ ]
356
+ }),
357
+ /* @__PURE__ */ jsxs("fieldset", {
358
+ children: [
359
+ /* @__PURE__ */ jsx("legend", {
360
+ children: m.french
361
+ }),
362
+ /* @__PURE__ */ jsx(createForm.AppField, {
363
+ name: "frSubject",
364
+ children: (field) => /* @__PURE__ */ jsx(field.TextField, {
365
+ label: m.subject
366
+ })
367
+ }),
368
+ /* @__PURE__ */ jsx(createForm.AppField, {
369
+ name: "frText",
370
+ children: (field) => /* @__PURE__ */ jsx(field.TextAreaField, {
371
+ label: m.text
372
+ })
373
+ })
374
+ ]
375
+ }),
376
+ failure && /* @__PURE__ */ jsx("p", {
377
+ role: "alert",
378
+ children: failure
379
+ }),
380
+ /* @__PURE__ */ jsx(createForm.SubmitButton, {
381
+ children: m.save
382
+ })
383
+ ]
384
+ })
385
+ })
386
+ ]
387
+ })
388
+ }),
389
+ /* @__PURE__ */ jsx(Dialog, {
390
+ open: confirming,
391
+ onOpenChange: setConfirming,
392
+ children: /* @__PURE__ */ jsxs(DialogContent, {
393
+ children: [
394
+ /* @__PURE__ */ jsx(DialogHeader, {
395
+ children: /* @__PURE__ */ jsx(DialogTitle, {
396
+ children: m.confirmCancel
397
+ })
398
+ }),
399
+ /* @__PURE__ */ jsx("p", {
400
+ children: m.cancelDescription
401
+ }),
402
+ failure && /* @__PURE__ */ jsx("p", {
403
+ role: "alert",
404
+ children: failure
405
+ }),
406
+ /* @__PURE__ */ jsx(Button, {
407
+ variant: "destructive",
408
+ disabled: busy,
409
+ onClick: () => void transition("cancel"),
410
+ children: m.confirmCancel
411
+ })
412
+ ]
413
+ })
414
+ })
415
+ ]
416
+ });
417
+ };
418
+
419
+ export { SendCampaignAdmin2 };