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.

Fix empty message emails by rendering attachment placeholders (#4031)

Pedro Ladeira20867408c8be
diff --git a/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts b/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts
index fddc0f9e00..926302c4b2 100644
--- a/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts
+++ b/apps/web/app/(ee)/api/cron/messages/notify-partner/route.ts
@@ -54,6 +54,13 @@ export async function POST(req: Request) {
},
include: {
senderUser: true,
+ attachments: {
+ select: {
+ name: true,
+ size: true,
+ type: true,
+ },
+ },
},
},
users: {
@@ -114,6 +121,7 @@ export async function POST(req: Request) {
messages: [...unreadMessages].reverse().map((message) => ({
text: message.text,
createdAt: message.createdAt,
+ attachments: message.attachments,
user: message.senderUser.name
? {
name: message.senderUser.name,
diff --git a/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts b/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts
index 1b18b63ed5..77710f963b 100644
--- a/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts
+++ b/apps/web/app/(ee)/api/cron/messages/notify-program/route.ts
@@ -56,6 +56,13 @@ export async function POST(req: Request) {
},
include: {
senderPartner: true,
+ attachments: {
+ select: {
+ name: true,
+ size: true,
+ type: true,
+ },
+ },
},
},
workspace: {
@@ -120,6 +127,7 @@ export async function POST(req: Request) {
messages: [...unreadMessages].reverse().map((message) => ({
text: message.text,
createdAt: message.createdAt,
+ attachments: message.attachments,
})),
email,
}),
diff --git a/packages/email/src/components/message-attachment-placeholders.tsx b/packages/email/src/components/message-attachment-placeholders.tsx
new file mode 100644
index 0000000000..060a919efb
--- /dev/null
+++ b/packages/email/src/components/message-attachment-placeholders.tsx
@@ -0,0 +1,78 @@
+import { formatFileSize } from "@dub/utils";
+import { Column, Link, Row, Text } from "@react-email/components";
+
+const ATTACHMENT_MIME_TYPE_LABELS: Record<string, string> = {
+ "application/pdf": "PDF",
+ "image/png": "PNG",
+ "image/jpeg": "JPG",
+ "image/webp": "WEBP",
+};
+
+const ATTACHMENT_MIME_TYPE_COLOR: Record<string, string> = {
+ "application/pdf": "#e11d48",
+ "image/png": "#2563eb",
+ "image/jpeg": "#3b82f6",
+ "image/webp": "#3b82f6",
+};
+
+function getAttachmentTypeLabel(mimeType: string): string {
+ return (
+ ATTACHMENT_MIME_TYPE_LABELS[mimeType] ||
+ mimeType.split("/").pop()?.toUpperCase() ||
+ "FILE"
+ );
+}
+
+export type MessageAttachmentPlaceholder = {
+ name: string;
+ size: number;
+ type: string;
+};
+
+export function MessageAttachmentPlaceholders({
+ attachments,
+ href,
+}: {
+ attachments: MessageAttachmentPlaceholder[];
+ href: string;
+}) {
+ if (attachments.length === 0) {
+ return null;
+ }
+
+ return (
+ <>
+ {attachments.map((attachment, idx) => (
+ <Link
+ key={`${attachment.name}-${idx}`}
+ href={href}
+ className="mt-2 block no-underline"
+ >
+ <Row className="rounded-lg border border-solid border-neutral-200 bg-neutral-50 p-1 pr-3">
+ <Column style={{ width: 48 }}>
+ <div
+ className="flex size-12 flex-col items-center justify-center rounded-md text-[10px] font-semibold uppercase text-white"
+ style={{
+ backgroundColor:
+ ATTACHMENT_MIME_TYPE_COLOR[attachment.type] || "#737373",
+ }}
+ >
+ <Text className="my-0 text-[10px] font-semibold uppercase text-white">
+ {getAttachmentTypeLabel(attachment.type)}
+ </Text>
+ </div>
+ </Column>
+ <Column className="pl-3">
+ <Text className="my-0 truncate text-sm font-medium text-neutral-900">
+ {attachment.name}
+ </Text>
+ <Text className="my-0 text-xs font-medium text-neutral-500">
+ {formatFileSize(attachment.size, 1)}
+ </Text>
+ </Column>
+ </Row>
+ </Link>
+ ))}
+ </>
+ );
+}
diff --git a/packages/email/src/templates/new-message-from-partner.tsx b/packages/email/src/templates/new-message-from-partner.tsx
index 4b68a9cc5d..dbe2d25228 100644
--- a/packages/email/src/templates/new-message-from-partner.tsx
+++ b/packages/email/src/templates/new-message-from-partner.tsx
@@ -16,6 +16,10 @@ import {
Text,
} from "@react-email/components";
import { Footer } from "../components/footer";
+import {
+ MessageAttachmentPlaceholder,
+ MessageAttachmentPlaceholders,
+} from "../components/message-attachment-placeholders";
const MAX_DISPLAYED_MESSAGES = 3;
@@ -28,8 +32,24 @@ export default function NewMessageFromPartner({
},
messages = [
{
- text: "Am I eligible for that one _bounty_?",
+ text: "",
createdAt: new Date(Date.now() - 1000 * 60 * 5),
+ attachments: [
+ {
+ name: "screenshot.png",
+ size: 245760,
+ type: "image/png",
+ },
+ {
+ name: "proof-of-work.webp",
+ size: 102400,
+ type: "image/webp",
+ },
+ ],
+ },
+ {
+ text: "Am I eligible for that one _bounty_?",
+ createdAt: new Date(),
},
],
email = "[email protected]",
@@ -43,9 +63,12 @@ export default function NewMessageFromPartner({
messages: {
text: string;
createdAt: Date;
+ attachments?: MessageAttachmentPlaceholder[];
}[];
email: string;
}) {
+ const threadUrl = `https://app.dub.co/${workspaceSlug}/program/messages/${partner.id}`;
+
return (
<Html>
<Head />
@@ -74,7 +97,7 @@ export default function NewMessageFromPartner({
<Section className="rounded-xl border border-solid border-neutral-200 p-6">
{messages.slice(0, MAX_DISPLAYED_MESSAGES).map((message, idx) => (
- <Row className={idx > 0 ? "pt-3" : ""}>
+ <Row key={idx} className={idx > 0 ? "pt-3" : ""}>
<Column className="align-bottom">
<Img
src={partner.image || `${OG_AVATAR_URL}${partner.id}`}
@@ -85,19 +108,25 @@ export default function NewMessageFromPartner({
/>
</Column>
<Column className="w-full pl-2">
- <Markdown
- markdownCustomStyles={{ link: { color: "black" } }}
- markdownContainerStyles={{
- borderRadius: 8,
- background: "#f5f5f5",
- padding: "1px 16px",
- fontSize: 14,
- lineHeight: "20px",
- color: "#262626",
- }}
- >
- {message.text}
- </Markdown>
+ {message.text ? (
+ <Markdown
+ markdownCustomStyles={{ link: { color: "black" } }}
+ markdownContainerStyles={{
+ borderRadius: 8,
+ background: "#f5f5f5",
+ padding: "1px 16px",
+ fontSize: 14,
+ lineHeight: "20px",
+ color: "#262626",
+ }}
+ >
+ {message.text}
+ </Markdown>
+ ) : null}
+ <MessageAttachmentPlaceholders
+ attachments={message.attachments ?? []}
+ href={threadUrl}
+ />
</Column>
</Row>
))}
@@ -109,7 +138,7 @@ export default function NewMessageFromPartner({
)}
<Link
className="mt-4 block rounded-lg bg-neutral-900 px-6 py-3 text-center text-[13px] font-medium text-white no-underline"
- href={`https://app.dub.co/${workspaceSlug}/program/messages/${partner.id}`}
+ href={threadUrl}
>
View in Dub
</Link>
diff --git a/packages/email/src/templates/new-message-from-program.tsx b/packages/email/src/templates/new-message-from-program.tsx
index 70360825d9..8a3e187694 100644
--- a/packages/email/src/templates/new-message-from-program.tsx
+++ b/packages/email/src/templates/new-message-from-program.tsx
@@ -16,6 +16,10 @@ import {
Text,
} from "@react-email/components";
import { Footer } from "../components/footer";
+import {
+ MessageAttachmentPlaceholder,
+ MessageAttachmentPlaceholders,
+} from "../components/message-attachment-placeholders";
const MAX_DISPLAYED_MESSAGES = 3;
@@ -35,7 +39,32 @@ export default function NewMessageFromProgram({
},
},
{
- text: "You're all set now!",
+ text: "",
+ createdAt: new Date(Date.now() - 1000 * 60 * 2),
+ user: {
+ name: "Brendan Urie",
+ image: null,
+ },
+ attachments: [
+ {
+ name: "acme-logo.png",
+ size: 1699,
+ type: "image/png",
+ },
+ {
+ name: "Acme - New Branding.pdf",
+ size: 833121,
+ type: "application/pdf",
+ },
+ {
+ name: "acme-thumbnail.jpg",
+ size: 468141,
+ type: "image/jpeg",
+ },
+ ],
+ },
+ {
+ text: "Here you go! Let me know if you have any questions 🤲",
createdAt: new Date(),
user: {
name: "Brendan Urie",
@@ -57,9 +86,12 @@ export default function NewMessageFromProgram({
name: string;
image: string | null;
};
+ attachments?: MessageAttachmentPlaceholder[];
}[];
email: string;
}) {
+ const threadUrl = `https://partners.dub.co/messages/${program.slug}`;
+
return (
<Html>
<Head />
@@ -99,7 +131,7 @@ export default function NewMessageFromProgram({
<Section className="rounded-xl border border-solid border-neutral-200 p-6">
{messages.slice(0, MAX_DISPLAYED_MESSAGES).map((message, idx) => (
- <Row className={idx > 0 ? "pt-3" : ""}>
+ <Row key={idx} className={idx > 0 ? "pt-3" : ""}>
<Column className="align-bottom">
<Img
src={
@@ -123,19 +155,25 @@ export default function NewMessageFromProgram({
minute: "numeric",
})}
</Text>
- <Markdown
- markdownCustomStyles={{ link: { color: "black" } }}
- markdownContainerStyles={{
- borderRadius: 8,
- background: "#f5f5f5",
- padding: "1px 16px",
- fontSize: 14,
- lineHeight: "20px",
- color: "#262626",
- }}
- >
- {message.text}
- </Markdown>
+ {message.text ? (
+ <Markdown
+ markdownCustomStyles={{ link: { color: "black" } }}
+ markdownContainerStyles={{
+ borderRadius: 8,
+ background: "#f5f5f5",
+ padding: "1px 16px",
+ fontSize: 14,
+ lineHeight: "20px",
+ color: "#262626",
+ }}
+ >
+ {message.text}
+ </Markdown>
+ ) : null}
+ <MessageAttachmentPlaceholders
+ attachments={message.attachments ?? []}
+ href={threadUrl}
+ />
</Column>
</Row>
))}
@@ -147,7 +185,7 @@ export default function NewMessageFromProgram({
)}
<Link
className="mt-4 block rounded-lg bg-neutral-900 px-6 py-3 text-center text-[13px] font-medium text-white no-underline"
- href={`https://partners.dub.co/messages/${program.slug}`}
+ href={threadUrl}
>
Reply in Dub
</Link>