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.

Add variables support and logging in campaign preview email

Kiran K79572093f36e
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 d8e3c013a9..401cc87fc1 100644
--- a/apps/web/lib/actions/campaigns/send-campaign-preview-email.ts
+++ b/apps/web/lib/actions/campaigns/send-campaign-preview-email.ts
@@ -39,6 +39,8 @@ export const sendCampaignPreviewEmail = authActionClient
},
});
+ console.log(JSON.stringify(bodyJson, null, 2));
+
await sendBatchEmail(
emailAddresses.map((email) => ({
variant: "notifications",
@@ -48,6 +50,10 @@ export const sendCampaignPreviewEmail = authActionClient
campaign: {
subject,
bodyJson: JSON.parse(JSON.stringify(bodyJson)),
+ variables: {
+ "PartnerName": "John Doe",
+ "PartnerEmail": "[email protected]",
+ },
},
}),
})),
diff --git a/packages/email/package.json b/packages/email/package.json
index 89a6286f7c..a1e7c5ba18 100644
--- a/packages/email/package.json
+++ b/packages/email/package.json
@@ -8,22 +8,23 @@
"dev": "email dev --dir ./src/templates -p 3333"
},
"dependencies": {
- "@dub/utils": "workspace:*",
"@dub/ui": "workspace:*",
+ "@dub/utils": "workspace:*",
"@react-email/components": "^0.0.22",
- "lucide-react": "^0.462.0",
- "nodemailer": "^6.9.3",
- "react-email": "^2.1.6",
- "resend": "^6.1.2",
"@tiptap/extension-file-handler": "^3.0.9",
"@tiptap/extension-image": "^3.0.9",
+ "@tiptap/extension-link": "^3.6.6",
"@tiptap/extension-mention": "^3.0.9",
"@tiptap/extensions": "^3.0.9",
+ "@tiptap/html": "^3.6.5",
"@tiptap/pm": "^3.0.9",
"@tiptap/react": "^3.0.9",
"@tiptap/starter-kit": "^3.0.9",
"@tiptap/static-renderer": "^3.6.6",
- "@tiptap/html": "^3.6.5"
+ "lucide-react": "^0.462.0",
+ "nodemailer": "^6.9.3",
+ "react-email": "^2.1.6",
+ "resend": "^6.1.2"
},
"devDependencies": {
"@types/nodemailer": "~6.4.17",
diff --git a/packages/email/src/templates/campaign-email.tsx b/packages/email/src/templates/campaign-email.tsx
index 25b8ba9813..00ecfa22a6 100644
--- a/packages/email/src/templates/campaign-email.tsx
+++ b/packages/email/src/templates/campaign-email.tsx
@@ -9,6 +9,7 @@ import {
Section,
Tailwind,
} from "@react-email/components";
+import Mention from "@tiptap/extension-mention";
import { generateHTML } from "@tiptap/html";
import StarterKit from "@tiptap/starter-kit";
@@ -31,19 +32,48 @@ export default function CampaignEmail({
campaign = {
subject: "Test Subject",
bodyJson,
+ variables: {},
},
}: {
campaign?: {
subject: string;
bodyJson: any;
+ variables: Record<string, string>;
};
}) {
- const bodyHtml = generateHTML(campaign.bodyJson, [
+ let bodyHtml = generateHTML(campaign.bodyJson, [
StarterKit.configure({
- heading: { levels: [1, 2] },
+ 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 />
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 27c950b7b8..75a9032eea 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -525,6 +525,9 @@ importers:
'@tiptap/extension-image':
specifier: ^3.0.9
version: 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))
+ '@tiptap/extension-link':
+ specifier: ^3.6.6
+ version: 3.6.6(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@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]))
@@ -5558,11 +5561,11 @@ packages:
peerDependencies:
'@tiptap/core': ^3.0.9
- '@tiptap/[email protected]':
- resolution: {integrity: sha512-cOsG3vpct7/JuenxCePDj5dlaSUEe2eK/g/jlRixgW4Llx5DvG2yj8+gha4MHdCUp/MrUBR4M+NJk1dOOSKXGw==}
+ '@tiptap/[email protected]':
+ resolution: {integrity: sha512-Uq41J6WpbZkyZinbikVxLNqu2WfwUJcErTmNkPdel1gvYoQajkXZkHz2NzQ2jS9DicHHPV1cJel5f/YvX7nm5w==}
peerDependencies:
- '@tiptap/core': ^3.0.9
- '@tiptap/pm': ^3.0.9
+ '@tiptap/core': ^3.6.6
+ '@tiptap/pm': ^3.6.6
'@tiptap/[email protected]':
resolution: {integrity: sha512-K+ogk1BH/eYhsK9nSTXNdIXlxQcXzty6h1QFiZNr9XmaLk+q4NZFHR5FVz3EJ7QXyw+Gv/2FQn+T2Q/GpbMxZQ==}
@@ -19795,7 +19798,7 @@ snapshots:
dependencies:
'@tiptap/core': 3.0.9(@tiptap/[email protected])
dependencies:
'@tiptap/core': 3.0.9(@tiptap/[email protected])
'@tiptap/pm': 3.0.9
@@ -19921,7 +19924,7 @@ snapshots:
'@tiptap/extension-heading': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))
'@tiptap/extension-horizontal-rule': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/extension-italic': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))
- '@tiptap/extension-link': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
+ '@tiptap/extension-link': 3.6.6(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/extension-list': 3.0.9(@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected])
'@tiptap/extension-list-item': 3.0.9(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))
'@tiptap/extension-list-keymap': 3.0.9(@tiptap/[email protected](@tiptap/[email protected](@tiptap/[email protected]))(@tiptap/[email protected]))