Skip to content

Commit 61a5d65

Browse files
committed
fix(web): preserve attachment labels, snapshot details, and copied context
1 parent 02e2ac3 commit 61a5d65

4 files changed

Lines changed: 49 additions & 54 deletions

File tree

‎apps/web/src/components/chat/MessagesTimeline.browser.test.tsx‎

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ describe("MessagesTimeline agent messages", () => {
649649

650650
describe("MessagesTimeline attachments", () => {
651651
it.each(["image", "file"] as const)(
652-
"copies a wordless %s attachment only when its context record is present",
652+
"copies only matching context records for a wordless %s attachment",
653653
async (kind) => {
654654
const attachment = {
655655
type: kind,
@@ -687,7 +687,20 @@ describe("MessagesTimeline attachments", () => {
687687
<MessagesTimeline
688688
{...baseProps()}
689689
timelineEntries={[
690-
message("", { attachments: [attachment], context: { version: 1, records: [record] } }),
690+
message("", {
691+
attachments: [attachment],
692+
context: {
693+
version: 1,
694+
records: [
695+
record,
696+
{
697+
...record,
698+
contextId: ComposerContextId.make("other-context"),
699+
attachmentId: "other",
700+
},
701+
],
702+
},
703+
}),
691704
]}
692705
/>,
693706
);
@@ -704,35 +717,6 @@ describe("MessagesTimeline attachments", () => {
704717
},
705718
);
706719

707-
it("shows the local video source while its attachment is sending", async () => {
708-
using _observer = stubGlobal(
709-
"IntersectionObserver",
710-
class extends IntersectionObserver {
711-
override observe() {}
712-
},
713-
);
714-
await using screen = await renderTimeline([
715-
message("", {
716-
attachments: [
717-
{
718-
type: "file",
719-
id: "pending-video",
720-
name: "clip.mp4",
721-
mimeType: "video/mp4",
722-
sizeBytes: 100,
723-
downloadable: false,
724-
previewUrl: "data:video/mp4;base64,",
725-
},
726-
],
727-
}),
728-
]);
729-
await vi.waitFor(() => {
730-
expect(screen.container.querySelector("video")?.getAttribute("src")).toBe(
731-
"data:video/mp4;base64,",
732-
);
733-
});
734-
});
735-
736720
it.each(["loadedmetadata", "loadeddata", "canplay"])(
737721
"plays audio with unknown duration after %s and updates the total later",
738722
async (readyEvent) => {

‎apps/web/src/components/chat/TimelineUserMessageRow.logic.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { type ChatImageAttachment } from "../../types";
1+
import { type ChatFileAttachment, type ChatImageAttachment } from "../../types";
22
import type { ComposerContextRecord, KnownComposerContextRecord } from "@t3tools/contracts";
33
import { asKnownContextRecord } from "~/lib/composerContextRecords";
44

5+
export function userMessageFileLabel(file: Pick<ChatFileAttachment, "name" | "source">) {
6+
return file.source?._tag === "pasted-text" ? "Pasted text" : file.name;
7+
}
8+
59
export function resolvePreviewAnnotationImage(input: {
610
record: Extract<KnownComposerContextRecord, { kind: "preview-annotation" }>;
711
recordsById: ReadonlyMap<string, ComposerContextRecord>;

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

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ import {
8383
TimelineRowTimestamp,
8484
} from "./TimelineRowParts";
8585
import { UserMessageContextReferenceChip } from "./UserMessageContextChips";
86-
import { resolvePreviewAnnotationImage } from "./TimelineUserMessageRow.logic";
86+
import {
87+
resolvePreviewAnnotationImage,
88+
userMessageFileLabel,
89+
} from "./TimelineUserMessageRow.logic";
8790

8891
// Drawn inside the media so dark screenshots keep an edge on the dark background.
8992
const MEDIA_EDGE_CLASS_NAME =
@@ -101,7 +104,7 @@ function UserFileCard({
101104
}) {
102105
const ctx = use(TimelineRowCtx);
103106
const pastedText = file.source?._tag === "pasted-text";
104-
const label = pastedText ? "Pasted text" : file.name;
107+
const label = userMessageFileLabel(file);
105108
const extension = /\.([a-z0-9]{1,8})$/i.exec(file.name)?.[1]?.toUpperCase() ?? "File";
106109
const identity = (
107110
<>
@@ -524,8 +527,19 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
524527
.split(/\r?\n/)
525528
.find((line) => line.trim().length > 0)
526529
?.trim() ?? "";
530+
const mediaCopyRecords = mediaOnly
531+
? resolvedContext.records.flatMap((record) => {
532+
const known = asKnownContextRecord(record);
533+
return (known?.kind === "image" || known?.kind === "file") &&
534+
(regularImages.some((image) => image.id === known.attachmentId) ||
535+
userVideos.some((file) => file.id === known.attachmentId))
536+
? [known]
537+
: [];
538+
})
539+
: [];
540+
const copyRecords = mediaOnly ? mediaCopyRecords : resolvedContext.records;
527541
const contextClipboardFragment =
528-
resolvedContext.records.length === 0
542+
copyRecords.length === 0
529543
? null
530544
: encodeComposerContextFragment({
531545
version: 1,
@@ -534,22 +548,10 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
534548
...(ctx.threadRef ? { threadId: ctx.threadRef.threadId } : {}),
535549
messageId: row.message.id,
536550
},
537-
records: resolvedContext.records,
551+
records: copyRecords,
538552
});
539553
const copyText =
540-
resolvedContext.text ||
541-
(mediaOnly && contextClipboardFragment
542-
? resolvedContext.records
543-
.flatMap((record) => {
544-
const known = asKnownContextRecord(record);
545-
return (known?.kind === "image" || known?.kind === "file") &&
546-
(regularImages.some((image) => image.id === known.attachmentId) ||
547-
userVideos.some((file) => file.id === known.attachmentId))
548-
? [formatComposerContextReference(known)]
549-
: [];
550-
})
551-
.join("\n")
552-
: "");
554+
resolvedContext.text || mediaCopyRecords.map(formatComposerContextReference).join("\n");
553555
const onBodyCopyCapture = (event: React.ClipboardEvent<HTMLDivElement>) => {
554556
if (resolvedContext.records.length === 0 || !event.clipboardData) return;
555557
const selection = window.getSelection();
@@ -660,7 +662,10 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
660662
? cn(
661663
"col-span-2",
662664
standaloneMedia
663-
? cn(MEDIA_EDGE_CLASS_NAME, "justify-self-end overflow-hidden rounded-2xl")
665+
? cn(
666+
MEDIA_EDGE_CLASS_NAME,
667+
"min-h-16 justify-self-end overflow-hidden rounded-2xl",
668+
)
664669
: SNAP_SHOT_ATTACHMENT_FRAME_CLASS,
665670
)
666671
: loneImage
@@ -901,7 +906,7 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
901906
<div className="mb-2 flex flex-col gap-1">
902907
{unchippedFiles.map((file) => {
903908
const pastedText = file.source?._tag === "pasted-text";
904-
const label = pastedText ? "Pasted text" : file.name;
909+
const label = userMessageFileLabel(file);
905910
const fileIdentity = (
906911
<>
907912
<PierreEntryIcon

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
type ReviewCommentContext,
6161
} from "../../reviewCommentContext";
6262
import { TimelineRowCtx } from "./timelineRowContext";
63+
import { userMessageFileLabel } from "./TimelineUserMessageRow.logic";
6364

6465
function UserMessageMentionChip(props: {
6566
record: Extract<KnownComposerContextRecord, { kind: "mention" }>;
@@ -372,12 +373,13 @@ const userMessageContextPresentationRegistry = createContextPresentationRegistry
372373
}
373374
const attachment = context.attachment;
374375
const isVideo = isVideoAttachment(attachment);
376+
const label = userMessageFileLabel(attachment);
375377
const disabled =
376378
attachment.downloadable === false && (!isVideo || attachment.previewUrl === undefined);
377379
const size = formatAttachmentSize(record.sizeBytes);
378380
return (
379381
<FileChip
380-
name={record.name}
382+
name={label}
381383
svgPreview={
382384
isSvgAttachment(attachment)
383385
? { environmentId: context.environmentId, attachmentId: attachment.id }
@@ -387,12 +389,12 @@ const userMessageContextPresentationRegistry = createContextPresentationRegistry
387389
isVideo={isVideo}
388390
theme={context.resolvedTheme}
389391
disabled={disabled}
390-
accessibleLabel={`${isVideo ? "Video" : "File"} attachment, ${record.name}, ${size}`}
392+
accessibleLabel={`${isVideo ? "Video" : "File"} attachment, ${label}, ${size}`}
391393
copyMarkdown={context.copyMarkdown}
392394
onOpen={() =>
393395
isVideo ? context.onExpandVideo(attachment) : context.onOpenFile(attachment)
394396
}
395-
tooltip={`${record.name}\n${size}`}
397+
tooltip={`${label}\n${size}`}
396398
/>
397399
);
398400
},

0 commit comments

Comments
 (0)