Improve bulk email compose with full template list and free-form HTML modes.
Load all active email templates for the send composer, fix API parsing/pagination, and add template preview with scheduled-job template labels. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
5df5a7f742
commit
2e59a446e5
|
|
@ -12,9 +12,16 @@ import type {
|
||||||
UpdateEmailTemplateResponse,
|
UpdateEmailTemplateResponse,
|
||||||
} from "../types/emailTemplate.types"
|
} from "../types/emailTemplate.types"
|
||||||
|
|
||||||
/** GET /admin/email-templates — list all email templates. */
|
export interface GetEmailTemplatesParams {
|
||||||
export const getEmailTemplates = () =>
|
status?: string
|
||||||
http.get<GetEmailTemplatesResponse>("/admin/email-templates")
|
limit?: number
|
||||||
|
offset?: number
|
||||||
|
query?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** GET /admin/email-templates — list email templates (paginated). */
|
||||||
|
export const getEmailTemplates = (params?: GetEmailTemplatesParams) =>
|
||||||
|
http.get<GetEmailTemplatesResponse>("/admin/email-templates", { params })
|
||||||
|
|
||||||
/** GET /admin/email-templates/slug/:slug — single template by slug. */
|
/** GET /admin/email-templates/slug/:slug — single template by slug. */
|
||||||
export const getEmailTemplateBySlug = (slug: string) =>
|
export const getEmailTemplateBySlug = (slug: string) =>
|
||||||
|
|
@ -23,28 +30,55 @@ export const getEmailTemplateBySlug = (slug: string) =>
|
||||||
)
|
)
|
||||||
|
|
||||||
function normalizeEmailTemplate(row: unknown): EmailTemplate | null {
|
function normalizeEmailTemplate(row: unknown): EmailTemplate | null {
|
||||||
if (!row || typeof row !== "object" || !("slug" in row)) return null
|
if (!row || typeof row !== "object") return null
|
||||||
const t = row as EmailTemplate
|
const o = row as Record<string, unknown>
|
||||||
|
const slug = String(o.slug ?? o.Slug ?? "").trim()
|
||||||
|
if (!slug) return null
|
||||||
|
|
||||||
|
const id = Number(o.id ?? o.ID ?? o.Id)
|
||||||
|
const variablesRaw = o.variables ?? o.Variables
|
||||||
|
const variables = Array.isArray(variablesRaw)
|
||||||
|
? variablesRaw.map((v) => String(v).trim()).filter(Boolean)
|
||||||
|
: []
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...t,
|
id: Number.isFinite(id) && id > 0 ? id : 0,
|
||||||
variables: Array.isArray(t.variables) ? t.variables : [],
|
slug,
|
||||||
status: t.status ?? "ACTIVE",
|
name: String(o.name ?? o.Name ?? slug),
|
||||||
updated_at: t.updated_at ?? t.created_at ?? "",
|
subject: String(o.subject ?? o.Subject ?? ""),
|
||||||
|
body_text: String(o.body_text ?? o.BodyText ?? o.bodyText ?? ""),
|
||||||
|
body_html: String(o.body_html ?? o.BodyHtml ?? o.bodyHTML ?? ""),
|
||||||
|
variables,
|
||||||
|
is_system: Boolean(o.is_system ?? o.IsSystem ?? false),
|
||||||
|
status: String(o.status ?? o.Status ?? "ACTIVE"),
|
||||||
|
created_at: String(o.created_at ?? o.CreatedAt ?? ""),
|
||||||
|
updated_at: String(o.updated_at ?? o.UpdatedAt ?? o.created_at ?? o.CreatedAt ?? ""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extractTemplateRows(data: unknown): unknown[] {
|
||||||
|
if (Array.isArray(data)) return data
|
||||||
|
if (!data || typeof data !== "object") return []
|
||||||
|
const record = data as Record<string, unknown>
|
||||||
|
const candidates = [
|
||||||
|
record.templates,
|
||||||
|
record.Templates,
|
||||||
|
record.email_templates,
|
||||||
|
record.EmailTemplates,
|
||||||
|
]
|
||||||
|
for (const candidate of candidates) {
|
||||||
|
if (Array.isArray(candidate)) return candidate
|
||||||
|
}
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
export function parseEmailTemplatesResponse(
|
export function parseEmailTemplatesResponse(
|
||||||
response: Awaited<ReturnType<typeof getEmailTemplates>>,
|
response: Awaited<ReturnType<typeof getEmailTemplates>>,
|
||||||
): EmailTemplate[] {
|
): EmailTemplate[] {
|
||||||
const data = response.data?.data
|
const data = response.data?.data
|
||||||
const rows = Array.isArray(data)
|
return extractTemplateRows(data)
|
||||||
? data
|
|
||||||
: Array.isArray(data?.templates)
|
|
||||||
? data.templates
|
|
||||||
: []
|
|
||||||
return rows
|
|
||||||
.map(normalizeEmailTemplate)
|
.map(normalizeEmailTemplate)
|
||||||
.filter((row): row is EmailTemplate => row != null)
|
.filter((row): row is EmailTemplate => row != null && row.id > 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** PUT /admin/email-templates/:id — update subject and bodies. */
|
/** PUT /admin/email-templates/:id — update subject and bodies. */
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { useEffect, useMemo, useState } from "react"
|
import { useEffect, useMemo, useState } from "react"
|
||||||
import { Eye, RefreshCw } from "lucide-react"
|
import { Link } from "react-router-dom"
|
||||||
|
import { Eye, ExternalLink, RefreshCw } from "lucide-react"
|
||||||
import { toast } from "sonner"
|
import { toast } from "sonner"
|
||||||
import {
|
import {
|
||||||
parseEmailTemplatePreviewResponse,
|
parseEmailTemplatePreviewResponse,
|
||||||
|
|
@ -10,12 +11,17 @@ import { Input } from "../ui/input"
|
||||||
import { SpinnerIcon } from "../ui/spinner-icon"
|
import { SpinnerIcon } from "../ui/spinner-icon"
|
||||||
import { Textarea } from "../ui/textarea"
|
import { Textarea } from "../ui/textarea"
|
||||||
import { Select } from "../ui/select"
|
import { Select } from "../ui/select"
|
||||||
|
import { cn } from "../../lib/utils"
|
||||||
import {
|
import {
|
||||||
buildEmailTemplateVariablesForSend,
|
buildEmailTemplateVariablesForSend,
|
||||||
|
defaultOutboundEmailTemplateSlug,
|
||||||
|
isAutomatedEmailTemplateSlug,
|
||||||
isCustomMessageTemplate,
|
isCustomMessageTemplate,
|
||||||
} from "../../lib/notificationEmailCompose"
|
} from "../../lib/notificationEmailCompose"
|
||||||
import type { EmailTemplate, PreviewEmailTemplateResult } from "../../types/emailTemplate.types"
|
import type { EmailTemplate, PreviewEmailTemplateResult } from "../../types/emailTemplate.types"
|
||||||
|
|
||||||
|
type EmailContentMode = "freeform" | "template"
|
||||||
|
|
||||||
type EmailComposeFieldsProps = {
|
type EmailComposeFieldsProps = {
|
||||||
templates: EmailTemplate[]
|
templates: EmailTemplate[]
|
||||||
templatesLoading: boolean
|
templatesLoading: boolean
|
||||||
|
|
@ -47,20 +53,48 @@ export function EmailComposeFields({
|
||||||
}: EmailComposeFieldsProps) {
|
}: EmailComposeFieldsProps) {
|
||||||
const [previewLoading, setPreviewLoading] = useState(false)
|
const [previewLoading, setPreviewLoading] = useState(false)
|
||||||
const [preview, setPreview] = useState<PreviewEmailTemplateResult | null>(null)
|
const [preview, setPreview] = useState<PreviewEmailTemplateResult | null>(null)
|
||||||
|
const [contentMode, setContentMode] = useState<EmailContentMode>(
|
||||||
|
templateSlug ? "template" : "freeform",
|
||||||
|
)
|
||||||
|
|
||||||
const selectedTemplate = useMemo(
|
const selectedTemplate = useMemo(
|
||||||
() => templates.find((t) => t.slug === templateSlug) ?? null,
|
() => templates.find((t) => t.slug === templateSlug) ?? null,
|
||||||
[templates, templateSlug],
|
[templates, templateSlug],
|
||||||
)
|
)
|
||||||
|
|
||||||
const isFreeForm = !templateSlug
|
const isFreeForm = contentMode === "freeform"
|
||||||
const isCustomMessage = isCustomMessageTemplate(templateSlug)
|
const isCustomMessage = isCustomMessageTemplate(templateSlug)
|
||||||
const usesTemplateVariables =
|
const usesTemplateVariables =
|
||||||
Boolean(templateSlug) && !isCustomMessage && (selectedTemplate?.variables.length ?? 0) > 0
|
contentMode === "template" &&
|
||||||
|
Boolean(templateSlug) &&
|
||||||
|
!isCustomMessage &&
|
||||||
|
(selectedTemplate?.variables.length ?? 0) > 0
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setContentMode(templateSlug ? "template" : "freeform")
|
||||||
|
}, [templateSlug])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setPreview(null)
|
setPreview(null)
|
||||||
}, [templateSlug, subject, message, htmlBody, templateVariables])
|
}, [templateSlug, subject, message, htmlBody, templateVariables, contentMode])
|
||||||
|
|
||||||
|
const switchToFreeForm = () => {
|
||||||
|
setContentMode("freeform")
|
||||||
|
onTemplateSlugChange("")
|
||||||
|
onTemplateVariablesChange({})
|
||||||
|
setPreview(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
const switchToTemplate = () => {
|
||||||
|
setContentMode("template")
|
||||||
|
const slug = templateSlug || defaultOutboundEmailTemplateSlug(templates)
|
||||||
|
onTemplateSlugChange(slug)
|
||||||
|
onTemplateVariablesChange({})
|
||||||
|
if (slug && !isCustomMessageTemplate(slug)) {
|
||||||
|
onHtmlBodyChange("")
|
||||||
|
}
|
||||||
|
setPreview(null)
|
||||||
|
}
|
||||||
|
|
||||||
const handleTemplateChange = (slug: string) => {
|
const handleTemplateChange = (slug: string) => {
|
||||||
onTemplateSlugChange(slug)
|
onTemplateSlugChange(slug)
|
||||||
|
|
@ -68,6 +102,7 @@ export function EmailComposeFields({
|
||||||
if (slug && !isCustomMessageTemplate(slug)) {
|
if (slug && !isCustomMessageTemplate(slug)) {
|
||||||
onHtmlBodyChange("")
|
onHtmlBodyChange("")
|
||||||
}
|
}
|
||||||
|
setPreview(null)
|
||||||
}
|
}
|
||||||
|
|
||||||
const handlePreview = async () => {
|
const handlePreview = async () => {
|
||||||
|
|
@ -98,133 +133,229 @@ export function EmailComposeFields({
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="space-y-3">
|
<div className="space-y-4">
|
||||||
<div>
|
<div className="flex flex-wrap items-start justify-between gap-2">
|
||||||
<label className="mb-1 block text-xs font-medium text-grayScale-500">
|
<div>
|
||||||
Email content mode
|
<p className="text-xs font-semibold uppercase tracking-[0.14em] text-grayScale-400">
|
||||||
</label>
|
Email content
|
||||||
<Select
|
</p>
|
||||||
value={templateSlug}
|
<p className="mt-0.5 text-[11px] text-grayScale-500">
|
||||||
onChange={(e) => handleTemplateChange(e.target.value)}
|
Send free-form HTML or render a stored email template on the server.
|
||||||
disabled={templatesLoading}
|
</p>
|
||||||
>
|
</div>
|
||||||
<option value="">Free-form (subject + message/HTML)</option>
|
<Button type="button" variant="ghost" size="sm" className="h-8 text-xs" asChild>
|
||||||
{templates.map((template) => (
|
<Link to="/notifications/email-templates">
|
||||||
<option key={template.slug} value={template.slug}>
|
<ExternalLink className="mr-1.5 h-3.5 w-3.5" />
|
||||||
{template.name} ({template.slug})
|
Manage templates
|
||||||
</option>
|
</Link>
|
||||||
))}
|
</Button>
|
||||||
</Select>
|
|
||||||
<p className="mt-1 text-[10px] text-grayScale-400">
|
|
||||||
{templatesLoading
|
|
||||||
? "Loading templates…"
|
|
||||||
: "System templates (OTP, welcome, etc.) are excluded from outbound campaigns."}
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{(isFreeForm || isCustomMessage) && (
|
<div className="inline-flex rounded-full border border-grayScale-200 bg-grayScale-50 p-0.5 text-xs font-medium">
|
||||||
<div>
|
<button
|
||||||
<label className="mb-1 block text-xs font-medium text-grayScale-500">Subject</label>
|
type="button"
|
||||||
<Input
|
onClick={switchToFreeForm}
|
||||||
placeholder="Email subject line"
|
className={cn(
|
||||||
value={subject}
|
"rounded-full px-3 py-1.5 transition-colors",
|
||||||
onChange={(e) => onSubjectChange(e.target.value)}
|
isFreeForm
|
||||||
/>
|
? "bg-brand-500 text-white shadow-sm"
|
||||||
</div>
|
: "text-grayScale-500 hover:text-grayScale-700",
|
||||||
)}
|
|
||||||
|
|
||||||
{(isFreeForm || isCustomMessage) && (
|
|
||||||
<div>
|
|
||||||
<label className="mb-1 block text-xs font-medium text-grayScale-500">Message</label>
|
|
||||||
<Textarea
|
|
||||||
rows={4}
|
|
||||||
placeholder="Plain-text email body"
|
|
||||||
value={message}
|
|
||||||
onChange={(e) => onMessageChange(e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{isFreeForm && (
|
|
||||||
<div>
|
|
||||||
<label className="mb-1 block text-xs font-medium text-grayScale-500">
|
|
||||||
HTML body (optional)
|
|
||||||
</label>
|
|
||||||
<Textarea
|
|
||||||
rows={3}
|
|
||||||
placeholder="<p>Rich HTML content</p>"
|
|
||||||
value={htmlBody}
|
|
||||||
onChange={(e) => onHtmlBodyChange(e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{usesTemplateVariables && selectedTemplate && (
|
|
||||||
<div className="space-y-2 rounded-lg border border-grayScale-100 bg-grayScale-50/50 p-3">
|
|
||||||
<p className="text-xs font-semibold text-grayScale-600">Template variables</p>
|
|
||||||
{selectedTemplate.variables.map((variable) => (
|
|
||||||
<div key={variable}>
|
|
||||||
<label className="mb-1 block text-[11px] font-medium text-grayScale-500">
|
|
||||||
{variable}
|
|
||||||
</label>
|
|
||||||
<Input
|
|
||||||
value={templateVariables[variable] ?? ""}
|
|
||||||
onChange={(e) =>
|
|
||||||
onTemplateVariablesChange({
|
|
||||||
...templateVariables,
|
|
||||||
[variable]: e.target.value,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
placeholder={`Value for {{.${variable}}}`}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
))}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
|
|
||||||
{templateSlug && (
|
|
||||||
<div className="flex flex-wrap items-center gap-2">
|
|
||||||
<Button
|
|
||||||
type="button"
|
|
||||||
variant="outline"
|
|
||||||
size="sm"
|
|
||||||
onClick={() => void handlePreview()}
|
|
||||||
disabled={previewLoading}
|
|
||||||
>
|
|
||||||
{previewLoading ? (
|
|
||||||
<SpinnerIcon className="mr-2 h-3.5 w-3.5" alt="" />
|
|
||||||
) : (
|
|
||||||
<Eye className="mr-2 h-3.5 w-3.5" />
|
|
||||||
)}
|
|
||||||
Preview rendered email
|
|
||||||
</Button>
|
|
||||||
{preview && (
|
|
||||||
<Button type="button" variant="ghost" size="sm" onClick={() => setPreview(null)}>
|
|
||||||
<RefreshCw className="mr-2 h-3.5 w-3.5" />
|
|
||||||
Hide preview
|
|
||||||
</Button>
|
|
||||||
)}
|
)}
|
||||||
</div>
|
>
|
||||||
)}
|
Free-form HTML
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={switchToTemplate}
|
||||||
|
disabled={templatesLoading || templates.length === 0}
|
||||||
|
className={cn(
|
||||||
|
"rounded-full px-3 py-1.5 transition-colors disabled:cursor-not-allowed disabled:opacity-50",
|
||||||
|
!isFreeForm
|
||||||
|
? "bg-brand-500 text-white shadow-sm"
|
||||||
|
: "text-grayScale-500 hover:text-grayScale-700",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
Email template
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
{preview && (
|
{isFreeForm ? (
|
||||||
<div className="space-y-2 rounded-lg border border-grayScale-200 bg-white p-3">
|
<div className="space-y-3 rounded-lg border border-grayScale-100 bg-grayScale-50/40 p-3">
|
||||||
<p className="text-xs font-semibold text-grayScale-600">Server preview</p>
|
<div>
|
||||||
<p className="rounded-md border border-grayScale-100 bg-grayScale-50 px-2 py-1.5 text-xs font-medium text-grayScale-800">
|
<label className="mb-1 block text-xs font-medium text-grayScale-500">Subject</label>
|
||||||
{preview.subject || "(no subject)"}
|
<Input
|
||||||
</p>
|
placeholder="Email subject line"
|
||||||
{preview.html ? (
|
value={subject}
|
||||||
<iframe
|
onChange={(e) => onSubjectChange(e.target.value)}
|
||||||
title="Email preview"
|
|
||||||
sandbox=""
|
|
||||||
srcDoc={preview.html}
|
|
||||||
className="h-48 w-full rounded-md border border-grayScale-200 bg-white"
|
|
||||||
/>
|
/>
|
||||||
) : (
|
</div>
|
||||||
<pre className="max-h-40 overflow-auto whitespace-pre-wrap rounded-md border border-grayScale-100 bg-grayScale-50 p-2 text-xs text-grayScale-700">
|
<div>
|
||||||
{preview.text || "(empty body)"}
|
<label className="mb-1 block text-xs font-medium text-grayScale-500">Message</label>
|
||||||
</pre>
|
<Textarea
|
||||||
|
rows={4}
|
||||||
|
placeholder="Plain-text email body"
|
||||||
|
value={message}
|
||||||
|
onChange={(e) => onMessageChange(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-xs font-medium text-grayScale-500">
|
||||||
|
HTML body (optional)
|
||||||
|
</label>
|
||||||
|
<Textarea
|
||||||
|
rows={4}
|
||||||
|
placeholder="<p>Rich HTML content</p>"
|
||||||
|
value={htmlBody}
|
||||||
|
onChange={(e) => onHtmlBodyChange(e.target.value)}
|
||||||
|
/>
|
||||||
|
<p className="mt-1 text-[10px] text-grayScale-400">
|
||||||
|
Provide message, HTML, or both. Sent as-is without a template wrapper.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-3 rounded-lg border border-grayScale-100 bg-grayScale-50/40 p-3">
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-xs font-medium text-grayScale-500">
|
||||||
|
Email template
|
||||||
|
</label>
|
||||||
|
<Select
|
||||||
|
value={templateSlug}
|
||||||
|
onChange={(e) => handleTemplateChange(e.target.value)}
|
||||||
|
disabled={templatesLoading || templates.length === 0}
|
||||||
|
>
|
||||||
|
{templates.length === 0 ? (
|
||||||
|
<option value="">No templates available</option>
|
||||||
|
) : (
|
||||||
|
templates.map((template) => (
|
||||||
|
<option key={template.slug} value={template.slug}>
|
||||||
|
{template.name} ({template.slug})
|
||||||
|
{isAutomatedEmailTemplateSlug(template.slug) ? " · system" : ""}
|
||||||
|
</option>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</Select>
|
||||||
|
<p className="mt-1 text-[10px] text-grayScale-400">
|
||||||
|
{templatesLoading
|
||||||
|
? "Loading templates…"
|
||||||
|
: `${templates.length} active template${templates.length === 1 ? "" : "s"} from the template store.`}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{selectedTemplate && selectedTemplate.variables.length === 0 && !isCustomMessage ? (
|
||||||
|
<p className="rounded-md border border-grayScale-100 bg-white px-2.5 py-2 text-[11px] text-grayScale-500">
|
||||||
|
This template has no placeholders — subject and HTML are taken from the template
|
||||||
|
body when sent.
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{isAutomatedEmailTemplateSlug(templateSlug) ? (
|
||||||
|
<p className="rounded-md border border-amber-200 bg-amber-50 px-2.5 py-2 text-[11px] text-amber-900">
|
||||||
|
This template is normally used by automated flows (OTP, invites, etc.). Fill all
|
||||||
|
variables below before sending.
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{isCustomMessage && (
|
||||||
|
<>
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-xs font-medium text-grayScale-500">Subject</label>
|
||||||
|
<Input
|
||||||
|
placeholder="Email subject line"
|
||||||
|
value={subject}
|
||||||
|
onChange={(e) => onSubjectChange(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="mb-1 block text-xs font-medium text-grayScale-500">Message</label>
|
||||||
|
<Textarea
|
||||||
|
rows={4}
|
||||||
|
placeholder="Body text rendered inside the branded custom_message template"
|
||||||
|
value={message}
|
||||||
|
onChange={(e) => onMessageChange(e.target.value)}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
{usesTemplateVariables && selectedTemplate && (
|
||||||
|
<div className="space-y-2 rounded-lg border border-grayScale-100 bg-white p-3">
|
||||||
|
<p className="text-xs font-semibold text-grayScale-600">Template variables</p>
|
||||||
|
{selectedTemplate.variables.map((variable) => (
|
||||||
|
<div key={variable}>
|
||||||
|
<label className="mb-1 block text-[11px] font-medium text-grayScale-500">
|
||||||
|
{variable}
|
||||||
|
</label>
|
||||||
|
<Input
|
||||||
|
value={templateVariables[variable] ?? ""}
|
||||||
|
onChange={(e) =>
|
||||||
|
onTemplateVariablesChange({
|
||||||
|
...templateVariables,
|
||||||
|
[variable]: e.target.value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
placeholder={`Value for {{.${variable}}}`}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{!templatesLoading && templates.length === 0 ? (
|
||||||
|
<p className="text-xs text-amber-800">
|
||||||
|
No outbound templates found. Use free-form HTML or{" "}
|
||||||
|
<Link to="/notifications/email-templates/new" className="font-semibold underline">
|
||||||
|
create a template
|
||||||
|
</Link>
|
||||||
|
.
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{templateSlug ? (
|
||||||
|
<div className="flex flex-wrap items-center gap-2">
|
||||||
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => void handlePreview()}
|
||||||
|
disabled={previewLoading}
|
||||||
|
>
|
||||||
|
{previewLoading ? (
|
||||||
|
<SpinnerIcon className="mr-2 h-3.5 w-3.5" alt="" />
|
||||||
|
) : (
|
||||||
|
<Eye className="mr-2 h-3.5 w-3.5" />
|
||||||
|
)}
|
||||||
|
Preview rendered email
|
||||||
|
</Button>
|
||||||
|
{preview ? (
|
||||||
|
<Button type="button" variant="ghost" size="sm" onClick={() => setPreview(null)}>
|
||||||
|
<RefreshCw className="mr-2 h-3.5 w-3.5" />
|
||||||
|
Hide preview
|
||||||
|
</Button>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{preview ? (
|
||||||
|
<div className="space-y-2 rounded-lg border border-grayScale-200 bg-white p-3">
|
||||||
|
<p className="text-xs font-semibold text-grayScale-600">Server preview</p>
|
||||||
|
<p className="rounded-md border border-grayScale-100 bg-grayScale-50 px-2 py-1.5 text-xs font-medium text-grayScale-800">
|
||||||
|
{preview.subject || "(no subject)"}
|
||||||
|
</p>
|
||||||
|
{preview.html ? (
|
||||||
|
<iframe
|
||||||
|
title="Email preview"
|
||||||
|
sandbox=""
|
||||||
|
srcDoc={preview.html}
|
||||||
|
className="h-48 w-full rounded-md border border-grayScale-200 bg-white"
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<pre className="max-h-40 overflow-auto whitespace-pre-wrap rounded-md border border-grayScale-100 bg-grayScale-50 p-2 text-xs text-grayScale-700">
|
||||||
|
{preview.text || "(empty body)"}
|
||||||
|
</pre>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
|
import { getEmailTemplates, parseEmailTemplatesResponse } from "../api/emailTemplates.api"
|
||||||
import type { EmailTemplate } from "../types/emailTemplate.types"
|
import type { EmailTemplate } from "../types/emailTemplate.types"
|
||||||
|
|
||||||
/** Reserved for automated learner/team flows — not for admin outbound campaigns. */
|
const TEMPLATE_LIST_PAGE_SIZE = 100
|
||||||
|
|
||||||
|
/** Slugs reserved for automated learner/team flows — shown with a label in the compose UI. */
|
||||||
export const AUTOMATED_EMAIL_TEMPLATE_SLUGS = new Set([
|
export const AUTOMATED_EMAIL_TEMPLATE_SLUGS = new Set([
|
||||||
"otp",
|
"otp",
|
||||||
"invitation",
|
"invitation",
|
||||||
|
|
@ -8,14 +11,71 @@ export const AUTOMATED_EMAIL_TEMPLATE_SLUGS = new Set([
|
||||||
"welcome",
|
"welcome",
|
||||||
])
|
])
|
||||||
|
|
||||||
export function isOutboundEmailTemplate(template: EmailTemplate): boolean {
|
export function isAutomatedEmailTemplateSlug(slug: string): boolean {
|
||||||
if (String(template.status ?? "").toUpperCase() !== "ACTIVE") return false
|
return AUTOMATED_EMAIL_TEMPLATE_SLUGS.has(slug)
|
||||||
if (AUTOMATED_EMAIL_TEMPLATE_SLUGS.has(template.slug)) return false
|
|
||||||
return template.slug === "custom_message" || !template.is_system
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** ACTIVE templates available in bulk-email compose. */
|
||||||
|
export function isComposeEmailTemplate(template: EmailTemplate): boolean {
|
||||||
|
return String(template.status ?? "").toUpperCase() === "ACTIVE"
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @deprecated Use isComposeEmailTemplate */
|
||||||
|
export function isOutboundEmailTemplate(template: EmailTemplate): boolean {
|
||||||
|
return isComposeEmailTemplate(template)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function filterComposeEmailTemplates(templates: EmailTemplate[]): EmailTemplate[] {
|
||||||
|
return templates.filter(isComposeEmailTemplate)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @deprecated Use filterComposeEmailTemplates */
|
||||||
export function filterOutboundEmailTemplates(templates: EmailTemplate[]): EmailTemplate[] {
|
export function filterOutboundEmailTemplates(templates: EmailTemplate[]): EmailTemplate[] {
|
||||||
return templates.filter(isOutboundEmailTemplate)
|
return filterComposeEmailTemplates(templates)
|
||||||
|
}
|
||||||
|
|
||||||
|
function sortOutboundEmailTemplates(templates: EmailTemplate[]): EmailTemplate[] {
|
||||||
|
return [...templates].sort((a, b) => {
|
||||||
|
if (a.slug === "custom_message") return -1
|
||||||
|
if (b.slug === "custom_message") return 1
|
||||||
|
return a.name.localeCompare(b.name)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Loads all ACTIVE email templates for bulk-email compose. */
|
||||||
|
export async function fetchActiveOutboundEmailTemplates(): Promise<EmailTemplate[]> {
|
||||||
|
const all: EmailTemplate[] = []
|
||||||
|
let offset = 0
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const response = await getEmailTemplates({
|
||||||
|
limit: TEMPLATE_LIST_PAGE_SIZE,
|
||||||
|
offset,
|
||||||
|
})
|
||||||
|
const rawPage = parseEmailTemplatesResponse(response)
|
||||||
|
if (rawPage.length === 0) break
|
||||||
|
|
||||||
|
all.push(...rawPage)
|
||||||
|
|
||||||
|
const data = response.data?.data
|
||||||
|
const totalRaw =
|
||||||
|
data && typeof data === "object" && !Array.isArray(data)
|
||||||
|
? (data as Record<string, unknown>).total_count ??
|
||||||
|
(data as Record<string, unknown>).TotalCount
|
||||||
|
: undefined
|
||||||
|
const total = totalRaw != null ? Number(totalRaw) : undefined
|
||||||
|
|
||||||
|
offset += rawPage.length
|
||||||
|
if (total != null && Number.isFinite(total) && offset >= total) break
|
||||||
|
if (rawPage.length < TEMPLATE_LIST_PAGE_SIZE) break
|
||||||
|
}
|
||||||
|
|
||||||
|
return sortOutboundEmailTemplates(filterComposeEmailTemplates(all))
|
||||||
|
}
|
||||||
|
|
||||||
|
export function defaultOutboundEmailTemplateSlug(templates: EmailTemplate[]): string {
|
||||||
|
const custom = templates.find((t) => t.slug === "custom_message")
|
||||||
|
return custom?.slug ?? templates[0]?.slug ?? ""
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCustomMessageTemplate(slug: string): boolean {
|
export function isCustomMessageTemplate(slug: string): boolean {
|
||||||
|
|
|
||||||
|
|
@ -12,10 +12,6 @@ import { SpinnerIcon } from "../../components/ui/spinner-icon"
|
||||||
import { NotificationSchedulePicker } from "../../components/notifications/NotificationSchedulePicker"
|
import { NotificationSchedulePicker } from "../../components/notifications/NotificationSchedulePicker"
|
||||||
import { EmailComposeFields } from "../../components/notifications/EmailComposeFields"
|
import { EmailComposeFields } from "../../components/notifications/EmailComposeFields"
|
||||||
import { cn } from "../../lib/utils"
|
import { cn } from "../../lib/utils"
|
||||||
import {
|
|
||||||
getEmailTemplates,
|
|
||||||
parseEmailTemplatesResponse,
|
|
||||||
} from "../../api/emailTemplates.api"
|
|
||||||
import {
|
import {
|
||||||
sendBulkEmail,
|
sendBulkEmail,
|
||||||
sendBulkInApp,
|
sendBulkInApp,
|
||||||
|
|
@ -24,7 +20,7 @@ import {
|
||||||
} from "../../api/notifications.api"
|
} from "../../api/notifications.api"
|
||||||
import {
|
import {
|
||||||
appendBulkEmailContentToForm,
|
appendBulkEmailContentToForm,
|
||||||
filterOutboundEmailTemplates,
|
fetchActiveOutboundEmailTemplates,
|
||||||
validateEmailComposeInput,
|
validateEmailComposeInput,
|
||||||
} from "../../lib/notificationEmailCompose"
|
} from "../../lib/notificationEmailCompose"
|
||||||
import {
|
import {
|
||||||
|
|
@ -102,6 +98,7 @@ function buildContentPreview(
|
||||||
title: string,
|
title: string,
|
||||||
message: string,
|
message: string,
|
||||||
emailTemplateSlug: string,
|
emailTemplateSlug: string,
|
||||||
|
htmlBody: string,
|
||||||
): { titlePreview: string; messagePreview: string } {
|
): { titlePreview: string; messagePreview: string } {
|
||||||
if (channel === "email" && emailTemplateSlug) {
|
if (channel === "email" && emailTemplateSlug) {
|
||||||
return {
|
return {
|
||||||
|
|
@ -109,6 +106,15 @@ function buildContentPreview(
|
||||||
messagePreview: message.trim() || `Using template "${emailTemplateSlug}"`,
|
messagePreview: message.trim() || `Using template "${emailTemplateSlug}"`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (channel === "email") {
|
||||||
|
return {
|
||||||
|
titlePreview: title.trim() || "Email",
|
||||||
|
messagePreview:
|
||||||
|
htmlBody.trim() ||
|
||||||
|
message.trim() ||
|
||||||
|
"Free-form email (no template)",
|
||||||
|
}
|
||||||
|
}
|
||||||
if (channel === "sms") {
|
if (channel === "sms") {
|
||||||
return {
|
return {
|
||||||
titlePreview: title.trim() || "SMS",
|
titlePreview: title.trim() || "SMS",
|
||||||
|
|
@ -190,10 +196,9 @@ export function CreateNotificationPage() {
|
||||||
}
|
}
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
setEmailTemplatesLoading(true)
|
setEmailTemplatesLoading(true)
|
||||||
getEmailTemplates()
|
fetchActiveOutboundEmailTemplates()
|
||||||
.then((response) => {
|
.then((templates) => {
|
||||||
if (cancelled) return
|
if (!cancelled) setEmailTemplates(templates)
|
||||||
setEmailTemplates(filterOutboundEmailTemplates(parseEmailTemplatesResponse(response)))
|
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
if (!cancelled) setEmailTemplates([])
|
if (!cancelled) setEmailTemplates([])
|
||||||
|
|
@ -384,7 +389,13 @@ export function CreateNotificationPage() {
|
||||||
directRecipients,
|
directRecipients,
|
||||||
channel,
|
channel,
|
||||||
)
|
)
|
||||||
const contentPreview = buildContentPreview(channel, title, message, emailTemplateSlug)
|
const contentPreview = buildContentPreview(
|
||||||
|
channel,
|
||||||
|
title,
|
||||||
|
message,
|
||||||
|
emailTemplateSlug,
|
||||||
|
htmlBody,
|
||||||
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
setSending(true)
|
setSending(true)
|
||||||
|
|
@ -1190,13 +1201,20 @@ export function CreateNotificationPage() {
|
||||||
: "Notification title")}
|
: "Notification title")}
|
||||||
</p>
|
</p>
|
||||||
<p className="truncate text-[11px] text-grayScale-500">
|
<p className="truncate text-[11px] text-grayScale-500">
|
||||||
{message || "Message preview will appear here."}
|
{channel === "email" && !emailTemplateSlug && htmlBody.trim()
|
||||||
|
? "HTML email body"
|
||||||
|
: message || "Message preview will appear here."}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-[10px] text-grayScale-400">
|
<p className="text-[10px] text-grayScale-400">
|
||||||
Channel: {channel.toUpperCase().replace("_", "-")}
|
Channel: {channel.toUpperCase().replace("_", "-")}
|
||||||
|
{channel === "email" && emailTemplateSlug
|
||||||
|
? ` · Template: ${emailTemplateSlug}`
|
||||||
|
: channel === "email" && htmlBody.trim()
|
||||||
|
? " · Free-form HTML"
|
||||||
|
: ""}
|
||||||
{isScheduling && scheduledAt
|
{isScheduling && scheduledAt
|
||||||
? ` · Scheduled ${formatScheduledAtLabel(scheduledAt)}`
|
? ` · Scheduled ${formatScheduledAtLabel(scheduledAt)}`
|
||||||
: ""}
|
: ""}
|
||||||
|
|
|
||||||
|
|
@ -263,6 +263,11 @@ export function ScheduledNotificationsPage() {
|
||||||
{job.title || "—"}
|
{job.title || "—"}
|
||||||
</p>
|
</p>
|
||||||
<p className="truncate text-xs text-grayScale-400">{job.message}</p>
|
<p className="truncate text-xs text-grayScale-400">{job.message}</p>
|
||||||
|
{job.channel === "email" && job.email_template_slug ? (
|
||||||
|
<p className="mt-0.5 truncate text-[10px] text-grayScale-400">
|
||||||
|
Template: {job.email_template_slug}
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell className="text-xs text-grayScale-500">
|
<TableCell className="text-xs text-grayScale-500">
|
||||||
{targetingSummary(job)}
|
{targetingSummary(job)}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user