Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Refactor campaign email preview to use HTML body

Kiran K46e9f6e7d4c5
diff --git a/apps/web/lib/actions/campaigns/send-campaign-preview-email.ts b/apps/web/lib/actions/campaigns/send-campaign-preview-email.ts
index 401cc87fc1..9c9d317322 100644
--- a/apps/web/lib/actions/campaigns/send-campaign-preview-email.ts
+++ b/apps/web/lib/actions/campaigns/send-campaign-preview-email.ts
@@ -1,10 +1,13 @@
"use server";
import { getDefaultProgramIdOrThrow } from "@/lib/api/programs/get-default-program-id-or-throw";
+import { renderEmailTemplate } from "@/lib/api/workflows/render-email-template";
import { CampaignSchema } from "@/lib/zod/schemas/campaigns";
import { sendBatchEmail } from "@dub/email";
import CampaignEmail from "@dub/email/templates/campaign-email";
import { prisma } from "@dub/prisma";
+import { generateHTML } from "@tiptap/html/server";
+import StarterKit from "@tiptap/starter-kit";
import { z } from "zod";
import { authActionClient } from "../safe-action";
@@ -39,7 +42,19 @@ export const sendCampaignPreviewEmail = authActionClient
},
});
- console.log(JSON.stringify(bodyJson, null, 2));
+ const bodyHtml = renderEmailTemplate({
+ template: generateHTML(bodyJson, [
+ StarterKit.configure({
+ heading: {
+ levels: [1, 2],
+ },
+ }),
+ ]),
+ variables: {
+ PartnerName: "Partner",
+ PartnerEmail: "[email protected]",
+ },
+ });
await sendBatchEmail(
emailAddresses.map((email) => ({
@@ -49,11 +64,7 @@ export const sendCampaignPreviewEmail = authActionClient
react: CampaignEmail({
campaign: {
subject,
- bodyJson: JSON.parse(JSON.stringify(bodyJson)),
- variables: {
- "PartnerName": "John Doe",
- "PartnerEmail": "[email protected]",
- },
+ body: bodyHtml,
},
}),
})),
diff --git a/apps/web/package.json b/apps/web/package.json
index 1459d1f65b..eddd0fb0ca 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -41,7 +41,9 @@
"@stripe/stripe-js": "^7.3.1",
"@tanstack/react-table": "^8.17.3",
"@team-plain/typescript-sdk": "^5.9.0",
+ "@tiptap/extension-mention": "^3.0.9",
"@tiptap/html": "^3.6.5",
+ "@tiptap/starter-kit": "^3.0.9",
"@types/base-x": "^3.0.10",
"@types/bcryptjs": "^2.4.6",
"@types/buffer-crc32": "0.2.0",
diff --git a/packages/email/src/templates/campaign-email.tsx b/packages/email/src/templates/campaign-email.tsx
index 00ecfa22a6..d468d42502 100644
--- a/packages/email/src/templates/campaign-email.tsx
+++ b/packages/email/src/templates/campaign-email.tsx
@@ -9,71 +9,18 @@ import {
Section,
Tailwind,
} from "@react-email/components";
-import Mention from "@tiptap/extension-mention";
-import { generateHTML } from "@tiptap/html";
-import StarterKit from "@tiptap/starter-kit";
-
-const bodyJson = {
- type: "doc",
- content: [
- {
- type: "paragraph",
- content: [
- {
- type: "text",
- text: "Hello, world!",
- },
- ],
- },
- ],
-};
export default function CampaignEmail({
campaign = {
subject: "Test Subject",
- bodyJson,
- variables: {},
+ body: "",
},
}: {
campaign?: {
subject: string;
- bodyJson: any;
- variables: Record<string, string>;
+ body: string;
};
}) {
- let bodyHtml = generateHTML(campaign.bodyJson, [
- StarterKit.configure({
- heading: {
- levels: [1, 2],
- },
- }),
- Mention.extend({
- renderHTML({ node }: { node: any }) {
- return [
- "span",
- {
- class:
- "px-1 py-0.5 bg-blue-100 text-blue-700 rounded font-semibold",
- "data-type": "mention",
- "data-id": node.attrs.id,
- },
- `{{${node.attrs.id}}}`,
- ];
- },
- renderText({ node }: { node: any }) {
- return `{{${node.attrs.id}}}`;
- },
- }),
- ]);
-
- bodyHtml = bodyHtml.replace(
- /{{\s*([\w.]+)(?:\|([^}]+))?\s*}}/g,
- (_, key, fallback) => {
- const value = campaign.variables[key];
- return value != null ? String(value) : fallback ?? "";
- },
- );
-
return (
<Html>
<Head />
@@ -86,7 +33,7 @@ export default function CampaignEmail({
</Section>
<Section className="my-6">
- <div dangerouslySetInnerHTML={{ __html: bodyHtml }} />
+ <div dangerouslySetInnerHTML={{ __html: campaign.body }} />
</Section>
</Container>
</Body>
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 75a9032eea..8e618cd066 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -112,9 +112,15 @@ importers:
'@team-plain/typescript-sdk':
specifier: ^5.9.0
version: 5.9.0
+ '@tiptap/extension-mention':
+ specifier: ^3.0.9
+ version: 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
'@tiptap/html':
specifier: ^3.6.5
+ '@tiptap/starter-kit':
+ specifier: ^3.0.9
+ version: 3.0.9
'@types/base-x':
specifier: ^3.0.10
version: 3.0.10