@agilo/medusa-email-templates-plugin 0.0.0-stage → 1.0.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.
- package/.medusa/server/medusa-config.d.ts +2 -0
- package/.medusa/server/medusa-config.js +57 -0
- package/.medusa/server/src/admin/_chunks/__admin-extensions__-B-Rs6wVu +1412 -0
- package/.medusa/server/src/admin/_chunks/__admin-extensions__-Cu08yi-Z +1413 -0
- package/.medusa/server/src/admin/_chunks/email-template-editor-JGYZjesE +820 -0
- package/.medusa/server/src/admin/_chunks/email-template-editor-_uBb7FNZ +820 -0
- package/.medusa/server/src/admin/_chunks/subject-editor-DsSopX-7 +115 -0
- package/.medusa/server/src/admin/_chunks/subject-editor-ykrWWMtp +115 -0
- package/.medusa/server/src/admin/_chunks/variable-node-BO5f9lJL +353 -0
- package/.medusa/server/src/admin/_chunks/variable-node-D2KY7H-p +352 -0
- package/.medusa/server/src/admin/index.js +3 -0
- package/.medusa/server/src/admin/index.mjs +4 -0
- package/.medusa/server/src/admin/tailwind-safelist.js +204 -0
- package/.medusa/server/src/api/admin/email/error-response.d.ts +2 -0
- package/.medusa/server/src/api/admin/email/error-response.js +26 -0
- package/.medusa/server/src/api/admin/email/middlewares.d.ts +2 -0
- package/.medusa/server/src/api/admin/email/middlewares.js +34 -0
- package/.medusa/server/src/api/admin/email/provider/route.d.ts +4 -0
- package/.medusa/server/src/api/admin/email/provider/route.js +36 -0
- package/.medusa/server/src/api/admin/email/provider/send-test/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/email/provider/send-test/route.js +23 -0
- package/.medusa/server/src/api/admin/email/provider/validators.d.ts +25 -0
- package/.medusa/server/src/api/admin/email/provider/validators.js +27 -0
- package/.medusa/server/src/api/admin/email/templates/[id]/preview/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/email/templates/[id]/preview/route.js +16 -0
- package/.medusa/server/src/api/admin/email/templates/[id]/route.d.ts +5 -0
- package/.medusa/server/src/api/admin/email/templates/[id]/route.js +45 -0
- package/.medusa/server/src/api/admin/email/templates/[id]/send-test/route.d.ts +3 -0
- package/.medusa/server/src/api/admin/email/templates/[id]/send-test/route.js +25 -0
- package/.medusa/server/src/api/admin/email/templates/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/email/templates/route.js +12 -0
- package/.medusa/server/src/api/admin/email/templates/validators.d.ts +28 -0
- package/.medusa/server/src/api/admin/email/templates/validators.js +30 -0
- package/.medusa/server/src/api/admin/plugin/route.d.ts +2 -0
- package/.medusa/server/src/api/admin/plugin/route.js +7 -0
- package/.medusa/server/src/api/middlewares.d.ts +2 -0
- package/.medusa/server/src/api/middlewares.js +8 -0
- package/.medusa/server/src/api/store/plugin/route.d.ts +2 -0
- package/.medusa/server/src/api/store/plugin/route.js +7 -0
- package/.medusa/server/src/lib/container.d.ts +8 -0
- package/.medusa/server/src/lib/container.js +23 -0
- package/.medusa/server/src/lib/content.d.ts +4 -0
- package/.medusa/server/src/lib/content.js +9 -0
- package/.medusa/server/src/lib/credentials/mask.d.ts +10 -0
- package/.medusa/server/src/lib/credentials/mask.js +55 -0
- package/.medusa/server/src/lib/credentials/resolve-credential.d.ts +11 -0
- package/.medusa/server/src/lib/credentials/resolve-credential.js +34 -0
- package/.medusa/server/src/lib/crypto/credential-cipher.d.ts +7 -0
- package/.medusa/server/src/lib/crypto/credential-cipher.js +65 -0
- package/.medusa/server/src/lib/crypto/encryption-key.d.ts +9 -0
- package/.medusa/server/src/lib/crypto/encryption-key.js +45 -0
- package/.medusa/server/src/lib/errors.d.ts +18 -0
- package/.medusa/server/src/lib/errors.js +40 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/account.d.ts +6 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/account.js +48 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/design.d.ts +67 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/design.js +94 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/order-placed.d.ts +5 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/order-placed.js +59 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/repeat.d.ts +3 -0
- package/.medusa/server/src/lib/events/built-in/default-templates/repeat.js +23 -0
- package/.medusa/server/src/lib/events/built-in/email-verification.d.ts +13 -0
- package/.medusa/server/src/lib/events/built-in/email-verification.js +53 -0
- package/.medusa/server/src/lib/events/built-in/invite.d.ts +5 -0
- package/.medusa/server/src/lib/events/built-in/invite.js +91 -0
- package/.medusa/server/src/lib/events/built-in/order-placed.d.ts +4 -0
- package/.medusa/server/src/lib/events/built-in/order-placed.js +234 -0
- package/.medusa/server/src/lib/events/built-in/password-reset.d.ts +12 -0
- package/.medusa/server/src/lib/events/built-in/password-reset.js +83 -0
- package/.medusa/server/src/lib/events/built-in/store.d.ts +3 -0
- package/.medusa/server/src/lib/events/built-in/store.js +12 -0
- package/.medusa/server/src/lib/events/define-email-event.d.ts +6 -0
- package/.medusa/server/src/lib/events/define-email-event.js +11 -0
- package/.medusa/server/src/lib/events/index.d.ts +8 -0
- package/.medusa/server/src/lib/events/index.js +21 -0
- package/.medusa/server/src/lib/events/registry.d.ts +9 -0
- package/.medusa/server/src/lib/events/registry.js +55 -0
- package/.medusa/server/src/lib/events/types.d.ts +82 -0
- package/.medusa/server/src/lib/events/types.js +3 -0
- package/.medusa/server/src/lib/events/urls.d.ts +23 -0
- package/.medusa/server/src/lib/events/urls.js +79 -0
- package/.medusa/server/src/lib/provider-configuration.d.ts +51 -0
- package/.medusa/server/src/lib/provider-configuration.js +114 -0
- package/.medusa/server/src/lib/redact.d.ts +1 -0
- package/.medusa/server/src/lib/redact.js +12 -0
- package/.medusa/server/src/lib/rendering/format-value.d.ts +21 -0
- package/.medusa/server/src/lib/rendering/format-value.js +55 -0
- package/.medusa/server/src/lib/rendering/liquid.d.ts +5 -0
- package/.medusa/server/src/lib/rendering/liquid.js +52 -0
- package/.medusa/server/src/lib/rendering/render-template.d.ts +12 -0
- package/.medusa/server/src/lib/rendering/render-template.js +46 -0
- package/.medusa/server/src/lib/rendering/sanitize-context.d.ts +1 -0
- package/.medusa/server/src/lib/rendering/sanitize-context.js +14 -0
- package/.medusa/server/src/lib/rendering/variable-format.d.ts +43 -0
- package/.medusa/server/src/lib/rendering/variable-format.js +103 -0
- package/.medusa/server/src/lib/resolve-email-content.d.ts +9 -0
- package/.medusa/server/src/lib/resolve-email-content.js +35 -0
- package/.medusa/server/src/lib/templates.d.ts +55 -0
- package/.medusa/server/src/lib/templates.js +161 -0
- package/.medusa/server/src/lib/test-email.d.ts +7 -0
- package/.medusa/server/src/lib/test-email.js +21 -0
- package/.medusa/server/src/lib/transports/address.d.ts +3 -0
- package/.medusa/server/src/lib/transports/address.js +16 -0
- package/.medusa/server/src/lib/transports/cloud-config.d.ts +13 -0
- package/.medusa/server/src/lib/transports/cloud-config.js +30 -0
- package/.medusa/server/src/lib/transports/mailgun.d.ts +15 -0
- package/.medusa/server/src/lib/transports/mailgun.js +116 -0
- package/.medusa/server/src/lib/transports/medusa-cloud.d.ts +15 -0
- package/.medusa/server/src/lib/transports/medusa-cloud.js +81 -0
- package/.medusa/server/src/lib/transports/registry.d.ts +4 -0
- package/.medusa/server/src/lib/transports/registry.js +24 -0
- package/.medusa/server/src/lib/transports/resend.d.ts +11 -0
- package/.medusa/server/src/lib/transports/resend.js +93 -0
- package/.medusa/server/src/lib/transports/types.d.ts +46 -0
- package/.medusa/server/src/lib/transports/types.js +10 -0
- package/.medusa/server/src/modules/email-templates/index.d.ts +49 -0
- package/.medusa/server/src/modules/email-templates/index.js +13 -0
- package/.medusa/server/src/modules/email-templates/migrations/Migration20260915141601.d.ts +5 -0
- package/.medusa/server/src/modules/email-templates/migrations/Migration20260915141601.js +27 -0
- package/.medusa/server/src/modules/email-templates/migrations/Migration20260916090537.d.ts +5 -0
- package/.medusa/server/src/modules/email-templates/migrations/Migration20260916090537.js +18 -0
- package/.medusa/server/src/modules/email-templates/models/email-provider-configuration.d.ts +24 -0
- package/.medusa/server/src/modules/email-templates/models/email-provider-configuration.js +32 -0
- package/.medusa/server/src/modules/email-templates/models/email-provider-credential.d.ts +21 -0
- package/.medusa/server/src/modules/email-templates/models/email-provider-credential.js +32 -0
- package/.medusa/server/src/modules/email-templates/models/email-template.d.ts +11 -0
- package/.medusa/server/src/modules/email-templates/models/email-template.js +25 -0
- package/.medusa/server/src/modules/email-templates/service.d.ts +70 -0
- package/.medusa/server/src/modules/email-templates/service.js +24 -0
- package/.medusa/server/src/providers/email/index.d.ts +2 -0
- package/.medusa/server/src/providers/email/index.js +11 -0
- package/.medusa/server/src/providers/email/service.d.ts +17 -0
- package/.medusa/server/src/providers/email/service.js +57 -0
- package/.medusa/server/src/registry.d.ts +34 -0
- package/.medusa/server/src/registry.js +49 -0
- package/.medusa/server/src/subscribers/email-account-events.d.ts +3 -0
- package/.medusa/server/src/subscribers/email-account-events.js +31 -0
- package/.medusa/server/src/subscribers/email-order-placed.d.ts +5 -0
- package/.medusa/server/src/subscribers/email-order-placed.js +26 -0
- package/.medusa/server/src/workflows/index.d.ts +6 -0
- package/.medusa/server/src/workflows/index.js +23 -0
- package/.medusa/server/src/workflows/preview-email-template.d.ts +3 -0
- package/.medusa/server/src/workflows/preview-email-template.js +11 -0
- package/.medusa/server/src/workflows/reset-email-template.d.ts +6 -0
- package/.medusa/server/src/workflows/reset-email-template.js +11 -0
- package/.medusa/server/src/workflows/save-email-provider-configuration.d.ts +6 -0
- package/.medusa/server/src/workflows/save-email-provider-configuration.js +28 -0
- package/.medusa/server/src/workflows/send-email-event.d.ts +3 -0
- package/.medusa/server/src/workflows/send-email-event.js +13 -0
- package/.medusa/server/src/workflows/send-provider-test-email.d.ts +7 -0
- package/.medusa/server/src/workflows/send-provider-test-email.js +11 -0
- package/.medusa/server/src/workflows/send-template-test-email.d.ts +12 -0
- package/.medusa/server/src/workflows/send-template-test-email.js +30 -0
- package/.medusa/server/src/workflows/steps/assert-email-provider-ready.d.ts +5 -0
- package/.medusa/server/src/workflows/steps/assert-email-provider-ready.js +21 -0
- package/.medusa/server/src/workflows/steps/render-email-template.d.ts +8 -0
- package/.medusa/server/src/workflows/steps/render-email-template.js +22 -0
- package/.medusa/server/src/workflows/steps/reset-email-template.d.ts +14 -0
- package/.medusa/server/src/workflows/steps/reset-email-template.js +69 -0
- package/.medusa/server/src/workflows/steps/resolve-email-event.d.ts +20 -0
- package/.medusa/server/src/workflows/steps/resolve-email-event.js +56 -0
- package/.medusa/server/src/workflows/steps/select-email-provider-configuration.d.ts +27 -0
- package/.medusa/server/src/workflows/steps/select-email-provider-configuration.js +62 -0
- package/.medusa/server/src/workflows/steps/send-email-event-notification.d.ts +9 -0
- package/.medusa/server/src/workflows/steps/send-email-event-notification.js +70 -0
- package/.medusa/server/src/workflows/steps/send-test-email.d.ts +15 -0
- package/.medusa/server/src/workflows/steps/send-test-email.js +31 -0
- package/.medusa/server/src/workflows/steps/send-transport-test-email.d.ts +16 -0
- package/.medusa/server/src/workflows/steps/send-transport-test-email.js +49 -0
- package/.medusa/server/src/workflows/steps/upsert-email-provider-credential.d.ts +10 -0
- package/.medusa/server/src/workflows/steps/upsert-email-provider-credential.js +84 -0
- package/.medusa/server/src/workflows/steps/upsert-email-template.d.ts +22 -0
- package/.medusa/server/src/workflows/steps/upsert-email-template.js +94 -0
- package/.medusa/server/src/workflows/steps/validate-email-provider-configuration.d.ts +20 -0
- package/.medusa/server/src/workflows/steps/validate-email-provider-configuration.js +58 -0
- package/.medusa/server/src/workflows/upsert-email-template.d.ts +15 -0
- package/.medusa/server/src/workflows/upsert-email-template.js +11 -0
- package/LICENSE +21 -0
- package/README.md +169 -2
- package/package.json +130 -4
|
@@ -0,0 +1,1412 @@
|
|
|
1
|
+
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { defineRouteConfig } from "@medusajs/admin-sdk";
|
|
3
|
+
import { Check, PencilSquare, PaperPlane, ExclamationCircle, ArrowPath, Eye, Envelope, ArrowLeft, Plus } from "@medusajs/icons";
|
|
4
|
+
import { Drawer, Label, Input, Text, Button, toast, Select, RadioGroup, createDataTableColumnHelper, StatusBadge, useDataTable, Container, Alert, DataTable, Heading, Skeleton, Tabs, usePrompt, Switch, Tooltip, IconButton, DropdownMenu } from "@medusajs/ui";
|
|
5
|
+
import { useState, useMemo, useEffect, useCallback, lazy, useRef, Suspense } from "react";
|
|
6
|
+
import { useQuery, useQueryClient, useMutation } from "@tanstack/react-query";
|
|
7
|
+
import Medusa from "@medusajs/js-sdk";
|
|
8
|
+
import { useNavigate, useParams } from "react-router-dom";
|
|
9
|
+
const sdk = new Medusa({
|
|
10
|
+
baseUrl: "/",
|
|
11
|
+
debug: false,
|
|
12
|
+
auth: {
|
|
13
|
+
type: "session"
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
const emailProviderQueryKey = ["email", "provider"];
|
|
17
|
+
const useEmailProvider = () => useQuery({
|
|
18
|
+
queryKey: emailProviderQueryKey,
|
|
19
|
+
queryFn: () => sdk.client.fetch(
|
|
20
|
+
"/admin/email/provider"
|
|
21
|
+
),
|
|
22
|
+
select: (data) => data.provider
|
|
23
|
+
});
|
|
24
|
+
const useSaveEmailProvider = () => {
|
|
25
|
+
const queryClient = useQueryClient();
|
|
26
|
+
return useMutation({
|
|
27
|
+
mutationFn: (payload) => sdk.client.fetch(
|
|
28
|
+
"/admin/email/provider",
|
|
29
|
+
{ method: "POST", body: payload }
|
|
30
|
+
),
|
|
31
|
+
onSuccess: (data) => {
|
|
32
|
+
queryClient.setQueryData(emailProviderQueryKey, data);
|
|
33
|
+
queryClient.invalidateQueries({ queryKey: emailProviderQueryKey });
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const useSendProviderTestEmail = () => useMutation({
|
|
38
|
+
mutationFn: (variables) => sdk.client.fetch(
|
|
39
|
+
"/admin/email/provider/send-test",
|
|
40
|
+
{ method: "POST", body: variables }
|
|
41
|
+
)
|
|
42
|
+
});
|
|
43
|
+
const readBody = (error) => {
|
|
44
|
+
if (!error || typeof error !== "object") {
|
|
45
|
+
return {};
|
|
46
|
+
}
|
|
47
|
+
const candidate = error;
|
|
48
|
+
return candidate.response?.data ?? candidate.body ?? candidate;
|
|
49
|
+
};
|
|
50
|
+
const getApiErrorMessage = (error, fallback = "Something went wrong.") => {
|
|
51
|
+
const { message } = readBody(error);
|
|
52
|
+
if (typeof message === "string" && message.length) {
|
|
53
|
+
return message;
|
|
54
|
+
}
|
|
55
|
+
return error instanceof Error && error.message ? error.message : fallback;
|
|
56
|
+
};
|
|
57
|
+
const TestEmailDrawer = ({ transport, onClose }) => {
|
|
58
|
+
const sendTest = useSendProviderTestEmail();
|
|
59
|
+
const [recipient, setRecipient] = useState("");
|
|
60
|
+
const send = () => {
|
|
61
|
+
sendTest.mutate(
|
|
62
|
+
{ to: recipient, transport_id: transport.id },
|
|
63
|
+
{
|
|
64
|
+
onSuccess: () => {
|
|
65
|
+
toast.success(`Test email sent to ${recipient}.`);
|
|
66
|
+
onClose();
|
|
67
|
+
},
|
|
68
|
+
onError: (error) => toast.error(getApiErrorMessage(error))
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
};
|
|
72
|
+
return /* @__PURE__ */ jsx(Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxs(Drawer.Content, { children: [
|
|
73
|
+
/* @__PURE__ */ jsxs(Drawer.Header, { children: [
|
|
74
|
+
/* @__PURE__ */ jsx(Drawer.Title, { className: "font-medium", children: "Send test email" }),
|
|
75
|
+
/* @__PURE__ */ jsxs(Drawer.Description, { children: [
|
|
76
|
+
"Sends a fixed message through ",
|
|
77
|
+
transport.label,
|
|
78
|
+
"."
|
|
79
|
+
] })
|
|
80
|
+
] }),
|
|
81
|
+
/* @__PURE__ */ jsx(Drawer.Body, { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
82
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "test-recipient", children: "Recipient" }),
|
|
83
|
+
/* @__PURE__ */ jsx(
|
|
84
|
+
Input,
|
|
85
|
+
{
|
|
86
|
+
id: "test-recipient",
|
|
87
|
+
type: "email",
|
|
88
|
+
value: recipient,
|
|
89
|
+
onChange: (event) => setRecipient(event.target.value),
|
|
90
|
+
placeholder: "you@example.com"
|
|
91
|
+
}
|
|
92
|
+
),
|
|
93
|
+
/* @__PURE__ */ jsx(Text, { size: "xsmall", className: "text-ui-fg-subtle", children: "Sends through this transport's saved configuration, whether or not it is the active one. Save any changes before testing." })
|
|
94
|
+
] }) }),
|
|
95
|
+
/* @__PURE__ */ jsxs(Drawer.Footer, { children: [
|
|
96
|
+
/* @__PURE__ */ jsx(Drawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
|
|
97
|
+
/* @__PURE__ */ jsx(
|
|
98
|
+
Button,
|
|
99
|
+
{
|
|
100
|
+
size: "small",
|
|
101
|
+
disabled: !recipient || sendTest.isPending,
|
|
102
|
+
onClick: send,
|
|
103
|
+
children: "Send"
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
] })
|
|
107
|
+
] }) });
|
|
108
|
+
};
|
|
109
|
+
const asString = (value) => typeof value === "string" ? value : "";
|
|
110
|
+
const ENTERABLE_SOURCES = [
|
|
111
|
+
"environment",
|
|
112
|
+
"encrypted_database"
|
|
113
|
+
];
|
|
114
|
+
const TransportDrawer = ({
|
|
115
|
+
transport,
|
|
116
|
+
encryptionAvailable,
|
|
117
|
+
onClose
|
|
118
|
+
}) => {
|
|
119
|
+
const saveProvider = useSaveEmailProvider();
|
|
120
|
+
const [fromAddress2, setFromAddress] = useState(
|
|
121
|
+
asString(transport.config.from_address)
|
|
122
|
+
);
|
|
123
|
+
const [replyTo, setReplyTo] = useState(asString(transport.config.reply_to));
|
|
124
|
+
const [domain, setDomain] = useState(asString(transport.config.domain));
|
|
125
|
+
const [region, setRegion] = useState(
|
|
126
|
+
asString(transport.config.region) || "us"
|
|
127
|
+
);
|
|
128
|
+
const [source, setSource] = useState(
|
|
129
|
+
transport.credential?.source ?? transport.credential_sources.find(
|
|
130
|
+
(it) => ENTERABLE_SOURCES.includes(it)
|
|
131
|
+
) ?? "environment"
|
|
132
|
+
);
|
|
133
|
+
const [envVarName, setEnvVarName] = useState(
|
|
134
|
+
transport.credential?.env_var_name ?? ""
|
|
135
|
+
);
|
|
136
|
+
const [apiKey, setApiKey] = useState("");
|
|
137
|
+
const isMailgun = transport.id === "mailgun";
|
|
138
|
+
const isCloud = transport.credential_sources.includes("managed");
|
|
139
|
+
const sources = transport.credential_sources.filter(
|
|
140
|
+
(it) => ENTERABLE_SOURCES.includes(it)
|
|
141
|
+
);
|
|
142
|
+
const buildConfig = () => {
|
|
143
|
+
if (isCloud) {
|
|
144
|
+
return {};
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
from_address: fromAddress2,
|
|
148
|
+
...replyTo ? { reply_to: replyTo } : {},
|
|
149
|
+
...isMailgun ? { domain, region } : {}
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
const buildCredential = () => {
|
|
153
|
+
if (!sources.length) {
|
|
154
|
+
return void 0;
|
|
155
|
+
}
|
|
156
|
+
if (source === "environment") {
|
|
157
|
+
return { source: "environment", env_var_name: envVarName };
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
source: "encrypted_database",
|
|
161
|
+
...apiKey ? { api_key: apiKey } : {}
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
const save = () => {
|
|
165
|
+
saveProvider.mutate(
|
|
166
|
+
{
|
|
167
|
+
transport_id: transport.id,
|
|
168
|
+
config: buildConfig(),
|
|
169
|
+
credential: buildCredential()
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
onSuccess: () => {
|
|
173
|
+
toast.success(
|
|
174
|
+
transport.selected ? `${transport.label} updated.` : `${transport.label} now sends email.`
|
|
175
|
+
);
|
|
176
|
+
onClose();
|
|
177
|
+
},
|
|
178
|
+
onError: (error) => toast.error(getApiErrorMessage(error))
|
|
179
|
+
}
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
return /* @__PURE__ */ jsx(Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxs(Drawer.Content, { children: [
|
|
183
|
+
/* @__PURE__ */ jsxs(Drawer.Header, { children: [
|
|
184
|
+
/* @__PURE__ */ jsx(Drawer.Title, { className: "font-medium", children: transport.label }),
|
|
185
|
+
/* @__PURE__ */ jsx(Drawer.Description, { children: isCloud ? "Managed by your Medusa Cloud project." : "Sender address and API credential." })
|
|
186
|
+
] }),
|
|
187
|
+
/* @__PURE__ */ jsxs(Drawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: [
|
|
188
|
+
isCloud ? /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Nothing to enter. This transport forwards the whole notification envelope to the Cloud email service using the configuration already injected into this backend. Because the plugin owns the email channel, Medusa's automatic Cloud provider registration does not run." }) : /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
|
|
189
|
+
isMailgun && /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
190
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
191
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "mg-domain", children: "Sending domain" }),
|
|
192
|
+
/* @__PURE__ */ jsx(
|
|
193
|
+
Input,
|
|
194
|
+
{
|
|
195
|
+
id: "mg-domain",
|
|
196
|
+
value: domain,
|
|
197
|
+
onChange: (event) => setDomain(event.target.value),
|
|
198
|
+
placeholder: "mg.example.com"
|
|
199
|
+
}
|
|
200
|
+
)
|
|
201
|
+
] }),
|
|
202
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
203
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "mg-region", children: "Region" }),
|
|
204
|
+
/* @__PURE__ */ jsxs(Select, { value: region, onValueChange: setRegion, children: [
|
|
205
|
+
/* @__PURE__ */ jsx(Select.Trigger, { id: "mg-region", children: /* @__PURE__ */ jsx(Select.Value, { placeholder: "Select a region" }) }),
|
|
206
|
+
/* @__PURE__ */ jsxs(Select.Content, { children: [
|
|
207
|
+
/* @__PURE__ */ jsx(Select.Item, { value: "us", children: "US — api.mailgun.net" }),
|
|
208
|
+
/* @__PURE__ */ jsx(Select.Item, { value: "eu", children: "EU — api.eu.mailgun.net" })
|
|
209
|
+
] })
|
|
210
|
+
] }),
|
|
211
|
+
/* @__PURE__ */ jsx(Text, { size: "xsmall", className: "text-ui-fg-subtle", children: "Must match where the domain was created." })
|
|
212
|
+
] })
|
|
213
|
+
] }),
|
|
214
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
215
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "from-address", children: "From address" }),
|
|
216
|
+
/* @__PURE__ */ jsx(
|
|
217
|
+
Input,
|
|
218
|
+
{
|
|
219
|
+
id: "from-address",
|
|
220
|
+
value: fromAddress2,
|
|
221
|
+
onChange: (event) => setFromAddress(event.target.value),
|
|
222
|
+
placeholder: "orders@example.com"
|
|
223
|
+
}
|
|
224
|
+
)
|
|
225
|
+
] }),
|
|
226
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
227
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "reply-to", children: "Reply-to" }),
|
|
228
|
+
/* @__PURE__ */ jsx(
|
|
229
|
+
Input,
|
|
230
|
+
{
|
|
231
|
+
id: "reply-to",
|
|
232
|
+
value: replyTo,
|
|
233
|
+
onChange: (event) => setReplyTo(event.target.value),
|
|
234
|
+
placeholder: "Optional"
|
|
235
|
+
}
|
|
236
|
+
)
|
|
237
|
+
] })
|
|
238
|
+
] }),
|
|
239
|
+
sources.length > 0 && /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-3", children: [
|
|
240
|
+
/* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Credential" }),
|
|
241
|
+
/* @__PURE__ */ jsxs(
|
|
242
|
+
RadioGroup,
|
|
243
|
+
{
|
|
244
|
+
value: source,
|
|
245
|
+
onValueChange: (value) => setSource(value),
|
|
246
|
+
children: [
|
|
247
|
+
sources.includes("environment") && /* @__PURE__ */ jsx(
|
|
248
|
+
RadioGroup.ChoiceBox,
|
|
249
|
+
{
|
|
250
|
+
value: "environment",
|
|
251
|
+
label: "Environment variable",
|
|
252
|
+
description: "Read from the server at send time."
|
|
253
|
+
}
|
|
254
|
+
),
|
|
255
|
+
sources.includes("encrypted_database") && /* @__PURE__ */ jsx(
|
|
256
|
+
RadioGroup.ChoiceBox,
|
|
257
|
+
{
|
|
258
|
+
value: "encrypted_database",
|
|
259
|
+
label: "Stored API key",
|
|
260
|
+
description: encryptionAvailable ? "Encrypted with AES-256-GCM." : "Needs MEDUSA_EMAIL_ENCRYPTION_KEY on the server.",
|
|
261
|
+
disabled: !encryptionAvailable
|
|
262
|
+
}
|
|
263
|
+
)
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
),
|
|
267
|
+
source === "environment" ? /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
268
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "env-var-name", children: "Variable name" }),
|
|
269
|
+
/* @__PURE__ */ jsx(
|
|
270
|
+
Input,
|
|
271
|
+
{
|
|
272
|
+
id: "env-var-name",
|
|
273
|
+
value: envVarName,
|
|
274
|
+
onChange: (event) => setEnvVarName(event.target.value),
|
|
275
|
+
placeholder: "RESEND_API_KEY"
|
|
276
|
+
}
|
|
277
|
+
)
|
|
278
|
+
] }) : /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
279
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "api-key", children: "API key" }),
|
|
280
|
+
/* @__PURE__ */ jsx(
|
|
281
|
+
Input,
|
|
282
|
+
{
|
|
283
|
+
id: "api-key",
|
|
284
|
+
type: "password",
|
|
285
|
+
value: apiKey,
|
|
286
|
+
onChange: (event) => setApiKey(event.target.value),
|
|
287
|
+
placeholder: "Configured — leave blank to keep"
|
|
288
|
+
}
|
|
289
|
+
),
|
|
290
|
+
/* @__PURE__ */ jsx(Text, { size: "xsmall", className: "text-ui-fg-subtle", children: "Stored encrypted. The admin API never returns the key." })
|
|
291
|
+
] })
|
|
292
|
+
] })
|
|
293
|
+
] }),
|
|
294
|
+
/* @__PURE__ */ jsxs(Drawer.Footer, { children: [
|
|
295
|
+
/* @__PURE__ */ jsx(Drawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
|
|
296
|
+
!isCloud && /* @__PURE__ */ jsx(
|
|
297
|
+
Button,
|
|
298
|
+
{
|
|
299
|
+
size: "small",
|
|
300
|
+
disabled: saveProvider.isPending,
|
|
301
|
+
onClick: save,
|
|
302
|
+
children: transport.selected ? "Save" : "Save & activate"
|
|
303
|
+
}
|
|
304
|
+
)
|
|
305
|
+
] })
|
|
306
|
+
] }) });
|
|
307
|
+
};
|
|
308
|
+
const isConfigured = (transport) => transport.credential !== null || transport.credential_sources.includes("managed");
|
|
309
|
+
const statusFor = (transport) => {
|
|
310
|
+
if (!isConfigured(transport)) {
|
|
311
|
+
return { color: "orange", label: "Not configured" };
|
|
312
|
+
}
|
|
313
|
+
if (!transport.available) {
|
|
314
|
+
return transport.selected ? { color: "red", label: "Cannot send" } : { color: "grey", label: "Unavailable" };
|
|
315
|
+
}
|
|
316
|
+
return transport.selected ? { color: "green", label: "Active" } : { color: "blue", label: "Available" };
|
|
317
|
+
};
|
|
318
|
+
const fromAddress = (transport) => typeof transport.config.from_address === "string" ? transport.config.from_address : "-";
|
|
319
|
+
const credentialSummary = (transport) => {
|
|
320
|
+
const credential = transport.credential;
|
|
321
|
+
if (!credential) {
|
|
322
|
+
return "-";
|
|
323
|
+
}
|
|
324
|
+
switch (credential.source) {
|
|
325
|
+
case "environment":
|
|
326
|
+
return credential.env_var_name ?? "Environment variable";
|
|
327
|
+
case "encrypted_database":
|
|
328
|
+
return "Stored API key";
|
|
329
|
+
case "managed":
|
|
330
|
+
return "Managed by Medusa";
|
|
331
|
+
default:
|
|
332
|
+
return "-";
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
const columnHelper$1 = createDataTableColumnHelper();
|
|
336
|
+
const SectionHeading$1 = () => /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
337
|
+
/* @__PURE__ */ jsx(Heading, { level: "h2", children: "Provider" }),
|
|
338
|
+
/* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "One provider sends every email. The others keep their settings, so switching back takes a single click." })
|
|
339
|
+
] });
|
|
340
|
+
const ProvidersSection = () => {
|
|
341
|
+
const { data: provider, isPending, isError, error } = useEmailProvider();
|
|
342
|
+
const saveProvider = useSaveEmailProvider();
|
|
343
|
+
const [editingId, setEditingId] = useState(null);
|
|
344
|
+
const [testingId, setTestingId] = useState(null);
|
|
345
|
+
const transports = useMemo(
|
|
346
|
+
() => provider?.transports ?? [],
|
|
347
|
+
[provider?.transports]
|
|
348
|
+
);
|
|
349
|
+
const editing = transports.find((it) => it.id === editingId) ?? null;
|
|
350
|
+
const testing = transports.find((it) => it.id === testingId) ?? null;
|
|
351
|
+
const columns = useMemo(
|
|
352
|
+
() => [
|
|
353
|
+
columnHelper$1.display({
|
|
354
|
+
id: "transport",
|
|
355
|
+
header: "Provider",
|
|
356
|
+
cell: ({ row }) => /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
|
|
357
|
+
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", weight: "plus", children: row.original.label }),
|
|
358
|
+
!row.original.available && row.original.unavailable_reason && /* @__PURE__ */ jsx(
|
|
359
|
+
Text,
|
|
360
|
+
{
|
|
361
|
+
size: "xsmall",
|
|
362
|
+
leading: "compact",
|
|
363
|
+
className: "text-ui-fg-subtle",
|
|
364
|
+
children: row.original.unavailable_reason
|
|
365
|
+
}
|
|
366
|
+
)
|
|
367
|
+
] })
|
|
368
|
+
}),
|
|
369
|
+
columnHelper$1.display({
|
|
370
|
+
id: "status",
|
|
371
|
+
header: "Status",
|
|
372
|
+
cell: ({ row }) => {
|
|
373
|
+
const status = statusFor(row.original);
|
|
374
|
+
return /* @__PURE__ */ jsx(StatusBadge, { color: status.color, children: status.label });
|
|
375
|
+
}
|
|
376
|
+
}),
|
|
377
|
+
columnHelper$1.accessor((row) => fromAddress(row), {
|
|
378
|
+
id: "from",
|
|
379
|
+
header: "Sends from"
|
|
380
|
+
}),
|
|
381
|
+
columnHelper$1.accessor((row) => credentialSummary(row), {
|
|
382
|
+
id: "credential",
|
|
383
|
+
header: "Credential"
|
|
384
|
+
}),
|
|
385
|
+
columnHelper$1.action({
|
|
386
|
+
actions: (ctx) => {
|
|
387
|
+
const transport = ctx.row.original;
|
|
388
|
+
const actions = [];
|
|
389
|
+
if (!transport.selected && transport.available) {
|
|
390
|
+
actions.push({
|
|
391
|
+
label: "Use",
|
|
392
|
+
icon: /* @__PURE__ */ jsx(Check, {}),
|
|
393
|
+
onClick: () => saveProvider.mutate(
|
|
394
|
+
{
|
|
395
|
+
transport_id: transport.id,
|
|
396
|
+
config: transport.config
|
|
397
|
+
},
|
|
398
|
+
{
|
|
399
|
+
onSuccess: () => toast.success(`${transport.label} now sends email.`),
|
|
400
|
+
onError: (mutationError) => toast.error(getApiErrorMessage(mutationError))
|
|
401
|
+
}
|
|
402
|
+
)
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
actions.push({
|
|
406
|
+
label: isConfigured(transport) ? "Edit" : "Configure",
|
|
407
|
+
icon: /* @__PURE__ */ jsx(PencilSquare, {}),
|
|
408
|
+
onClick: () => setEditingId(transport.id)
|
|
409
|
+
});
|
|
410
|
+
if (isConfigured(transport) && transport.available) {
|
|
411
|
+
actions.push({
|
|
412
|
+
label: "Send test",
|
|
413
|
+
icon: /* @__PURE__ */ jsx(PaperPlane, {}),
|
|
414
|
+
onClick: () => setTestingId(transport.id)
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
return actions;
|
|
418
|
+
}
|
|
419
|
+
})
|
|
420
|
+
],
|
|
421
|
+
[saveProvider]
|
|
422
|
+
);
|
|
423
|
+
const table = useDataTable({
|
|
424
|
+
data: transports,
|
|
425
|
+
columns,
|
|
426
|
+
getRowId: (transport) => transport.id,
|
|
427
|
+
isLoading: isPending
|
|
428
|
+
});
|
|
429
|
+
if (isError) {
|
|
430
|
+
return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", children: [
|
|
431
|
+
/* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1 px-6 py-4", children: /* @__PURE__ */ jsx(SectionHeading$1, {}) }),
|
|
432
|
+
/* @__PURE__ */ jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsx(Alert, { variant: "error", children: getApiErrorMessage(error) }) })
|
|
433
|
+
] });
|
|
434
|
+
}
|
|
435
|
+
return /* @__PURE__ */ jsxs(Container, { className: "divide-y overflow-hidden p-0", children: [
|
|
436
|
+
/* @__PURE__ */ jsxs(DataTable, { instance: table, children: [
|
|
437
|
+
/* @__PURE__ */ jsx(DataTable.Toolbar, { className: "flex-col items-start gap-y-1", children: /* @__PURE__ */ jsx(SectionHeading$1, {}) }),
|
|
438
|
+
/* @__PURE__ */ jsx(DataTable.Table, {})
|
|
439
|
+
] }),
|
|
440
|
+
editing && /* @__PURE__ */ jsx(
|
|
441
|
+
TransportDrawer,
|
|
442
|
+
{
|
|
443
|
+
transport: editing,
|
|
444
|
+
encryptionAvailable: provider?.encryption_available ?? false,
|
|
445
|
+
onClose: () => setEditingId(null)
|
|
446
|
+
},
|
|
447
|
+
editing.id
|
|
448
|
+
),
|
|
449
|
+
testing && /* @__PURE__ */ jsx(
|
|
450
|
+
TestEmailDrawer,
|
|
451
|
+
{
|
|
452
|
+
transport: testing,
|
|
453
|
+
onClose: () => setTestingId(null)
|
|
454
|
+
},
|
|
455
|
+
`test-${testing.id}`
|
|
456
|
+
)
|
|
457
|
+
] });
|
|
458
|
+
};
|
|
459
|
+
const emailTemplatesQueryKey = ["email", "templates"];
|
|
460
|
+
const useEmailTemplates = () => useQuery({
|
|
461
|
+
queryKey: emailTemplatesQueryKey,
|
|
462
|
+
queryFn: () => sdk.client.fetch("/admin/email/templates")
|
|
463
|
+
});
|
|
464
|
+
const emailTemplateQueryKey = (templateKey) => ["email", "templates", templateKey];
|
|
465
|
+
const templatePath = (templateKey) => `/admin/email/templates/${encodeURIComponent(templateKey)}`;
|
|
466
|
+
const useEmailTemplate = (templateKey) => useQuery({
|
|
467
|
+
queryKey: emailTemplateQueryKey(templateKey),
|
|
468
|
+
queryFn: () => sdk.client.fetch(
|
|
469
|
+
templatePath(templateKey)
|
|
470
|
+
),
|
|
471
|
+
select: (data) => data.template,
|
|
472
|
+
enabled: Boolean(templateKey)
|
|
473
|
+
});
|
|
474
|
+
const useSaveEmailTemplate = (templateKey) => {
|
|
475
|
+
const queryClient = useQueryClient();
|
|
476
|
+
return useMutation({
|
|
477
|
+
mutationFn: (payload) => sdk.client.fetch(
|
|
478
|
+
templatePath(templateKey),
|
|
479
|
+
{ method: "POST", body: payload }
|
|
480
|
+
),
|
|
481
|
+
onSuccess: (data) => {
|
|
482
|
+
queryClient.setQueryData(emailTemplateQueryKey(templateKey), data);
|
|
483
|
+
queryClient.invalidateQueries({
|
|
484
|
+
queryKey: emailTemplateQueryKey(templateKey)
|
|
485
|
+
});
|
|
486
|
+
queryClient.invalidateQueries({ queryKey: emailTemplatesQueryKey });
|
|
487
|
+
}
|
|
488
|
+
});
|
|
489
|
+
};
|
|
490
|
+
const useToggleEmailTemplate = () => {
|
|
491
|
+
const queryClient = useQueryClient();
|
|
492
|
+
return useMutation({
|
|
493
|
+
mutationFn: (variables) => sdk.client.fetch(
|
|
494
|
+
templatePath(variables.template_key),
|
|
495
|
+
{ method: "POST", body: { enabled: variables.enabled } }
|
|
496
|
+
),
|
|
497
|
+
onSuccess: (data, variables) => {
|
|
498
|
+
queryClient.setQueryData(
|
|
499
|
+
emailTemplateQueryKey(variables.template_key),
|
|
500
|
+
data
|
|
501
|
+
);
|
|
502
|
+
queryClient.invalidateQueries({ queryKey: emailTemplatesQueryKey });
|
|
503
|
+
}
|
|
504
|
+
});
|
|
505
|
+
};
|
|
506
|
+
const useResetEmailTemplate = () => {
|
|
507
|
+
const queryClient = useQueryClient();
|
|
508
|
+
return useMutation({
|
|
509
|
+
mutationFn: (variables) => sdk.client.fetch(
|
|
510
|
+
templatePath(variables.template_key),
|
|
511
|
+
{ method: "DELETE" }
|
|
512
|
+
),
|
|
513
|
+
onSuccess: (data, variables) => {
|
|
514
|
+
queryClient.setQueryData(
|
|
515
|
+
emailTemplateQueryKey(variables.template_key),
|
|
516
|
+
data
|
|
517
|
+
);
|
|
518
|
+
queryClient.invalidateQueries({ queryKey: emailTemplatesQueryKey });
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
};
|
|
522
|
+
const usePreviewEmailTemplate = (templateKey) => useMutation({
|
|
523
|
+
mutationFn: (variables = {}) => sdk.client.fetch(
|
|
524
|
+
`${templatePath(templateKey)}/preview`,
|
|
525
|
+
{ method: "POST", body: variables }
|
|
526
|
+
)
|
|
527
|
+
});
|
|
528
|
+
const useSendTemplateTestEmail = (templateKey) => useMutation({
|
|
529
|
+
mutationFn: (variables) => sdk.client.fetch(
|
|
530
|
+
`${templatePath(templateKey)}/send-test`,
|
|
531
|
+
{ method: "POST", body: variables }
|
|
532
|
+
)
|
|
533
|
+
});
|
|
534
|
+
const WARNINGS = {
|
|
535
|
+
storefront_url_not_configured: {
|
|
536
|
+
text: "No storefront URL is configured, so the link in this email cannot be built. It will not send until that is set.",
|
|
537
|
+
blocksSending: true
|
|
538
|
+
},
|
|
539
|
+
order_link_unavailable: {
|
|
540
|
+
text: "No storefront URL is configured, so the link to the order cannot be built. The email still sends, without it.",
|
|
541
|
+
blocksSending: false
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
const describeWarning = (code) => WARNINGS[code] ?? { text: code, blocksSending: false };
|
|
545
|
+
const TemplatePreviewDrawer = ({
|
|
546
|
+
template,
|
|
547
|
+
onClose,
|
|
548
|
+
getDraft
|
|
549
|
+
}) => {
|
|
550
|
+
const preview = usePreviewEmailTemplate(template.template_key);
|
|
551
|
+
const { mutate } = preview;
|
|
552
|
+
const [draftError, setDraftError] = useState(null);
|
|
553
|
+
useEffect(() => {
|
|
554
|
+
if (!getDraft) {
|
|
555
|
+
mutate({});
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
let active = true;
|
|
559
|
+
getDraft().then((draft) => {
|
|
560
|
+
if (active) {
|
|
561
|
+
mutate({ draft });
|
|
562
|
+
}
|
|
563
|
+
}).catch((error) => {
|
|
564
|
+
if (active) {
|
|
565
|
+
setDraftError(getApiErrorMessage(error));
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
return () => {
|
|
569
|
+
active = false;
|
|
570
|
+
};
|
|
571
|
+
}, [getDraft, mutate]);
|
|
572
|
+
const rendered = preview.data?.preview;
|
|
573
|
+
return /* @__PURE__ */ jsx(Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxs(Drawer.Content, { children: [
|
|
574
|
+
/* @__PURE__ */ jsxs(Drawer.Header, { children: [
|
|
575
|
+
/* @__PURE__ */ jsx(Drawer.Title, { className: "font-medium", children: template.label }),
|
|
576
|
+
/* @__PURE__ */ jsx(Drawer.Description, { children: "Rendered with the example values from each variable. Nothing is sent." })
|
|
577
|
+
] }),
|
|
578
|
+
/* @__PURE__ */ jsxs(Drawer.Body, { className: "flex min-h-0 flex-1 flex-col gap-y-4 overflow-hidden", children: [
|
|
579
|
+
!rendered && !draftError && !preview.isError && /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-3", children: [
|
|
580
|
+
/* @__PURE__ */ jsx(Skeleton, { className: "h-5 w-2/3" }),
|
|
581
|
+
/* @__PURE__ */ jsx(Skeleton, { className: "h-64 w-full" })
|
|
582
|
+
] }),
|
|
583
|
+
draftError && /* @__PURE__ */ jsx(Alert, { variant: "error", children: draftError }),
|
|
584
|
+
preview.isError && /* @__PURE__ */ jsx(Alert, { variant: "error", children: getApiErrorMessage(preview.error) }),
|
|
585
|
+
rendered && /* @__PURE__ */ jsxs("div", { className: "flex min-h-0 flex-1 flex-col gap-y-4", children: [
|
|
586
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-1", children: [
|
|
587
|
+
/* @__PURE__ */ jsx(Text, { size: "xsmall", className: "text-ui-fg-subtle", children: "Subject" }),
|
|
588
|
+
/* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: rendered.subject })
|
|
589
|
+
] }),
|
|
590
|
+
/* @__PURE__ */ jsxs(
|
|
591
|
+
Tabs,
|
|
592
|
+
{
|
|
593
|
+
defaultValue: "html",
|
|
594
|
+
className: "flex min-h-0 flex-1 flex-col",
|
|
595
|
+
children: [
|
|
596
|
+
/* @__PURE__ */ jsxs(Tabs.List, { children: [
|
|
597
|
+
/* @__PURE__ */ jsx(Tabs.Trigger, { value: "html", children: "HTML" }),
|
|
598
|
+
/* @__PURE__ */ jsx(Tabs.Trigger, { value: "text", children: "Text" })
|
|
599
|
+
] }),
|
|
600
|
+
/* @__PURE__ */ jsx(Tabs.Content, { value: "html", className: "min-h-0 flex-1 pt-4", children: /* @__PURE__ */ jsx(
|
|
601
|
+
"iframe",
|
|
602
|
+
{
|
|
603
|
+
title: `${template.label} preview`,
|
|
604
|
+
srcDoc: rendered.html,
|
|
605
|
+
sandbox: "",
|
|
606
|
+
className: "bg-ui-bg-base border-ui-border-base h-full w-full rounded-lg border"
|
|
607
|
+
}
|
|
608
|
+
) }),
|
|
609
|
+
/* @__PURE__ */ jsx(Tabs.Content, { value: "text", className: "min-h-0 flex-1 pt-4", children: /* @__PURE__ */ jsx("div", { className: "bg-ui-bg-base border-ui-border-base h-full w-full overflow-auto rounded-lg border p-4", children: /* @__PURE__ */ jsx("pre", { className: "txt-compact-small text-ui-fg-base whitespace-pre-wrap", children: rendered.text }) }) })
|
|
610
|
+
]
|
|
611
|
+
}
|
|
612
|
+
)
|
|
613
|
+
] })
|
|
614
|
+
] }),
|
|
615
|
+
/* @__PURE__ */ jsx(Drawer.Footer, { children: /* @__PURE__ */ jsx(Drawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Close" }) }) })
|
|
616
|
+
] }) });
|
|
617
|
+
};
|
|
618
|
+
const TemplateTestEmailDrawer = ({
|
|
619
|
+
template,
|
|
620
|
+
onClose,
|
|
621
|
+
getDraft
|
|
622
|
+
}) => {
|
|
623
|
+
const sendTest = useSendTemplateTestEmail(template.template_key);
|
|
624
|
+
const [recipient, setRecipient] = useState("");
|
|
625
|
+
const send = async () => {
|
|
626
|
+
let draft;
|
|
627
|
+
if (getDraft) {
|
|
628
|
+
try {
|
|
629
|
+
draft = await getDraft();
|
|
630
|
+
} catch (error) {
|
|
631
|
+
toast.error(getApiErrorMessage(error));
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
sendTest.mutate(
|
|
636
|
+
{ to: recipient, ...draft ? { draft } : {} },
|
|
637
|
+
{
|
|
638
|
+
onSuccess: () => {
|
|
639
|
+
toast.success(`Test email sent to ${recipient}.`);
|
|
640
|
+
onClose();
|
|
641
|
+
},
|
|
642
|
+
onError: (error) => toast.error(getApiErrorMessage(error))
|
|
643
|
+
}
|
|
644
|
+
);
|
|
645
|
+
};
|
|
646
|
+
return /* @__PURE__ */ jsx(Drawer, { open: true, onOpenChange: (open) => !open && onClose(), children: /* @__PURE__ */ jsxs(Drawer.Content, { children: [
|
|
647
|
+
/* @__PURE__ */ jsxs(Drawer.Header, { children: [
|
|
648
|
+
/* @__PURE__ */ jsx(Drawer.Title, { className: "font-medium", children: "Send test email" }),
|
|
649
|
+
/* @__PURE__ */ jsxs(Drawer.Description, { children: [
|
|
650
|
+
"Sends ",
|
|
651
|
+
template.label,
|
|
652
|
+
", rendered with example values."
|
|
653
|
+
] })
|
|
654
|
+
] }),
|
|
655
|
+
/* @__PURE__ */ jsx(Drawer.Body, { className: "flex flex-col gap-y-4", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2", children: [
|
|
656
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "template-test-recipient", children: "Recipient" }),
|
|
657
|
+
/* @__PURE__ */ jsx(
|
|
658
|
+
Input,
|
|
659
|
+
{
|
|
660
|
+
id: "template-test-recipient",
|
|
661
|
+
type: "email",
|
|
662
|
+
value: recipient,
|
|
663
|
+
onChange: (event) => setRecipient(event.target.value),
|
|
664
|
+
placeholder: "you@example.com"
|
|
665
|
+
}
|
|
666
|
+
),
|
|
667
|
+
/* @__PURE__ */ jsx(Text, { size: "xsmall", className: "text-ui-fg-subtle", children: "Goes out through the active email provider, and works even while this template is disabled — which is how you check it before turning it on." })
|
|
668
|
+
] }) }),
|
|
669
|
+
/* @__PURE__ */ jsxs(Drawer.Footer, { children: [
|
|
670
|
+
/* @__PURE__ */ jsx(Drawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
|
|
671
|
+
/* @__PURE__ */ jsx(
|
|
672
|
+
Button,
|
|
673
|
+
{
|
|
674
|
+
size: "small",
|
|
675
|
+
disabled: !recipient || sendTest.isPending,
|
|
676
|
+
onClick: send,
|
|
677
|
+
children: "Send"
|
|
678
|
+
}
|
|
679
|
+
)
|
|
680
|
+
] })
|
|
681
|
+
] }) });
|
|
682
|
+
};
|
|
683
|
+
const firstWarning = (template) => {
|
|
684
|
+
const [code] = template.warnings;
|
|
685
|
+
return code ? describeWarning(code) : null;
|
|
686
|
+
};
|
|
687
|
+
const lastEdited = (template) => {
|
|
688
|
+
if (!template.updated_at) {
|
|
689
|
+
return "Never";
|
|
690
|
+
}
|
|
691
|
+
const edited = new Date(template.updated_at);
|
|
692
|
+
if (Number.isNaN(edited.getTime())) {
|
|
693
|
+
return "Never";
|
|
694
|
+
}
|
|
695
|
+
return edited.toLocaleDateString(void 0, {
|
|
696
|
+
year: "numeric",
|
|
697
|
+
month: "short",
|
|
698
|
+
day: "numeric"
|
|
699
|
+
});
|
|
700
|
+
};
|
|
701
|
+
const columnHelper = createDataTableColumnHelper();
|
|
702
|
+
const SectionHeading = () => /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
703
|
+
/* @__PURE__ */ jsx(Heading, { level: "h2", children: "Templates" }),
|
|
704
|
+
/* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "One template per registered event. Disabled templates are skipped at send time, so nothing goes out until you turn it on." })
|
|
705
|
+
] });
|
|
706
|
+
const editorPath = (templateKey) => `/settings/email/templates/${encodeURIComponent(templateKey)}`;
|
|
707
|
+
const isInteractive = (target) => target instanceof Element && Boolean(
|
|
708
|
+
target.closest(
|
|
709
|
+
'button, a, input, [role="switch"], [role="menu"], [role="menuitem"]'
|
|
710
|
+
)
|
|
711
|
+
);
|
|
712
|
+
const TemplatesSection = () => {
|
|
713
|
+
const { data, isPending, isError, error } = useEmailTemplates();
|
|
714
|
+
const toggleTemplate = useToggleEmailTemplate();
|
|
715
|
+
const resetTemplate = useResetEmailTemplate();
|
|
716
|
+
const navigate = useNavigate();
|
|
717
|
+
const prompt = usePrompt();
|
|
718
|
+
const [previewKey, setPreviewKey] = useState(null);
|
|
719
|
+
const [testKey, setTestKey] = useState(null);
|
|
720
|
+
const templates = useMemo(() => data?.templates ?? [], [data?.templates]);
|
|
721
|
+
const previewing = templates.find((it) => it.template_key === previewKey) ?? null;
|
|
722
|
+
const testing = templates.find((it) => it.template_key === testKey) ?? null;
|
|
723
|
+
const openTemplate = useCallback(
|
|
724
|
+
(event, row) => {
|
|
725
|
+
if (isInteractive(event.target)) {
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
const template = row.original ?? row;
|
|
729
|
+
navigate(editorPath(template.template_key));
|
|
730
|
+
},
|
|
731
|
+
[navigate]
|
|
732
|
+
);
|
|
733
|
+
const confirmReset = useCallback(
|
|
734
|
+
async (template) => {
|
|
735
|
+
const confirmed = await prompt({
|
|
736
|
+
title: `Reset ${template.label}?`,
|
|
737
|
+
description: "The saved subject and body are discarded, and this event goes back to the built-in template. This cannot be undone.",
|
|
738
|
+
variant: "danger",
|
|
739
|
+
confirmText: "Reset"
|
|
740
|
+
});
|
|
741
|
+
if (!confirmed) {
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
resetTemplate.mutate(
|
|
745
|
+
{ template_key: template.template_key },
|
|
746
|
+
{
|
|
747
|
+
onSuccess: () => toast.success(`${template.label} is back to the default.`),
|
|
748
|
+
onError: (error2) => toast.error(getApiErrorMessage(error2))
|
|
749
|
+
}
|
|
750
|
+
);
|
|
751
|
+
},
|
|
752
|
+
[prompt, resetTemplate]
|
|
753
|
+
);
|
|
754
|
+
const columns = useMemo(
|
|
755
|
+
() => [
|
|
756
|
+
columnHelper.display({
|
|
757
|
+
id: "template",
|
|
758
|
+
header: "Template",
|
|
759
|
+
cell: ({ row }) => /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
|
|
760
|
+
/* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", weight: "plus", children: row.original.label }),
|
|
761
|
+
/* @__PURE__ */ jsx(
|
|
762
|
+
Text,
|
|
763
|
+
{
|
|
764
|
+
size: "xsmall",
|
|
765
|
+
leading: "compact",
|
|
766
|
+
className: "text-ui-fg-subtle font-mono",
|
|
767
|
+
children: row.original.event
|
|
768
|
+
}
|
|
769
|
+
)
|
|
770
|
+
] })
|
|
771
|
+
}),
|
|
772
|
+
columnHelper.display({
|
|
773
|
+
id: "status",
|
|
774
|
+
header: "Status",
|
|
775
|
+
cell: ({ row }) => {
|
|
776
|
+
const template = row.original;
|
|
777
|
+
const warning = firstWarning(template);
|
|
778
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
|
|
779
|
+
/* @__PURE__ */ jsx(
|
|
780
|
+
Switch,
|
|
781
|
+
{
|
|
782
|
+
checked: template.enabled,
|
|
783
|
+
disabled: toggleTemplate.isPending,
|
|
784
|
+
"aria-label": `Enable ${template.label}`,
|
|
785
|
+
onCheckedChange: (enabled) => toggleTemplate.mutate(
|
|
786
|
+
{ template_key: template.template_key, enabled },
|
|
787
|
+
{
|
|
788
|
+
onSuccess: () => toast.success(
|
|
789
|
+
enabled ? `${template.label} is now sending.` : `${template.label} is disabled.`
|
|
790
|
+
),
|
|
791
|
+
onError: (mutationError) => toast.error(getApiErrorMessage(mutationError))
|
|
792
|
+
}
|
|
793
|
+
)
|
|
794
|
+
}
|
|
795
|
+
),
|
|
796
|
+
warning && /* @__PURE__ */ jsx(Tooltip, { content: warning.text, children: /* @__PURE__ */ jsx(
|
|
797
|
+
"span",
|
|
798
|
+
{
|
|
799
|
+
className: "text-ui-tag-orange-text flex",
|
|
800
|
+
"aria-label": warning.blocksSending ? `${template.label} cannot send` : `${template.label} has a warning`,
|
|
801
|
+
children: /* @__PURE__ */ jsx(ExclamationCircle, {})
|
|
802
|
+
}
|
|
803
|
+
) })
|
|
804
|
+
] });
|
|
805
|
+
}
|
|
806
|
+
}),
|
|
807
|
+
columnHelper.accessor(
|
|
808
|
+
(row) => row.is_customized ? "Custom" : "Default",
|
|
809
|
+
{ id: "source", header: "Source" }
|
|
810
|
+
),
|
|
811
|
+
columnHelper.accessor((row) => lastEdited(row), {
|
|
812
|
+
id: "updated",
|
|
813
|
+
header: "Last edited"
|
|
814
|
+
}),
|
|
815
|
+
columnHelper.action({
|
|
816
|
+
actions: (ctx) => {
|
|
817
|
+
const template = ctx.row.original;
|
|
818
|
+
const edit = [
|
|
819
|
+
{
|
|
820
|
+
label: "Edit",
|
|
821
|
+
icon: /* @__PURE__ */ jsx(PencilSquare, {}),
|
|
822
|
+
onClick: () => navigate(editorPath(template.template_key))
|
|
823
|
+
},
|
|
824
|
+
{
|
|
825
|
+
label: "Preview",
|
|
826
|
+
icon: /* @__PURE__ */ jsx(Eye, {}),
|
|
827
|
+
onClick: () => setPreviewKey(template.template_key)
|
|
828
|
+
},
|
|
829
|
+
{
|
|
830
|
+
label: "Send test",
|
|
831
|
+
icon: /* @__PURE__ */ jsx(PaperPlane, {}),
|
|
832
|
+
onClick: () => setTestKey(template.template_key)
|
|
833
|
+
}
|
|
834
|
+
];
|
|
835
|
+
return template.is_customized ? [
|
|
836
|
+
edit,
|
|
837
|
+
[
|
|
838
|
+
{
|
|
839
|
+
label: "Reset to default",
|
|
840
|
+
icon: /* @__PURE__ */ jsx(ArrowPath, {}),
|
|
841
|
+
onClick: () => confirmReset(template)
|
|
842
|
+
}
|
|
843
|
+
]
|
|
844
|
+
] : [edit];
|
|
845
|
+
}
|
|
846
|
+
})
|
|
847
|
+
],
|
|
848
|
+
[confirmReset, navigate, toggleTemplate]
|
|
849
|
+
);
|
|
850
|
+
const table = useDataTable({
|
|
851
|
+
data: templates,
|
|
852
|
+
columns,
|
|
853
|
+
getRowId: (template) => template.template_key,
|
|
854
|
+
isLoading: isPending,
|
|
855
|
+
onRowClick: openTemplate
|
|
856
|
+
});
|
|
857
|
+
if (isError) {
|
|
858
|
+
return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", children: [
|
|
859
|
+
/* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1 px-6 py-4", children: /* @__PURE__ */ jsx(SectionHeading, {}) }),
|
|
860
|
+
/* @__PURE__ */ jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsx(Alert, { variant: "error", children: getApiErrorMessage(error) }) })
|
|
861
|
+
] });
|
|
862
|
+
}
|
|
863
|
+
return /* @__PURE__ */ jsxs(Container, { className: "divide-y overflow-hidden p-0", children: [
|
|
864
|
+
/* @__PURE__ */ jsxs(DataTable, { instance: table, children: [
|
|
865
|
+
/* @__PURE__ */ jsx(DataTable.Toolbar, { className: "flex-col items-start gap-y-1", children: /* @__PURE__ */ jsx(SectionHeading, {}) }),
|
|
866
|
+
/* @__PURE__ */ jsx(DataTable.Table, {})
|
|
867
|
+
] }),
|
|
868
|
+
previewing && /* @__PURE__ */ jsx(
|
|
869
|
+
TemplatePreviewDrawer,
|
|
870
|
+
{
|
|
871
|
+
template: previewing,
|
|
872
|
+
onClose: () => setPreviewKey(null)
|
|
873
|
+
},
|
|
874
|
+
`preview-${previewing.template_key}`
|
|
875
|
+
),
|
|
876
|
+
testing && /* @__PURE__ */ jsx(
|
|
877
|
+
TemplateTestEmailDrawer,
|
|
878
|
+
{
|
|
879
|
+
template: testing,
|
|
880
|
+
onClose: () => setTestKey(null)
|
|
881
|
+
},
|
|
882
|
+
`test-${testing.template_key}`
|
|
883
|
+
)
|
|
884
|
+
] });
|
|
885
|
+
};
|
|
886
|
+
const EmailSettingsPage = () => {
|
|
887
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-3", children: [
|
|
888
|
+
/* @__PURE__ */ jsx(Container, { children: /* @__PURE__ */ jsxs("div", { children: [
|
|
889
|
+
/* @__PURE__ */ jsx(Heading, { children: "Email" }),
|
|
890
|
+
/* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Manage transactional email templates for registered Medusa events and choose the provider used to send them." })
|
|
891
|
+
] }) }),
|
|
892
|
+
/* @__PURE__ */ jsx(ProvidersSection, {}),
|
|
893
|
+
/* @__PURE__ */ jsx(TemplatesSection, {})
|
|
894
|
+
] });
|
|
895
|
+
};
|
|
896
|
+
const config = defineRouteConfig({
|
|
897
|
+
label: "Email",
|
|
898
|
+
icon: Envelope
|
|
899
|
+
});
|
|
900
|
+
const handle$1 = {
|
|
901
|
+
breadcrumb: () => "Email"
|
|
902
|
+
};
|
|
903
|
+
const VARIABLE_FORMATS = [
|
|
904
|
+
"text",
|
|
905
|
+
"money",
|
|
906
|
+
"date_long",
|
|
907
|
+
"date_short",
|
|
908
|
+
"date_time"
|
|
909
|
+
];
|
|
910
|
+
const VARIABLE_KEY_PATTERN = /^[a-z0-9_]+(\.[a-z0-9_]+)*$/;
|
|
911
|
+
const isVariableFormat = (value) => typeof value === "string" && VARIABLE_FORMATS.includes(value);
|
|
912
|
+
const assertIdentifier = (value, label) => {
|
|
913
|
+
if (!VARIABLE_KEY_PATTERN.test(value)) {
|
|
914
|
+
throw new Error(`Invalid email template ${label}: "${value}"`);
|
|
915
|
+
}
|
|
916
|
+
};
|
|
917
|
+
const toLiquidExpression = (key, format = "text", options = {}) => {
|
|
918
|
+
assertIdentifier(key, "variable key");
|
|
919
|
+
if (format === "text") {
|
|
920
|
+
return `{{ ${key} }}`;
|
|
921
|
+
}
|
|
922
|
+
if (format === "money") {
|
|
923
|
+
const { currencySource } = options;
|
|
924
|
+
if (currencySource === void 0) {
|
|
925
|
+
return `{{ ${key} | money }}`;
|
|
926
|
+
}
|
|
927
|
+
assertIdentifier(currencySource, "currency source");
|
|
928
|
+
return `{{ ${key} | money: ${currencySource} }}`;
|
|
929
|
+
}
|
|
930
|
+
return `{{ ${key} | ${format} }}`;
|
|
931
|
+
};
|
|
932
|
+
const LOOP_ITEM = "item";
|
|
933
|
+
const toLiquidLoop = (source) => {
|
|
934
|
+
assertIdentifier(source, "repeat source");
|
|
935
|
+
return {
|
|
936
|
+
open: `{% for ${LOOP_ITEM} in ${source} %}`,
|
|
937
|
+
close: "{% endfor %}"
|
|
938
|
+
};
|
|
939
|
+
};
|
|
940
|
+
const EXPRESSION_PATTERN = /\{\{\s*([a-z0-9_]+(?:\.[a-z0-9_]+)*)\s*(?:\|\s*([a-z0-9_]+)\s*(?::\s*([a-z0-9_]+(?:\.[a-z0-9_]+)*)\s*)?)?\}\}/g;
|
|
941
|
+
const fromLiquidExpression = (expression) => {
|
|
942
|
+
const match = new RegExp(`^${EXPRESSION_PATTERN.source}$`, "s").exec(
|
|
943
|
+
expression.trim()
|
|
944
|
+
);
|
|
945
|
+
if (!match) {
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
const [, key, filter, argument] = match;
|
|
949
|
+
if (!filter) {
|
|
950
|
+
return { key, format: "text" };
|
|
951
|
+
}
|
|
952
|
+
if (!isVariableFormat(filter) || filter === "text") {
|
|
953
|
+
return null;
|
|
954
|
+
}
|
|
955
|
+
if (filter === "money") {
|
|
956
|
+
return argument ? { key, format: filter, currencySource: argument } : { key, format: filter };
|
|
957
|
+
}
|
|
958
|
+
return argument ? null : { key, format: filter };
|
|
959
|
+
};
|
|
960
|
+
const findLiquidExpressions = (text) => {
|
|
961
|
+
const matches = [];
|
|
962
|
+
const pattern = new RegExp(EXPRESSION_PATTERN.source, "g");
|
|
963
|
+
let match;
|
|
964
|
+
while ((match = pattern.exec(text)) !== null) {
|
|
965
|
+
const parsed = fromLiquidExpression(match[0]);
|
|
966
|
+
if (parsed) {
|
|
967
|
+
matches.push({
|
|
968
|
+
...parsed,
|
|
969
|
+
source: match[0],
|
|
970
|
+
start: match.index,
|
|
971
|
+
end: match.index + match[0].length
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
return matches;
|
|
976
|
+
};
|
|
977
|
+
const scalarVariables = (variables) => variables.filter((variable) => variable.type !== "list");
|
|
978
|
+
const listVariables = (variables) => variables.filter((variable) => variable.type === "list");
|
|
979
|
+
const itemVariables = (list) => (list.fields ?? []).map((field) => ({
|
|
980
|
+
...field,
|
|
981
|
+
key: `${LOOP_ITEM}.${field.key}`,
|
|
982
|
+
group: list.label
|
|
983
|
+
}));
|
|
984
|
+
const variablesAt = (doc, pos, variables) => {
|
|
985
|
+
const scalars = scalarVariables(variables);
|
|
986
|
+
if (typeof pos !== "number" || pos < 0 || pos > doc.content.size) {
|
|
987
|
+
return scalars;
|
|
988
|
+
}
|
|
989
|
+
const $pos = doc.resolve(pos);
|
|
990
|
+
for (let depth = $pos.depth; depth > 0; depth--) {
|
|
991
|
+
const node = $pos.node(depth);
|
|
992
|
+
if (node.type.name === "repeat") {
|
|
993
|
+
const list = listVariables(variables).find(
|
|
994
|
+
(entry) => entry.key === node.attrs.source
|
|
995
|
+
);
|
|
996
|
+
return list ? [...scalars, ...itemVariables(list)] : scalars;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
return scalars;
|
|
1000
|
+
};
|
|
1001
|
+
const groupVariables = (variables) => {
|
|
1002
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1003
|
+
variables.forEach((variable) => {
|
|
1004
|
+
const group = variable.group ?? "Other";
|
|
1005
|
+
groups.set(group, [...groups.get(group) ?? [], variable]);
|
|
1006
|
+
});
|
|
1007
|
+
return [...groups.entries()];
|
|
1008
|
+
};
|
|
1009
|
+
const urlVariables = (variables) => variables.filter((variable) => variable.type === "url");
|
|
1010
|
+
const FORMAT_LABELS = {
|
|
1011
|
+
text: "Text",
|
|
1012
|
+
money: "Money",
|
|
1013
|
+
date_long: "Long date",
|
|
1014
|
+
date_short: "Short date",
|
|
1015
|
+
date_time: "Date and time"
|
|
1016
|
+
};
|
|
1017
|
+
const formatsFor = (variable) => {
|
|
1018
|
+
switch (variable?.type) {
|
|
1019
|
+
case "money":
|
|
1020
|
+
return ["text", "money"];
|
|
1021
|
+
case "date":
|
|
1022
|
+
return ["text", "date_long", "date_short", "date_time"];
|
|
1023
|
+
default:
|
|
1024
|
+
return ["text"];
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
const defaultFormatFor = (variable) => {
|
|
1028
|
+
switch (variable?.type) {
|
|
1029
|
+
case "money":
|
|
1030
|
+
return "money";
|
|
1031
|
+
case "date":
|
|
1032
|
+
return "date_long";
|
|
1033
|
+
default:
|
|
1034
|
+
return "text";
|
|
1035
|
+
}
|
|
1036
|
+
};
|
|
1037
|
+
const attributesFor = (variable, format) => ({
|
|
1038
|
+
key: variable?.key ?? "",
|
|
1039
|
+
format: format ?? defaultFormatFor(variable),
|
|
1040
|
+
currencySource: variable?.currencyKey ?? null
|
|
1041
|
+
});
|
|
1042
|
+
const EmailTemplateEditor = lazy(
|
|
1043
|
+
() => import("./email-template-editor-JGYZjesE")
|
|
1044
|
+
);
|
|
1045
|
+
const SubjectEditor = lazy(
|
|
1046
|
+
() => import("./subject-editor-ykrWWMtp")
|
|
1047
|
+
);
|
|
1048
|
+
const SETTINGS_PATH = "/settings/email";
|
|
1049
|
+
const CodeChip = ({ children }) => /* @__PURE__ */ jsx("code", { className: "bg-ui-bg-component text-ui-fg-subtle rounded-md px-1 py-px font-mono text-xs", children });
|
|
1050
|
+
const EmailTemplatePage = () => {
|
|
1051
|
+
const { id: templateKey = "" } = useParams();
|
|
1052
|
+
const navigate = useNavigate();
|
|
1053
|
+
const prompt = usePrompt();
|
|
1054
|
+
const {
|
|
1055
|
+
data: template,
|
|
1056
|
+
isPending,
|
|
1057
|
+
isError,
|
|
1058
|
+
error
|
|
1059
|
+
} = useEmailTemplate(templateKey);
|
|
1060
|
+
const saveTemplate = useSaveEmailTemplate(templateKey);
|
|
1061
|
+
const toggleTemplate = useToggleEmailTemplate();
|
|
1062
|
+
const subjectRef = useRef(null);
|
|
1063
|
+
const editorRef = useRef(null);
|
|
1064
|
+
const [subjectDirty, setSubjectDirty] = useState(false);
|
|
1065
|
+
const [bodyDirty, setBodyDirtyState] = useState(false);
|
|
1066
|
+
const bodyDirtyRef = useRef(false);
|
|
1067
|
+
const setBodyDirty = useCallback((dirty) => {
|
|
1068
|
+
bodyDirtyRef.current = dirty;
|
|
1069
|
+
setBodyDirtyState(dirty);
|
|
1070
|
+
}, []);
|
|
1071
|
+
const subjectEdits = useRef(0);
|
|
1072
|
+
const bodyEdits = useRef(0);
|
|
1073
|
+
const markSubjectEdited = useCallback(() => {
|
|
1074
|
+
subjectEdits.current += 1;
|
|
1075
|
+
setSubjectDirty(true);
|
|
1076
|
+
}, []);
|
|
1077
|
+
const markBodyEdited = useCallback(() => {
|
|
1078
|
+
bodyEdits.current += 1;
|
|
1079
|
+
setBodyDirty(true);
|
|
1080
|
+
}, [setBodyDirty]);
|
|
1081
|
+
const isDirty = subjectDirty || bodyDirty;
|
|
1082
|
+
const groups = useMemo(
|
|
1083
|
+
() => groupVariables(scalarVariables(template?.variables ?? [])),
|
|
1084
|
+
[template?.variables]
|
|
1085
|
+
);
|
|
1086
|
+
const [previewing, setPreviewing] = useState(false);
|
|
1087
|
+
const [testing, setTesting] = useState(false);
|
|
1088
|
+
const insertVariable = (key, format) => {
|
|
1089
|
+
const variable = template?.variables.find((entry) => entry.key === key);
|
|
1090
|
+
subjectRef.current?.insertVariable(attributesFor(variable, format));
|
|
1091
|
+
markSubjectEdited();
|
|
1092
|
+
};
|
|
1093
|
+
const getDraft = useCallback(async () => {
|
|
1094
|
+
const subject = subjectRef.current?.getText() ?? "";
|
|
1095
|
+
const body = bodyDirtyRef.current ? await editorRef.current?.getBody() : void 0;
|
|
1096
|
+
return {
|
|
1097
|
+
...subject ? { subject_template: subject } : {},
|
|
1098
|
+
...body ? { html_template: body.html_template } : {}
|
|
1099
|
+
};
|
|
1100
|
+
}, []);
|
|
1101
|
+
const save = async () => {
|
|
1102
|
+
const sentSubjectEdits = subjectEdits.current;
|
|
1103
|
+
const sentBodyEdits = bodyEdits.current;
|
|
1104
|
+
let body;
|
|
1105
|
+
if (bodyDirty) {
|
|
1106
|
+
try {
|
|
1107
|
+
body = await editorRef.current?.getBody();
|
|
1108
|
+
} catch (exportError) {
|
|
1109
|
+
toast.error(getApiErrorMessage(exportError));
|
|
1110
|
+
return;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
saveTemplate.mutate(
|
|
1114
|
+
{
|
|
1115
|
+
...subjectDirty ? { subject_template: subjectRef.current?.getText() ?? "" } : {},
|
|
1116
|
+
...body ?? {}
|
|
1117
|
+
},
|
|
1118
|
+
{
|
|
1119
|
+
onSuccess: () => {
|
|
1120
|
+
if (subjectEdits.current === sentSubjectEdits) {
|
|
1121
|
+
setSubjectDirty(false);
|
|
1122
|
+
}
|
|
1123
|
+
if (bodyEdits.current === sentBodyEdits) {
|
|
1124
|
+
setBodyDirty(false);
|
|
1125
|
+
}
|
|
1126
|
+
toast.success("Template saved.");
|
|
1127
|
+
},
|
|
1128
|
+
onError: (saveError) => toast.error(getApiErrorMessage(saveError))
|
|
1129
|
+
}
|
|
1130
|
+
);
|
|
1131
|
+
};
|
|
1132
|
+
const leave = async () => {
|
|
1133
|
+
if (isDirty) {
|
|
1134
|
+
const confirmed = await prompt({
|
|
1135
|
+
title: "Discard changes?",
|
|
1136
|
+
description: "This template has unsaved changes. Leaving now discards them.",
|
|
1137
|
+
variant: "danger",
|
|
1138
|
+
confirmText: "Discard"
|
|
1139
|
+
});
|
|
1140
|
+
if (!confirmed) {
|
|
1141
|
+
return;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
navigate(SETTINGS_PATH);
|
|
1145
|
+
};
|
|
1146
|
+
if (isPending) {
|
|
1147
|
+
return /* @__PURE__ */ jsxs(Container, { className: "divide-y p-0", children: [
|
|
1148
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2 px-6 py-4", children: [
|
|
1149
|
+
/* @__PURE__ */ jsx(Skeleton, { className: "h-6 w-48" }),
|
|
1150
|
+
/* @__PURE__ */ jsx(Skeleton, { className: "h-4 w-64" })
|
|
1151
|
+
] }),
|
|
1152
|
+
/* @__PURE__ */ jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsx(Skeleton, { className: "h-64 w-full" }) })
|
|
1153
|
+
] });
|
|
1154
|
+
}
|
|
1155
|
+
if (isError || !template) {
|
|
1156
|
+
return /* @__PURE__ */ jsx(Container, { className: "p-0", children: /* @__PURE__ */ jsx("div", { className: "px-6 py-4", children: /* @__PURE__ */ jsx(Alert, { variant: "error", children: getApiErrorMessage(error) }) }) });
|
|
1157
|
+
}
|
|
1158
|
+
const warning = template.warnings[0];
|
|
1159
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-3", children: [
|
|
1160
|
+
/* @__PURE__ */ jsxs(Container, { className: "flex flex-col gap-y-3 px-6 py-4", children: [
|
|
1161
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-start justify-between gap-4", children: [
|
|
1162
|
+
/* @__PURE__ */ jsxs("div", { className: "flex min-w-0 items-start gap-x-3", children: [
|
|
1163
|
+
/* @__PURE__ */ jsx(
|
|
1164
|
+
IconButton,
|
|
1165
|
+
{
|
|
1166
|
+
size: "small",
|
|
1167
|
+
variant: "transparent",
|
|
1168
|
+
"aria-label": "Back to email settings",
|
|
1169
|
+
onClick: leave,
|
|
1170
|
+
children: /* @__PURE__ */ jsx(ArrowLeft, {})
|
|
1171
|
+
}
|
|
1172
|
+
),
|
|
1173
|
+
/* @__PURE__ */ jsxs("div", { className: "flex min-w-0 flex-col gap-y-0.5", children: [
|
|
1174
|
+
/* @__PURE__ */ jsx(Heading, { level: "h2", children: template.label }),
|
|
1175
|
+
/* @__PURE__ */ jsx(
|
|
1176
|
+
Text,
|
|
1177
|
+
{
|
|
1178
|
+
size: "small",
|
|
1179
|
+
className: "text-ui-fg-subtle flex flex-wrap items-center gap-x-1.5",
|
|
1180
|
+
children: template.description ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1181
|
+
template.description,
|
|
1182
|
+
/* @__PURE__ */ jsx(CodeChip, { children: template.event })
|
|
1183
|
+
] }) : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1184
|
+
"Sent when ",
|
|
1185
|
+
/* @__PURE__ */ jsx(CodeChip, { children: template.event }),
|
|
1186
|
+
" fires"
|
|
1187
|
+
] })
|
|
1188
|
+
}
|
|
1189
|
+
)
|
|
1190
|
+
] })
|
|
1191
|
+
] }),
|
|
1192
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center gap-x-2 gap-y-2", children: [
|
|
1193
|
+
/* @__PURE__ */ jsx(
|
|
1194
|
+
Switch,
|
|
1195
|
+
{
|
|
1196
|
+
checked: template.enabled,
|
|
1197
|
+
disabled: toggleTemplate.isPending,
|
|
1198
|
+
"aria-label": `Enable ${template.label}`,
|
|
1199
|
+
onCheckedChange: (enabled) => toggleTemplate.mutate(
|
|
1200
|
+
{ template_key: template.template_key, enabled },
|
|
1201
|
+
{
|
|
1202
|
+
onSuccess: () => toast.success(
|
|
1203
|
+
enabled ? `${template.label} is now sending.` : `${template.label} is disabled.`
|
|
1204
|
+
),
|
|
1205
|
+
onError: (toggleError) => toast.error(getApiErrorMessage(toggleError))
|
|
1206
|
+
}
|
|
1207
|
+
)
|
|
1208
|
+
}
|
|
1209
|
+
),
|
|
1210
|
+
/* @__PURE__ */ jsxs(
|
|
1211
|
+
Button,
|
|
1212
|
+
{
|
|
1213
|
+
size: "small",
|
|
1214
|
+
variant: "secondary",
|
|
1215
|
+
onClick: () => setPreviewing(true),
|
|
1216
|
+
children: [
|
|
1217
|
+
/* @__PURE__ */ jsx(Eye, {}),
|
|
1218
|
+
"Preview"
|
|
1219
|
+
]
|
|
1220
|
+
}
|
|
1221
|
+
),
|
|
1222
|
+
/* @__PURE__ */ jsxs(
|
|
1223
|
+
Button,
|
|
1224
|
+
{
|
|
1225
|
+
size: "small",
|
|
1226
|
+
variant: "secondary",
|
|
1227
|
+
onClick: () => setTesting(true),
|
|
1228
|
+
children: [
|
|
1229
|
+
/* @__PURE__ */ jsx(PaperPlane, {}),
|
|
1230
|
+
"Send test"
|
|
1231
|
+
]
|
|
1232
|
+
}
|
|
1233
|
+
),
|
|
1234
|
+
/* @__PURE__ */ jsx(
|
|
1235
|
+
Button,
|
|
1236
|
+
{
|
|
1237
|
+
size: "small",
|
|
1238
|
+
disabled: !isDirty || saveTemplate.isPending,
|
|
1239
|
+
onClick: save,
|
|
1240
|
+
children: "Save"
|
|
1241
|
+
}
|
|
1242
|
+
)
|
|
1243
|
+
] })
|
|
1244
|
+
] }),
|
|
1245
|
+
warning && /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Alert, { variant: "warning", children: describeWarning(warning).text }) })
|
|
1246
|
+
] }),
|
|
1247
|
+
/* @__PURE__ */ jsx(Container, { className: "p-0", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-2 px-6 py-4", children: [
|
|
1248
|
+
/* @__PURE__ */ jsx(Label, { size: "small", weight: "plus", htmlFor: "subject", children: "Subject" }),
|
|
1249
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
1250
|
+
/* @__PURE__ */ jsx("div", { className: "min-w-0 flex-1", children: /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(Skeleton, { className: "h-8 w-full" }), children: /* @__PURE__ */ jsx(
|
|
1251
|
+
SubjectEditor,
|
|
1252
|
+
{
|
|
1253
|
+
ref: subjectRef,
|
|
1254
|
+
subject: template.subject_template,
|
|
1255
|
+
variables: template.variables,
|
|
1256
|
+
onDirty: markSubjectEdited
|
|
1257
|
+
}
|
|
1258
|
+
) }) }),
|
|
1259
|
+
/* @__PURE__ */ jsxs(DropdownMenu, { children: [
|
|
1260
|
+
/* @__PURE__ */ jsx(DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxs(
|
|
1261
|
+
Button,
|
|
1262
|
+
{
|
|
1263
|
+
size: "small",
|
|
1264
|
+
variant: "secondary",
|
|
1265
|
+
className: "flex items-center gap-x-2 shrink-0",
|
|
1266
|
+
"aria-label": "Insert variable",
|
|
1267
|
+
children: [
|
|
1268
|
+
/* @__PURE__ */ jsx(Plus, {}),
|
|
1269
|
+
/* @__PURE__ */ jsx("span", { children: "Insert variable" })
|
|
1270
|
+
]
|
|
1271
|
+
}
|
|
1272
|
+
) }),
|
|
1273
|
+
/* @__PURE__ */ jsx(DropdownMenu.Content, { children: groups.map(([group, variables], index) => /* @__PURE__ */ jsxs(DropdownMenu.Group, { children: [
|
|
1274
|
+
index > 0 && /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
|
|
1275
|
+
/* @__PURE__ */ jsx(DropdownMenu.Label, { children: group }),
|
|
1276
|
+
variables.map((variable) => {
|
|
1277
|
+
const formats = formatsFor(variable);
|
|
1278
|
+
if (formats.length === 1) {
|
|
1279
|
+
return /* @__PURE__ */ jsx(
|
|
1280
|
+
DropdownMenu.Item,
|
|
1281
|
+
{
|
|
1282
|
+
onClick: () => insertVariable(variable.key),
|
|
1283
|
+
children: variable.label
|
|
1284
|
+
},
|
|
1285
|
+
variable.key
|
|
1286
|
+
);
|
|
1287
|
+
}
|
|
1288
|
+
return /* @__PURE__ */ jsxs(DropdownMenu.SubMenu, { children: [
|
|
1289
|
+
/* @__PURE__ */ jsx(DropdownMenu.SubMenuTrigger, { children: variable.label }),
|
|
1290
|
+
/* @__PURE__ */ jsx(DropdownMenu.SubMenuContent, { children: formats.map((format) => /* @__PURE__ */ jsx(
|
|
1291
|
+
DropdownMenu.Item,
|
|
1292
|
+
{
|
|
1293
|
+
onClick: () => insertVariable(variable.key, format),
|
|
1294
|
+
children: FORMAT_LABELS[format]
|
|
1295
|
+
},
|
|
1296
|
+
format
|
|
1297
|
+
)) })
|
|
1298
|
+
] }, variable.key);
|
|
1299
|
+
})
|
|
1300
|
+
] }, group)) })
|
|
1301
|
+
] })
|
|
1302
|
+
] })
|
|
1303
|
+
] }) }),
|
|
1304
|
+
/* @__PURE__ */ jsxs(Container, { className: "overflow-hidden p-0", children: [
|
|
1305
|
+
/* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center justify-between gap-x-4 gap-y-1 px-6 py-4", children: [
|
|
1306
|
+
/* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Body" }),
|
|
1307
|
+
/* @__PURE__ */ jsxs(
|
|
1308
|
+
Text,
|
|
1309
|
+
{
|
|
1310
|
+
size: "small",
|
|
1311
|
+
className: "text-ui-fg-subtle flex items-center gap-x-1.5",
|
|
1312
|
+
children: [
|
|
1313
|
+
"Type ",
|
|
1314
|
+
/* @__PURE__ */ jsx(CodeChip, { children: "/" }),
|
|
1315
|
+
" to add a block"
|
|
1316
|
+
]
|
|
1317
|
+
}
|
|
1318
|
+
)
|
|
1319
|
+
] }),
|
|
1320
|
+
/* @__PURE__ */ jsx("div", { className: "border-ui-border-base border-t", children: /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(Skeleton, { className: "h-96 w-full" }), children: /* @__PURE__ */ jsx(
|
|
1321
|
+
EmailTemplateEditor,
|
|
1322
|
+
{
|
|
1323
|
+
ref: editorRef,
|
|
1324
|
+
template,
|
|
1325
|
+
onDirty: markBodyEdited
|
|
1326
|
+
}
|
|
1327
|
+
) }) })
|
|
1328
|
+
] }),
|
|
1329
|
+
previewing && /* @__PURE__ */ jsx(
|
|
1330
|
+
TemplatePreviewDrawer,
|
|
1331
|
+
{
|
|
1332
|
+
template,
|
|
1333
|
+
getDraft,
|
|
1334
|
+
onClose: () => setPreviewing(false)
|
|
1335
|
+
}
|
|
1336
|
+
),
|
|
1337
|
+
testing && /* @__PURE__ */ jsx(
|
|
1338
|
+
TemplateTestEmailDrawer,
|
|
1339
|
+
{
|
|
1340
|
+
template,
|
|
1341
|
+
getDraft,
|
|
1342
|
+
onClose: () => setTesting(false)
|
|
1343
|
+
}
|
|
1344
|
+
)
|
|
1345
|
+
] });
|
|
1346
|
+
};
|
|
1347
|
+
const handle = {
|
|
1348
|
+
breadcrumb: (match) => match.params.id ?? "Template"
|
|
1349
|
+
};
|
|
1350
|
+
const i18nTranslations0 = {};
|
|
1351
|
+
const widgetModule = { widgets: [] };
|
|
1352
|
+
const routeModule = {
|
|
1353
|
+
routes: [
|
|
1354
|
+
{
|
|
1355
|
+
Component: EmailSettingsPage,
|
|
1356
|
+
path: "/settings/email",
|
|
1357
|
+
handle: { label: config.label, translationNs: config.translationNs, permissions: config.permissions, ...handle$1 }
|
|
1358
|
+
},
|
|
1359
|
+
{
|
|
1360
|
+
Component: EmailTemplatePage,
|
|
1361
|
+
path: "/settings/email/templates/:id",
|
|
1362
|
+
handle
|
|
1363
|
+
}
|
|
1364
|
+
]
|
|
1365
|
+
};
|
|
1366
|
+
const menuItemModule = {
|
|
1367
|
+
menuItems: [
|
|
1368
|
+
{
|
|
1369
|
+
label: config.label,
|
|
1370
|
+
icon: config.icon,
|
|
1371
|
+
path: "/settings/email",
|
|
1372
|
+
nested: void 0,
|
|
1373
|
+
rank: void 0,
|
|
1374
|
+
translationNs: void 0,
|
|
1375
|
+
permissions: void 0
|
|
1376
|
+
}
|
|
1377
|
+
]
|
|
1378
|
+
};
|
|
1379
|
+
const formModule = { customFields: {} };
|
|
1380
|
+
const displayModule = {
|
|
1381
|
+
displays: {}
|
|
1382
|
+
};
|
|
1383
|
+
const i18nModule = { resources: i18nTranslations0 };
|
|
1384
|
+
const cellRendererModule = {};
|
|
1385
|
+
const layoutModule = { layouts: [] };
|
|
1386
|
+
const searchEntityModule = {};
|
|
1387
|
+
const plugin = {
|
|
1388
|
+
widgetModule,
|
|
1389
|
+
routeModule,
|
|
1390
|
+
menuItemModule,
|
|
1391
|
+
formModule,
|
|
1392
|
+
displayModule,
|
|
1393
|
+
i18nModule,
|
|
1394
|
+
cellRendererModule,
|
|
1395
|
+
layoutModule,
|
|
1396
|
+
searchEntityModule
|
|
1397
|
+
};
|
|
1398
|
+
export {
|
|
1399
|
+
FORMAT_LABELS as F,
|
|
1400
|
+
attributesFor as a,
|
|
1401
|
+
findLiquidExpressions as b,
|
|
1402
|
+
toLiquidExpression as c,
|
|
1403
|
+
formatsFor as f,
|
|
1404
|
+
groupVariables as g,
|
|
1405
|
+
isVariableFormat as i,
|
|
1406
|
+
listVariables as l,
|
|
1407
|
+
plugin as p,
|
|
1408
|
+
scalarVariables as s,
|
|
1409
|
+
toLiquidLoop as t,
|
|
1410
|
+
urlVariables as u,
|
|
1411
|
+
variablesAt as v
|
|
1412
|
+
};
|