Skip to content
TypeScript

feat(web): files sent without text show as file cards (#27324)

maria1da41f949950
diff --git a/apps/web/src/components/chat/TimelineUserMessageRow.tsx b/apps/web/src/components/chat/TimelineUserMessageRow.tsx
index 56f4ef8217f..aee30e27d51 100644
--- a/apps/web/src/components/chat/TimelineUserMessageRow.tsx
+++ b/apps/web/src/components/chat/TimelineUserMessageRow.tsx
@@ -54,6 +54,7 @@ import { GuestMessageName, guestBubbleClassName } from "./guestMessageStyle";
import { MessageCopyButton } from "./MessageCopyButton";
import { promptLinkFor } from "../promptLinks/copyPromptLink";
import { PierreEntryIcon } from "./PierreEntryIcon";
+import { formatBytes } from "../../lib/formatBytes";
import { shouldCollapseUserMessage } from "./MessagesTimeline.logic";
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
import {
@@ -88,6 +89,68 @@ const MEDIA_EDGE_CLASS_NAME =
const BUBBLE_CHIP_CLASS_NAME =
"[&_[data-slot=context-chip]]:[--context-chip-ink:var(--context-chip-message,var(--context-chip-accent))]";
+function UserFileCard({
+ file,
+ copyMarkdown,
+}: {
+ readonly file: ChatFileAttachment;
+ readonly copyMarkdown: string | undefined;
+}) {
+ const ctx = use(TimelineRowCtx);
+ const extension = /\.([a-z0-9]{1,8})$/i.exec(file.name)?.[1]?.toUpperCase() ?? "File";
+ const identity = (
+ <>
+ <span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-background/60">
+ <PierreEntryIcon pathValue={file.name} kind="file" theme={ctx.resolvedTheme} />
+ </span>
+ <span className="min-w-0 flex-1">
+ <span className="block truncate text-sm font-medium group-hover/file:underline">
+ {file.name}
+ </span>
+ <span className="block text-xs text-muted-foreground tabular-nums">
+ {extension} · {formatBytes(file.sizeBytes)}
+ </span>
+ </span>
+ </>
+ );
+ return (
+ <div
+ data-markdown-copy={copyMarkdown}
+ className="flex w-64 max-w-full items-center gap-1 rounded-2xl bg-message py-2 ps-2 pe-1 text-message-foreground"
+ >
+ {file.downloadable === false ? (
+ <div className="flex min-w-0 flex-1 items-center gap-3">{identity}</div>
+ ) : (
+ <>
+ <button
+ type="button"
+ aria-label={`Preview ${file.name}`}
+ onClick={() => ctx.onFileOpen(file)}
+ className="group/file focus-visible:ring-ring/70 flex min-w-0 flex-1 cursor-pointer items-center gap-3 rounded-xl text-left focus-visible:ring-2 focus-visible:ring-inset focus-visible:outline-none"
+ >
+ {identity}
+ </button>
+ <Tooltip>
+ <TooltipTrigger
+ render={
+ <Button
+ size="icon-xs"
+ variant="ghost-muted"
+ aria-label={`Download ${file.name}`}
+ onClick={() => ctx.onFileDownload(file)}
+ />
+ }
+ >
+ <DownloadIcon />
+ </TooltipTrigger>
+ <TooltipPopup side="top">Download {file.name}</TooltipPopup>
+ </Tooltip>
+ </>
+ )}
+ </div>
+ );
+}
+
function UserVideoAttachment({
file,
standalone = false,
@@ -358,11 +421,22 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
const resolvedContext = resolveUserMessageContext({ ...row.message, text: userMessage.text });
const previewImages = userImages.filter((image) => image.name.startsWith("preview-annotation-"));
const revertTurnCount = row.revertTurnCount;
- const chippedAttachmentIds = new Set(
- collectComposerContextReferences(resolvedContext.text).flatMap((occurrence) => {
+ const attachmentReferences = collectComposerContextReferences(resolvedContext.text).flatMap(
+ (occurrence) => {
const record = asKnownContextRecord(resolvedContext.recordsById.get(occurrence.contextId));
- return record?.kind === "file" || record?.kind === "image" ? [record.attachmentId] : [];
- }),
+ return record?.kind === "file" || record?.kind === "image"
+ ? [
+ {
+ attachmentId: record.attachmentId,
+ contextId: occurrence.contextId,
+ source: occurrence.source,
+ },
+ ]
+ : [];
+ },
+ );
+ const chippedAttachmentIds = new Set(
+ attachmentReferences.map((reference) => reference.attachmentId),
);
const regularImages = userImages.filter((image) => !image.name.startsWith("preview-annotation-"));
const unchippedFiles = otherUserFiles.filter((file) => !chippedAttachmentIds.has(file.id));
@@ -394,6 +468,21 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
userVideos.length === 0 &&
regularImages.length > 1 &&
regularImages.every((image) => image.source?.kind !== "snap-shot");
+ // Files sent without words show as cards, not as a bubble around a name chip.
+ const fileCardsOnly =
+ !folded &&
+ userMessage.attribution !== "guest" &&
+ otherUserFiles.length > 0 &&
+ regularImages.length + userVideos.length + userAudio.length + unknownAttachments.length === 0 &&
+ replaceComposerContextReferences(resolvedContext.text, (reference) =>
+ attachmentReferences.some(
+ (attached) =>
+ attached.contextId === reference.contextId &&
+ otherUserFiles.some((file) => file.id === attached.attachmentId),
+ )
+ ? ""
+ : reference.label,
+ ).trim() === "";
const annotationRecordIds = resolvedContext.records
.filter((record) => record.kind === "preview-annotation")
.map((record) => record.contextId);
@@ -635,8 +724,8 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
BUBBLE_CHIP_CLASS_NAME,
userMessage.attribution === "guest"
? guestBubbleClassName(false)
- : !mediaOnly && "bg-message",
- !folded && !mediaOnly && "p-3",
+ : !mediaOnly && !fileCardsOnly && "bg-message",
+ !folded && !mediaOnly && !fileCardsOnly && "p-3",
folded && expanded && "w-full",
)}
>
@@ -689,7 +778,21 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
))}
</div>
) : null}
- {unchippedFiles.length > 0 || unknownAttachments.length > 0 ? (
+ {fileCardsOnly ? (
+ <div className="flex flex-col items-end gap-1" onCopyCapture={onBodyCopyCapture}>
+ {otherUserFiles.map((file) => (
+ <UserFileCard
+ key={file.id}
+ file={file}
+ copyMarkdown={
+ attachmentReferences.find((reference) => reference.attachmentId === file.id)
+ ?.source
+ }
+ />
+ ))}
+ </div>
+ ) : null}
+ {!fileCardsOnly && (unchippedFiles.length > 0 || unknownAttachments.length > 0) ? (
<div className="mb-2 flex flex-col gap-1">
{unchippedFiles.map((file) => {
const fileIdentity = (
@@ -751,15 +854,17 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
))}
</div>
) : null}
- <div onCopyCapture={onBodyCopyCapture}>
- <CollapsibleUserMessageBody
- text={resolvedContext.text}
- renderContextReference={renderContextReference}
- skills={ctx.skills}
- markdownCwd={ctx.markdownCwd}
- collapsible={!folded}
- />
- </div>
+ {fileCardsOnly ? null : (
+ <div onCopyCapture={onBodyCopyCapture}>
+ <CollapsibleUserMessageBody
+ text={resolvedContext.text}
+ renderContextReference={renderContextReference}
+ skills={ctx.skills}
+ markdownCwd={ctx.markdownCwd}
+ collapsible={!folded}
+ />
+ </div>
+ )}
</div>
) : null}
</div>