Skip to content

Commit 1da41f9

Browse files
authored
feat(web): files sent without text show as file cards (#27324)
1 parent b8397e5 commit 1da41f9

1 file changed

Lines changed: 121 additions & 16 deletions

File tree

‎apps/web/src/components/chat/TimelineUserMessageRow.tsx‎

Lines changed: 121 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import { GuestMessageName, guestBubbleClassName } from "./guestMessageStyle";
5454
import { MessageCopyButton } from "./MessageCopyButton";
5555
import { promptLinkFor } from "../promptLinks/copyPromptLink";
5656
import { PierreEntryIcon } from "./PierreEntryIcon";
57+
import { formatBytes } from "../../lib/formatBytes";
5758
import { shouldCollapseUserMessage } from "./MessagesTimeline.logic";
5859
import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
5960
import {
@@ -88,6 +89,68 @@ const MEDIA_EDGE_CLASS_NAME =
8889
const BUBBLE_CHIP_CLASS_NAME =
8990
"[&_[data-slot=context-chip]]:[--context-chip-ink:var(--context-chip-message,var(--context-chip-accent))]";
9091

92+
function UserFileCard({
93+
file,
94+
copyMarkdown,
95+
}: {
96+
readonly file: ChatFileAttachment;
97+
readonly copyMarkdown: string | undefined;
98+
}) {
99+
const ctx = use(TimelineRowCtx);
100+
const extension = /\.([a-z0-9]{1,8})$/i.exec(file.name)?.[1]?.toUpperCase() ?? "File";
101+
const identity = (
102+
<>
103+
<span className="flex size-9 shrink-0 items-center justify-center rounded-lg bg-background/60">
104+
<PierreEntryIcon pathValue={file.name} kind="file" theme={ctx.resolvedTheme} />
105+
</span>
106+
<span className="min-w-0 flex-1">
107+
<span className="block truncate text-sm font-medium group-hover/file:underline">
108+
{file.name}
109+
</span>
110+
<span className="block text-xs text-muted-foreground tabular-nums">
111+
{extension} · {formatBytes(file.sizeBytes)}
112+
</span>
113+
</span>
114+
</>
115+
);
116+
return (
117+
<div
118+
data-markdown-copy={copyMarkdown}
119+
className="flex w-64 max-w-full items-center gap-1 rounded-2xl bg-message py-2 ps-2 pe-1 text-message-foreground"
120+
>
121+
{file.downloadable === false ? (
122+
<div className="flex min-w-0 flex-1 items-center gap-3">{identity}</div>
123+
) : (
124+
<>
125+
<button
126+
type="button"
127+
aria-label={`Preview ${file.name}`}
128+
onClick={() => ctx.onFileOpen(file)}
129+
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"
130+
>
131+
{identity}
132+
</button>
133+
<Tooltip>
134+
<TooltipTrigger
135+
render={
136+
<Button
137+
size="icon-xs"
138+
variant="ghost-muted"
139+
aria-label={`Download ${file.name}`}
140+
onClick={() => ctx.onFileDownload(file)}
141+
/>
142+
}
143+
>
144+
<DownloadIcon />
145+
</TooltipTrigger>
146+
<TooltipPopup side="top">Download {file.name}</TooltipPopup>
147+
</Tooltip>
148+
</>
149+
)}
150+
</div>
151+
);
152+
}
153+
91154
function UserVideoAttachment({
92155
file,
93156
standalone = false,
@@ -358,11 +421,22 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
358421
const resolvedContext = resolveUserMessageContext({ ...row.message, text: userMessage.text });
359422
const previewImages = userImages.filter((image) => image.name.startsWith("preview-annotation-"));
360423
const revertTurnCount = row.revertTurnCount;
361-
const chippedAttachmentIds = new Set(
362-
collectComposerContextReferences(resolvedContext.text).flatMap((occurrence) => {
424+
const attachmentReferences = collectComposerContextReferences(resolvedContext.text).flatMap(
425+
(occurrence) => {
363426
const record = asKnownContextRecord(resolvedContext.recordsById.get(occurrence.contextId));
364-
return record?.kind === "file" || record?.kind === "image" ? [record.attachmentId] : [];
365-
}),
427+
return record?.kind === "file" || record?.kind === "image"
428+
? [
429+
{
430+
attachmentId: record.attachmentId,
431+
contextId: occurrence.contextId,
432+
source: occurrence.source,
433+
},
434+
]
435+
: [];
436+
},
437+
);
438+
const chippedAttachmentIds = new Set(
439+
attachmentReferences.map((reference) => reference.attachmentId),
366440
);
367441
const regularImages = userImages.filter((image) => !image.name.startsWith("preview-annotation-"));
368442
const unchippedFiles = otherUserFiles.filter((file) => !chippedAttachmentIds.has(file.id));
@@ -394,6 +468,21 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
394468
userVideos.length === 0 &&
395469
regularImages.length > 1 &&
396470
regularImages.every((image) => image.source?.kind !== "snap-shot");
471+
// Files sent without words show as cards, not as a bubble around a name chip.
472+
const fileCardsOnly =
473+
!folded &&
474+
userMessage.attribution !== "guest" &&
475+
otherUserFiles.length > 0 &&
476+
regularImages.length + userVideos.length + userAudio.length + unknownAttachments.length === 0 &&
477+
replaceComposerContextReferences(resolvedContext.text, (reference) =>
478+
attachmentReferences.some(
479+
(attached) =>
480+
attached.contextId === reference.contextId &&
481+
otherUserFiles.some((file) => file.id === attached.attachmentId),
482+
)
483+
? ""
484+
: reference.label,
485+
).trim() === "";
397486
const annotationRecordIds = resolvedContext.records
398487
.filter((record) => record.kind === "preview-annotation")
399488
.map((record) => record.contextId);
@@ -635,8 +724,8 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
635724
BUBBLE_CHIP_CLASS_NAME,
636725
userMessage.attribution === "guest"
637726
? guestBubbleClassName(false)
638-
: !mediaOnly && "bg-message",
639-
!folded && !mediaOnly && "p-3",
727+
: !mediaOnly && !fileCardsOnly && "bg-message",
728+
!folded && !mediaOnly && !fileCardsOnly && "p-3",
640729
folded && expanded && "w-full",
641730
)}
642731
>
@@ -689,7 +778,21 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
689778
))}
690779
</div>
691780
) : null}
692-
{unchippedFiles.length > 0 || unknownAttachments.length > 0 ? (
781+
{fileCardsOnly ? (
782+
<div className="flex flex-col items-end gap-1" onCopyCapture={onBodyCopyCapture}>
783+
{otherUserFiles.map((file) => (
784+
<UserFileCard
785+
key={file.id}
786+
file={file}
787+
copyMarkdown={
788+
attachmentReferences.find((reference) => reference.attachmentId === file.id)
789+
?.source
790+
}
791+
/>
792+
))}
793+
</div>
794+
) : null}
795+
{!fileCardsOnly && (unchippedFiles.length > 0 || unknownAttachments.length > 0) ? (
693796
<div className="mb-2 flex flex-col gap-1">
694797
{unchippedFiles.map((file) => {
695798
const fileIdentity = (
@@ -751,15 +854,17 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
751854
))}
752855
</div>
753856
) : null}
754-
<div onCopyCapture={onBodyCopyCapture}>
755-
<CollapsibleUserMessageBody
756-
text={resolvedContext.text}
757-
renderContextReference={renderContextReference}
758-
skills={ctx.skills}
759-
markdownCwd={ctx.markdownCwd}
760-
collapsible={!folded}
761-
/>
762-
</div>
857+
{fileCardsOnly ? null : (
858+
<div onCopyCapture={onBodyCopyCapture}>
859+
<CollapsibleUserMessageBody
860+
text={resolvedContext.text}
861+
renderContextReference={renderContextReference}
862+
skills={ctx.skills}
863+
markdownCwd={ctx.markdownCwd}
864+
collapsible={!folded}
865+
/>
866+
</div>
867+
)}
763868
</div>
764869
) : null}
765870
</div>

0 commit comments

Comments
 (0)