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.

message attachment order and align attachments to sender side

Pedro Ladeira2ed4c7927792
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 d5518a3530..d7eb2191b5 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
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import NewMessageFromProgram from "@dub/email/templates/new-message-from-program";
@@ -55,6 +56,7 @@ export async function POST(req: Request) {
include: {
senderUser: true,
attachments: {
+ orderBy: messageAttachmentsOrderBy,
select: {
name: true,
size: true,
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 6300694bce..b862b30e10 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
@@ -1,6 +1,7 @@
import { createId } from "@/lib/api/create-id";
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { sendBatchEmail } from "@dub/email";
import NewMessageFromPartner from "@dub/email/templates/new-message-from-partner";
@@ -57,6 +58,7 @@ export async function POST(req: Request) {
include: {
senderPartner: true,
attachments: {
+ orderBy: messageAttachmentsOrderBy,
select: {
name: true,
size: true,
diff --git a/apps/web/app/(ee)/api/intercom/webhook/process/conversation-admin-replied.ts b/apps/web/app/(ee)/api/intercom/webhook/process/conversation-admin-replied.ts
index 27021e08c9..813187abd0 100644
--- a/apps/web/app/(ee)/api/intercom/webhook/process/conversation-admin-replied.ts
+++ b/apps/web/app/(ee)/api/intercom/webhook/process/conversation-admin-replied.ts
@@ -10,7 +10,10 @@ import {
intercomCredentialsSchema,
} from "@/lib/integrations/intercom/schema";
import { PROGRAM_ALLOWED_ATTACHMENT_TYPES } from "@/lib/messages/constants";
-import { sanitizeFileName } from "@/lib/messages/utils";
+import {
+ mapMessageAttachmentsForCreate,
+ sanitizeFileName,
+} from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import {
@@ -112,13 +115,7 @@ export async function handleConversationAdminReplied({
text: originalMessage,
...(storedAttachments.length > 0 && {
attachments: {
- create: storedAttachments.map((attachment) => ({
- id: createId({ prefix: "msa_" }),
- storageKey: attachment.storageKey,
- name: attachment.name,
- size: attachment.size,
- type: attachment.type,
- })),
+ create: mapMessageAttachmentsForCreate(storedAttachments),
},
}),
},
diff --git a/apps/web/app/(ee)/api/messages/route.ts b/apps/web/app/(ee)/api/messages/route.ts
index 7082e977ae..33b00f8296 100644
--- a/apps/web/app/(ee)/api/messages/route.ts
+++ b/apps/web/app/(ee)/api/messages/route.ts
@@ -6,6 +6,7 @@ import {
PartnerMessagesSchema,
getPartnerMessagesQuerySchema,
} from "@/lib/messages/schemas";
+import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";
@@ -47,7 +48,9 @@ export const GET = withWorkspace(
include: {
senderPartner: true,
senderUser: true,
- attachments: true,
+ attachments: {
+ orderBy: messageAttachmentsOrderBy,
+ },
},
orderBy: {
[sortBy]: sortOrder,
diff --git a/apps/web/app/(ee)/api/partner-profile/messages/route.ts b/apps/web/app/(ee)/api/partner-profile/messages/route.ts
index 163e0a3b2d..ba9a69e561 100644
--- a/apps/web/app/(ee)/api/partner-profile/messages/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/messages/route.ts
@@ -4,6 +4,7 @@ import {
ProgramMessagesSchema,
getProgramMessagesQuerySchema,
} from "@/lib/messages/schemas";
+import { messageAttachmentsOrderBy } from "@/lib/messages/utils";
import { prisma } from "@/lib/prisma";
import { NextResponse } from "next/server";
@@ -78,7 +79,9 @@ export const GET = withPartnerProfile(async ({ partner, searchParams }) => {
include: {
senderPartner: true,
senderUser: true,
- attachments: true,
+ attachments: {
+ orderBy: messageAttachmentsOrderBy,
+ },
},
orderBy: {
[sortBy]: sortOrder,
diff --git a/apps/web/lib/messages/enrich.ts b/apps/web/lib/messages/enrich.ts
index 48182f0853..7eb02c8fd4 100644
--- a/apps/web/lib/messages/enrich.ts
+++ b/apps/web/lib/messages/enrich.ts
@@ -1,12 +1,15 @@
+import { sortMessageAttachments } from "@/lib/messages/utils";
import { storage } from "@/lib/storage";
import { Message, MessageAttachment } from "@prisma/client";
export async function enrichMessage(
message: Message & { attachments: MessageAttachment[] },
) {
+ const attachments = sortMessageAttachments(message.attachments);
+
return {
...message,
- attachments: await Promise.all(message.attachments.map(enrichAttachment)),
+ attachments: await Promise.all(attachments.map(enrichAttachment)),
};
}
diff --git a/apps/web/lib/messages/message-partner.ts b/apps/web/lib/messages/message-partner.ts
index 9a532dc2c6..20a17e791c 100644
--- a/apps/web/lib/messages/message-partner.ts
+++ b/apps/web/lib/messages/message-partner.ts
@@ -14,6 +14,10 @@ import * as z from "zod/v4";
import { authActionClient } from "../actions/safe-action";
import { throwIfNoPermission } from "../actions/throw-if-no-permission";
import { MessageSchema, messagePartnerSchema } from "./schemas";
+import {
+ mapMessageAttachmentsForCreate,
+ messageAttachmentsOrderBy,
+} from "./utils";
const schema = messagePartnerSchema
.extend({
@@ -136,20 +140,16 @@ export const messagePartnerAction = authActionClient
text,
...(attachments.length > 0 && {
attachments: {
- create: attachments.map((att) => ({
- id: createId({ prefix: "msa_" }),
- storageKey: att.storageKey,
- name: att.name,
- size: att.size,
- type: att.type,
- })),
+ create: mapMessageAttachmentsForCreate(attachments),
},
}),
},
include: {
senderUser: true,
senderPartner: true,
- attachments: true,
+ attachments: {
+ orderBy: messageAttachmentsOrderBy,
+ },
},
});
diff --git a/apps/web/lib/messages/message-program.ts b/apps/web/lib/messages/message-program.ts
index 4e478d74fa..d040dc9422 100644
--- a/apps/web/lib/messages/message-program.ts
+++ b/apps/web/lib/messages/message-program.ts
@@ -8,6 +8,10 @@ import { APP_DOMAIN_WITH_NGROK, INTERCOM_INTEGRATION_ID } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { authPartnerActionClient } from "../actions/safe-action";
import { MessageSchema, messageProgramSchema } from "./schemas";
+import {
+ mapMessageAttachmentsForCreate,
+ messageAttachmentsOrderBy,
+} from "./utils";
const schema = messageProgramSchema.refine(
(data) => data.text.trim().length > 0 || data.attachments.length > 0,
@@ -81,20 +85,16 @@ export const messageProgramAction = authPartnerActionClient
text,
...(attachments.length > 0 && {
attachments: {
- create: attachments.map((att) => ({
- id: createId({ prefix: "msa_" }),
- storageKey: att.storageKey,
- name: att.name,
- size: att.size,
- type: att.type,
- })),
+ create: mapMessageAttachmentsForCreate(attachments),
},
}),
},
include: {
senderUser: true,
senderPartner: true,
- attachments: true,
+ attachments: {
+ orderBy: messageAttachmentsOrderBy,
+ },
},
});
diff --git a/apps/web/lib/messages/utils.ts b/apps/web/lib/messages/utils.ts
index 1af9b366f8..edbf8ddc75 100644
--- a/apps/web/lib/messages/utils.ts
+++ b/apps/web/lib/messages/utils.ts
@@ -1,8 +1,42 @@
+import { createId } from "@/lib/api/create-id";
import {
ATTACHMENT_MIME_TYPE_LABELS,
PREVIEWABLE_IMAGE_TYPES,
} from "./constants";
+export const messageAttachmentsOrderBy = { createdAt: "asc" } as const;
+
+type MessageAttachmentInput = {
+ storageKey: string;
+ name: string;
+ size: number;
+ type: string;
+};
+
+export function mapMessageAttachmentsForCreate(
+ attachments: MessageAttachmentInput[],
+) {
+ const base = Date.now();
+
+ return attachments.map((att, index) => ({
+ id: createId({ prefix: "msa_" }),
+ storageKey: att.storageKey,
+ name: att.name,
+ size: att.size,
+ type: att.type,
+ createdAt: new Date(base + index),
+ }));
+}
+
+export function sortMessageAttachments<
+ T extends { createdAt: Date; id: string },
+>(attachments: T[]) {
+ return [...attachments].sort(
+ (a, b) =>
+ a.createdAt.getTime() - b.createdAt.getTime() || a.id.localeCompare(b.id),
+ );
+}
+
// Normalize a user-supplied file name
export function sanitizeFileName(name: string): string {
return name
diff --git a/apps/web/ui/messages/message-attachments.tsx b/apps/web/ui/messages/message-attachments.tsx
index 3e7fc954a4..e966467aa2 100644
--- a/apps/web/ui/messages/message-attachments.tsx
+++ b/apps/web/ui/messages/message-attachments.tsx
@@ -1,6 +1,10 @@
"use client";
-import { getAttachmentTypeLabel } from "@/lib/messages/utils";
+import {
+ getAttachmentTypeLabel,
+ isPreviewableImageType,
+ sortMessageAttachments,
+} from "@/lib/messages/utils";
import { MessageAttachment } from "@/lib/types";
import { formatFileSize } from "@dub/utils";
import { cn } from "@dub/utils/src";
@@ -36,82 +40,105 @@ export const ATTACHMENT_MIME_TYPE_COLOR: Record<string, string> = {
"image/webp": "bg-blue-500",
};
-export function MessageImageAttachments({
+export function MessageAttachmentsList({
attachments,
+ isMySide,
}: {
attachments: MessageAttachment[];
+ isMySide: boolean;
}) {
+ if (attachments.length === 0) {
+ return null;
+ }
+
+ const sortedAttachments = sortMessageAttachments(attachments);
+
return (
- <div className="flex max-w-[min(100%,512px)] flex-col gap-1">
- {attachments.map((img) => (
- <div key={img.id} className="group relative">
- {img.signedUrl ? (
- <>
- <ZoomImage
- src={img.signedUrl}
- alt={img.name}
- className="max-h-64 w-full cursor-pointer rounded-lg border border-neutral-200 object-cover"
- />
- <button
- type="button"
- onClick={(e) => {
- e.stopPropagation();
- downloadFile(img.signedUrl!, img.name);
- }}
- className="absolute right-2 top-2 flex size-7 items-center justify-center rounded-md bg-white/70 text-neutral-600 opacity-0 backdrop-blur-sm transition-opacity hover:bg-white/90 group-hover:opacity-100"
- >
- <Download className="size-3.5" />
- </button>
- </>
- ) : (
- <div className="h-40 w-full animate-pulse rounded-lg bg-neutral-200" />
- )}
- </div>
- ))}
+ <div
+ className={cn(
+ "flex w-fit max-w-[min(100%,512px)] flex-col gap-1",
+ isMySide ? "self-end" : "self-start",
+ )}
+ >
+ {sortedAttachments.map((attachment) =>
+ isPreviewableImageType(attachment.type) ? (
+ <MessageImageAttachment key={attachment.id} attachment={attachment} />
+ ) : (
+ <MessageFileAttachment key={attachment.id} attachment={attachment} />
+ ),
+ )}
</div>
);
}
-export function MessageFileAttachments({
- attachments,
+function MessageImageAttachment({
+ attachment,
}: {
- attachments: MessageAttachment[];
+ attachment: MessageAttachment;
}) {
return (
- <div className="flex max-w-[min(100%,512px)] flex-col gap-2">
- {attachments.map((file) => (
- <button
- key={file.id}
- type="button"
- disabled={!file.signedUrl}
- onClick={() =>
- file.signedUrl && downloadFile(file.signedUrl, file.name)
- }
- className={cn(
- "flex items-center gap-3 rounded-lg border border-neutral-200 bg-neutral-50 p-1 pr-3 text-left transition-colors",
- file.signedUrl
- ? "hover:bg-neutral-100"
- : "cursor-default opacity-60",
- )}
- >
- <FileTypeBadge type={file.type} />
- <div className="flex min-w-0 flex-1 flex-col">
- <span className="text-content-default truncate text-sm font-medium">
- {file.name}
- </span>
- <span className="text-content-subtle text-xs font-medium">
- {formatFileSize(file.size, 1)}
- </span>
- </div>
- <div className="border-bg-subtle flex size-8 shrink-0 items-center justify-center rounded-lg border bg-white">
- <Download className="text-content-emphasis size-4 shrink-0" />
- </div>
- </button>
- ))}
+ <div className="group relative">
+ {attachment.signedUrl ? (
+ <>
+ <ZoomImage
+ src={attachment.signedUrl}
+ alt={attachment.name}
+ className="max-h-64 w-full cursor-pointer rounded-lg border border-neutral-200 object-cover"
+ />
+ <button
+ type="button"
+ onClick={(e) => {
+ e.stopPropagation();
+ downloadFile(attachment.signedUrl!, attachment.name);
+ }}
+ className="absolute right-2 top-2 flex size-7 items-center justify-center rounded-md bg-white/70 text-neutral-600 opacity-0 backdrop-blur-sm transition-opacity hover:bg-white/90 group-hover:opacity-100"
+ >
+ <Download className="size-3.5" />
+ </button>
+ </>
+ ) : (
+ <div className="h-40 w-full animate-pulse rounded-lg bg-neutral-200" />
+ )}
</div>
);
}
+function MessageFileAttachment({
+ attachment,
+}: {
+ attachment: MessageAttachment;
+}) {
+ return (
+ <button
+ type="button"
+ disabled={!attachment.signedUrl}
+ onClick={() =>
+ attachment.signedUrl &&
+ downloadFile(attachment.signedUrl, attachment.name)
+ }
+ className={cn(
+ "flex items-center gap-3 rounded-lg border border-neutral-200 bg-neutral-50 p-1 pr-3 text-left transition-colors",
+ attachment.signedUrl
+ ? "hover:bg-neutral-100"
+ : "cursor-default opacity-60",
+ )}
+ >
+ <FileTypeBadge type={attachment.type} />
+ <div className="flex min-w-0 flex-1 flex-col">
+ <span className="text-content-default truncate text-sm font-medium">
+ {attachment.name}
+ </span>
+ <span className="text-content-subtle text-xs font-medium">
+ {formatFileSize(attachment.size, 1)}
+ </span>
+ </div>
+ <div className="border-bg-subtle flex size-8 shrink-0 items-center justify-center rounded-lg border bg-white">
+ <Download className="text-content-emphasis size-4 shrink-0" />
+ </div>
+ </button>
+ );
+}
+
function FileTypeBadge({ type }: { type: string }) {
return (
<div
diff --git a/apps/web/ui/messages/messages-panel.tsx b/apps/web/ui/messages/messages-panel.tsx
index 95e5b7612a..cf209c04eb 100644
--- a/apps/web/ui/messages/messages-panel.tsx
+++ b/apps/web/ui/messages/messages-panel.tsx
@@ -1,10 +1,4 @@
-import { isPreviewableImageType } from "@/lib/messages/utils";
-import {
- Message,
- MessageAttachment,
- PartnerProps,
- ProgramProps,
-} from "@/lib/types";
+import { Message, MessageAttachment, PartnerProps, ProgramProps } from "@/lib/types";
import {
AnimatedSizeContainer,
Check2,
@@ -17,10 +11,7 @@ import { OG_AVATAR_URL, cn, formatDateTime } from "@dub/utils";
import { ChevronRight } from "lucide-react";
import { Fragment, ReactNode, useMemo, useRef, useState } from "react";
import { MessageInput, PendingAttachment } from "../shared/message-input";
-import {
- MessageFileAttachments,
- MessageImageAttachments,
-} from "./message-attachments";
+import { MessageAttachmentsList } from "./message-attachments";
import { MessageMarkdown } from "./message-markdown";
interface Sender {
@@ -260,31 +251,13 @@ export function MessagesPanel({
</div>
)}
{/* Attachments — rendered outside the bubble */}
- {(() => {
- const imageAttachments =
- message.attachments?.filter((a) =>
- isPreviewableImageType(a.type),
- ) ?? [];
- const fileAttachments =
- message.attachments?.filter(
- (a) => !isPreviewableImageType(a.type),
- ) ?? [];
-
- return (
- <>
- {imageAttachments.length > 0 && (
- <MessageImageAttachments
- attachments={imageAttachments}
- />
- )}
- {fileAttachments.length > 0 && (
- <MessageFileAttachments
- attachments={fileAttachments}
- />
- )}
- </>
- );
- })()}
+ {message.attachments &&
+ message.attachments.length > 0 && (
+ <MessageAttachmentsList
+ attachments={message.attachments}
+ isMySide={isMySide}
+ />
+ )}
</div>
</div>
)}