Skip to content

Commit aaae62d

Browse files
committed
feat(web): images and videos sent with text sit above the bubble as their own shape
1 parent f012fa2 commit aaae62d

1 file changed

Lines changed: 158 additions & 106 deletions

File tree

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

Lines changed: 158 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,13 @@ const MEDIA_EDGE_CLASS_NAME =
8888
const BUBBLE_CHIP_CLASS_NAME =
8989
"[&_[data-slot=context-chip]]:[--context-chip-ink:var(--context-chip-message,var(--context-chip-accent))]";
9090

91-
function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) {
91+
function UserVideoAttachment({
92+
file,
93+
standalone = false,
94+
}: {
95+
readonly file: ChatFileAttachment;
96+
readonly standalone?: boolean;
97+
}) {
9298
const ctx = use(TimelineRowCtx);
9399
// Row updates rebuild attachment objects, so the asset follows only the
94100
// fields it reads.
@@ -105,13 +111,20 @@ function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) {
105111

106112
if (asset === null && src === null) {
107113
return (
108-
<div className="flex aspect-[4/3] w-full items-center justify-center rounded-lg border border-border/80 bg-black px-2 py-3 text-center text-2xs text-white/70">
114+
<div
115+
className={cn(
116+
"flex aspect-[4/3] w-full items-center justify-center bg-black px-2 py-3 text-center text-2xs text-white/70",
117+
standalone
118+
? cn(MEDIA_EDGE_CLASS_NAME, "rounded-2xl")
119+
: "rounded-lg border border-border/80",
120+
)}
121+
>
109122
{file.name}
110123
</div>
111124
);
112125
}
113126

114-
return (
127+
const player = (
115128
<MediaVideoPlayer
116129
src={src}
117130
sourceFailed={
@@ -124,12 +137,23 @@ function UserVideoAttachment({ file }: { readonly file: ChatFileAttachment }) {
124137
if (preview) ctx.onImageExpand(preview);
125138
}}
126139
className="block aspect-[4/3] w-full"
127-
videoClassName="aspect-auto size-full rounded-lg border border-border/80"
128-
stateClassName="aspect-auto min-h-full rounded-lg border border-border/80 bg-black text-white"
140+
videoClassName={cn(
141+
"aspect-auto size-full",
142+
!standalone && "rounded-lg border border-border/80",
143+
)}
144+
stateClassName={cn(
145+
"aspect-auto min-h-full bg-black text-white",
146+
!standalone && "rounded-lg border border-border/80",
147+
)}
129148
onRetry={asset ? refreshAssetUrl : undefined}
130149
actionsSource={asset ? { kind: "video", name: file.name, src, asset } : undefined}
131150
/>
132151
);
152+
return standalone ? (
153+
<div className={cn(MEDIA_EDGE_CLASS_NAME, "overflow-hidden rounded-2xl")}>{player}</div>
154+
) : (
155+
player
156+
);
133157
}
134158

135159
export function UserMessageRow({ row }: { row: Extract<TimelineRow, { kind: "message" }> }) {
@@ -351,15 +375,22 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
351375
unchippedFiles.length === 0 &&
352376
unknownAttachments.length === 0 &&
353377
regularImages.length + userVideos.length > 0;
378+
// Media sent with words sits above the bubble as its own shape, like a chat app.
379+
const mediaAboveText =
380+
!folded &&
381+
userMessage.attribution !== "guest" &&
382+
!mediaOnly &&
383+
regularImages.length + userVideos.length > 0;
384+
const standaloneMedia = mediaOnly || mediaAboveText;
354385
// A lone image keeps its own shape; a 4:3 crop hides most of a wide screenshot.
355386
const loneImage =
356-
mediaOnly &&
387+
standaloneMedia &&
357388
userVideos.length === 0 &&
358389
regularImages.length === 1 &&
359390
regularImages[0]!.source?.kind !== "snap-shot";
360-
// Several images sent without words share one rounded shape, split by hairlines.
391+
// Several images share one rounded shape, split by hairlines.
361392
const collage =
362-
mediaOnly &&
393+
standaloneMedia &&
363394
userVideos.length === 0 &&
364395
regularImages.length > 1 &&
365396
regularImages.every((image) => image.source?.kind !== "snap-shot");
@@ -465,6 +496,107 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
465496
);
466497
};
467498

499+
const mediaBlock =
500+
regularImages.length > 0 || userVideos.length > 0 ? (
501+
<div
502+
className={cn(
503+
"grid",
504+
!standaloneMedia
505+
? "mb-2 max-w-[210px] grid-cols-2 gap-2"
506+
: loneImage
507+
? "max-w-full"
508+
: collage
509+
? cn(
510+
MEDIA_EDGE_CLASS_NAME,
511+
"w-80 max-w-full gap-px overflow-hidden rounded-2xl bg-black/9 dark:bg-white/12",
512+
regularImages.length === 2
513+
? "auto-rows-[12rem] grid-cols-2"
514+
: regularImages.length === 3
515+
? "auto-rows-[calc(6rem-0.5px)] grid-cols-[3fr_2fr]"
516+
: "auto-rows-[6rem] grid-cols-2",
517+
)
518+
: regularImages.length + userVideos.length === 1
519+
? "w-80 max-w-full"
520+
: "w-80 max-w-full grid-cols-2 gap-2",
521+
)}
522+
>
523+
{regularImages.map((image, index) => (
524+
<div
525+
key={image.id}
526+
className={cn(
527+
"bg-background/70",
528+
image.source?.kind === "snap-shot" && image.previewUrl
529+
? cn(
530+
SNAP_SHOT_ATTACHMENT_FRAME_CLASS,
531+
"col-span-2",
532+
standaloneMedia && "justify-self-end",
533+
)
534+
: loneImage
535+
? cn(MEDIA_EDGE_CLASS_NAME, "overflow-hidden rounded-2xl")
536+
: collage
537+
? cn(
538+
"overflow-hidden bg-background",
539+
regularImages.length === 3 && index === 0 && "row-span-2",
540+
regularImages.length > 3 &&
541+
regularImages.length % 2 === 1 &&
542+
index === regularImages.length - 1 &&
543+
"col-span-2",
544+
)
545+
: standaloneMedia
546+
? cn(MEDIA_EDGE_CLASS_NAME, "aspect-[4/3] overflow-hidden rounded-2xl")
547+
: "aspect-[4/3] overflow-hidden rounded-lg border border-border/80",
548+
)}
549+
>
550+
{image.previewUrl ? (
551+
<button
552+
type="button"
553+
className="block h-full w-full cursor-zoom-in"
554+
aria-label={`Preview ${image.name}`}
555+
onClick={() => {
556+
const preview = buildExpandedImagePreview(regularImages, image.id);
557+
if (!preview) return;
558+
ctx.onImageExpand(preview);
559+
}}
560+
>
561+
{/* Isolate the thumbnail raster from nearby activity repaints in Chromium. */}
562+
<AttachmentImage
563+
name={image.name}
564+
mimeType={image.mimeType}
565+
src={image.previewUrl}
566+
alt={image.name}
567+
decoding="async"
568+
loading="lazy"
569+
className={cn(
570+
"block transform-gpu",
571+
// A fixed height keeps the row from growing when the image loads.
572+
loneImage
573+
? "h-48 w-auto min-w-24 max-w-full object-cover"
574+
: "size-full object-cover",
575+
)}
576+
/>
577+
</button>
578+
) : (
579+
<div
580+
className={cn(
581+
"flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-2xs text-muted-foreground/70",
582+
loneImage && "h-48 w-64 max-w-full",
583+
collage && "h-full",
584+
)}
585+
>
586+
{image.name}
587+
</div>
588+
)}
589+
{image.previewUrl && image.source?.kind === "snap-shot" ? (
590+
<SnapShotAttachmentDetails source={image.source} />
591+
) : null}
592+
</div>
593+
))}
594+
{userVideos.map((file) => (
595+
<UserVideoAttachment key={file.id} file={file} standalone={standaloneMedia} />
596+
))}
597+
</div>
598+
) : null;
599+
468600
return (
469601
<div
470602
className="group flex flex-col items-end gap-1"
@@ -489,9 +621,17 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
489621
!row.continuesSteer ? (
490622
<UserMessageIntentMarker intent={row.message.inputIntent} />
491623
) : null}
624+
{mediaAboveText ? (
625+
<div className="flex max-w-[80%] justify-end">
626+
{/* The heading leads the media so screen readers reach it before the images. */}
627+
<MessageAuthorHeading>You</MessageAuthorHeading>
628+
{mediaBlock}
629+
</div>
630+
) : null}
492631
<div
493632
className={cn(
494633
"relative min-w-0 max-w-[80%] rounded-2xl text-message-foreground",
634+
mediaAboveText && "rounded-tr-md",
495635
BUBBLE_CHIP_CLASS_NAME,
496636
userMessage.attribution === "guest"
497637
? guestBubbleClassName(false)
@@ -501,13 +641,15 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
501641
)}
502642
>
503643
{/* Outside the fold so a collapsed message still has a heading to navigate to. */}
504-
<MessageAuthorHeading>
505-
{folded
506-
? senderLabel
507-
: userMessage.guestName === undefined
508-
? "You"
509-
: `${userMessage.guestName} (guest)`}
510-
</MessageAuthorHeading>
644+
{mediaAboveText ? null : (
645+
<MessageAuthorHeading>
646+
{folded
647+
? senderLabel
648+
: userMessage.guestName === undefined
649+
? "You"
650+
: `${userMessage.guestName} (guest)`}
651+
</MessageAuthorHeading>
652+
)}
511653
{folded ? (
512654
<FoldedMessageHeader
513655
icon={MessagesSquareIcon}
@@ -527,97 +669,7 @@ function UserTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "message"
527669
folded && !ctx.findActive && "max-h-96 overflow-auto",
528670
)}
529671
>
530-
{(regularImages.length > 0 || userVideos.length > 0) && (
531-
<div
532-
className={cn(
533-
"grid",
534-
!mediaOnly
535-
? "mb-2 max-w-[210px] grid-cols-2 gap-2"
536-
: loneImage
537-
? "max-w-full"
538-
: collage
539-
? cn(
540-
MEDIA_EDGE_CLASS_NAME,
541-
"w-80 max-w-full gap-px overflow-hidden rounded-2xl bg-black/9 dark:bg-white/12",
542-
regularImages.length === 2
543-
? "auto-rows-[12rem] grid-cols-2"
544-
: regularImages.length === 3
545-
? "auto-rows-[calc(6rem-0.5px)] grid-cols-[3fr_2fr]"
546-
: "auto-rows-[6rem] grid-cols-2",
547-
)
548-
: "w-80 max-w-full grid-cols-2 gap-2",
549-
)}
550-
>
551-
{regularImages.map((image, index) => (
552-
<div
553-
key={image.id}
554-
className={cn(
555-
"bg-background/70",
556-
image.source?.kind === "snap-shot" && image.previewUrl
557-
? cn(SNAP_SHOT_ATTACHMENT_FRAME_CLASS, "col-span-2")
558-
: loneImage
559-
? cn(MEDIA_EDGE_CLASS_NAME, "overflow-hidden rounded-2xl")
560-
: collage
561-
? cn(
562-
"overflow-hidden bg-background",
563-
regularImages.length === 3 && index === 0 && "row-span-2",
564-
regularImages.length > 3 &&
565-
regularImages.length % 2 === 1 &&
566-
index === regularImages.length - 1 &&
567-
"col-span-2",
568-
)
569-
: "aspect-[4/3] overflow-hidden rounded-lg border border-border/80",
570-
)}
571-
>
572-
{image.previewUrl ? (
573-
<button
574-
type="button"
575-
className="block h-full w-full cursor-zoom-in"
576-
aria-label={`Preview ${image.name}`}
577-
onClick={() => {
578-
const preview = buildExpandedImagePreview(regularImages, image.id);
579-
if (!preview) return;
580-
ctx.onImageExpand(preview);
581-
}}
582-
>
583-
{/* Isolate the thumbnail raster from nearby activity repaints in Chromium. */}
584-
<AttachmentImage
585-
name={image.name}
586-
mimeType={image.mimeType}
587-
src={image.previewUrl}
588-
alt={image.name}
589-
decoding="async"
590-
loading="lazy"
591-
className={cn(
592-
"block transform-gpu",
593-
// A fixed height keeps the row from growing when the image loads.
594-
loneImage
595-
? "h-48 w-auto min-w-24 max-w-full object-cover"
596-
: "size-full object-cover",
597-
)}
598-
/>
599-
</button>
600-
) : (
601-
<div
602-
className={cn(
603-
"flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-2xs text-muted-foreground/70",
604-
loneImage && "h-48 w-64 max-w-full",
605-
collage && "h-full",
606-
)}
607-
>
608-
{image.name}
609-
</div>
610-
)}
611-
{image.previewUrl && image.source?.kind === "snap-shot" ? (
612-
<SnapShotAttachmentDetails source={image.source} />
613-
) : null}
614-
</div>
615-
))}
616-
{userVideos.map((file) => (
617-
<UserVideoAttachment key={file.id} file={file} />
618-
))}
619-
</div>
620-
)}
672+
{!mediaAboveText && mediaBlock}
621673
{userAudio.length > 0 ? (
622674
<div className="mb-2 flex max-w-full flex-col gap-1">
623675
{userAudio.map((file) => (

0 commit comments

Comments
 (0)