Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Show image thumbnails in chats

laurenspriemf546bcbccecd
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
index e2bad8ebe8..2bd8e0927b 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
@@ -53,6 +53,7 @@ import io.ente.ensu.designsystem.EnsuCornerRadius
import io.ente.ensu.designsystem.EnsuSpacing
import io.ente.ensu.designsystem.EnsuTypography
import io.ente.ensu.designsystem.HugeIcons
+import io.ente.ensu.components.ImageAttachmentThumbnail
import io.ente.ensu.domain.model.Attachment
import io.ente.ensu.domain.model.AttachmentType
import io.ente.ensu.domain.model.ChatMessage
@@ -145,13 +146,24 @@ internal fun MessageInput(
verticalArrangement = Arrangement.spacedBy(EnsuSpacing.sm.dp)
) {
attachments.forEach { attachment ->
- io.ente.ensu.components.AttachmentChip(
- name = attachment.name,
- size = attachment.sizeBytes.formattedFileSize(),
- iconRes = HugeIcons.Attachment01Icon,
- isUploading = attachment.isUploading,
- onDelete = { onRemoveAttachment(attachment) }
- )
+ if (attachment.type == AttachmentType.Image && attachment.localPath != null) {
+ ImageAttachmentThumbnail(
+ path = attachment.localPath,
+ contentDescription = attachment.name,
+ width = 76.dp,
+ height = 76.dp,
+ isUploading = attachment.isUploading,
+ onDelete = { onRemoveAttachment(attachment) }
+ )
+ } else {
+ io.ente.ensu.components.AttachmentChip(
+ name = attachment.name,
+ size = attachment.sizeBytes.formattedFileSize(),
+ iconRes = HugeIcons.Attachment01Icon,
+ isUploading = attachment.isUploading,
+ onDelete = { onRemoveAttachment(attachment) }
+ )
+ }
}
}
}
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
index f3e067b804..ff56d93586 100644
--- a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/chat/ChatMessages.kt
@@ -62,12 +62,14 @@ import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Popup
import androidx.compose.ui.window.PopupProperties
import io.ente.ensu.components.BranchSwitcher
+import io.ente.ensu.components.ImageAttachmentThumbnail
import io.ente.ensu.designsystem.EnsuColor
import io.ente.ensu.designsystem.EnsuCornerRadius
import io.ente.ensu.designsystem.EnsuSpacing
import io.ente.ensu.designsystem.EnsuTypography
import io.ente.ensu.designsystem.HugeIcons
import io.ente.ensu.domain.model.Attachment
+import io.ente.ensu.domain.model.AttachmentType
import io.ente.ensu.domain.model.ChatMessage
import io.ente.ensu.domain.model.MessageAuthor
import io.ente.ensu.domain.util.formatBytes
@@ -507,13 +509,24 @@ private fun UserMessageBubble(
verticalArrangement = Arrangement.spacedBy(EnsuSpacing.sm.dp)
) {
message.attachments.forEach { attachment ->
- io.ente.ensu.components.AttachmentChip(
- name = attachment.name,
- size = attachment.sizeBytes.formattedFileSize(),
- iconRes = HugeIcons.Attachment01Icon,
- isUploading = attachment.isUploading,
- onClick = { onOpenAttachment(attachment) }
- )
+ if (attachment.type == AttachmentType.Image && attachment.localPath != null) {
+ ImageAttachmentThumbnail(
+ path = attachment.localPath,
+ contentDescription = attachment.name,
+ width = 164.dp,
+ height = 124.dp,
+ isUploading = attachment.isUploading,
+ onClick = { onOpenAttachment(attachment) }
+ )
+ } else {
+ io.ente.ensu.components.AttachmentChip(
+ name = attachment.name,
+ size = attachment.sizeBytes.formattedFileSize(),
+ iconRes = HugeIcons.Attachment01Icon,
+ isUploading = attachment.isUploading,
+ onClick = { onOpenAttachment(attachment) }
+ )
+ }
}
}
Spacer(modifier = Modifier.height(EnsuSpacing.sm.dp))
diff --git a/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/components/ImageAttachmentThumbnail.kt b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/components/ImageAttachmentThumbnail.kt
new file mode 100644
index 0000000000..d29b0e9253
--- /dev/null
+++ b/mobile/native/android/apps/ensu/app-ui/src/main/java/io/ente/ensu/components/ImageAttachmentThumbnail.kt
@@ -0,0 +1,174 @@
+package io.ente.ensu.components
+
+import android.graphics.BitmapFactory
+import androidx.compose.foundation.Image
+import androidx.compose.foundation.background
+import androidx.compose.foundation.border
+import androidx.compose.foundation.clickable
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.shape.CircleShape
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material3.CircularProgressIndicator
+import androidx.compose.material3.Icon
+import androidx.compose.material3.IconButton
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.produceState
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.draw.clip
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.graphics.ImageBitmap
+import androidx.compose.ui.graphics.asImageBitmap
+import androidx.compose.ui.hapticfeedback.HapticFeedbackType
+import androidx.compose.ui.layout.ContentScale
+import androidx.compose.ui.platform.LocalDensity
+import androidx.compose.ui.res.painterResource
+import androidx.compose.ui.unit.Dp
+import androidx.compose.ui.unit.dp
+import io.ente.ensu.designsystem.EnsuColor
+import io.ente.ensu.designsystem.EnsuCornerRadius
+import io.ente.ensu.designsystem.HugeIcons
+import io.ente.ensu.utils.rememberEnsuHaptics
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+
+@Composable
+fun ImageAttachmentThumbnail(
+ path: String?,
+ contentDescription: String?,
+ width: Dp,
+ height: Dp,
+ modifier: Modifier = Modifier,
+ isUploading: Boolean = false,
+ onDelete: (() -> Unit)? = null,
+ onClick: (() -> Unit)? = null
+) {
+ val density = LocalDensity.current
+ val targetWidthPx = with(density) { width.roundToPx() }
+ val targetHeightPx = with(density) { height.roundToPx() }
+ val bitmap by produceState<ImageBitmap?>(initialValue = null, path, targetWidthPx, targetHeightPx) {
+ value = decodeSampledImage(path, targetWidthPx, targetHeightPx)
+ }
+ val haptic = rememberEnsuHaptics()
+ val shape = RoundedCornerShape(EnsuCornerRadius.card.dp)
+ val clickModifier = if (onClick != null) {
+ Modifier.clickable {
+ haptic.perform(HapticFeedbackType.TextHandleMove)
+ onClick()
+ }
+ } else {
+ Modifier
+ }
+
+ Box(
+ modifier = modifier
+ .size(width = width, height = height)
+ .clip(shape)
+ .background(EnsuColor.fillFaint(), shape)
+ .border(1.dp, EnsuColor.border().copy(alpha = 0.7f), shape)
+ .then(clickModifier),
+ contentAlignment = Alignment.Center
+ ) {
+ if (bitmap != null) {
+ Image(
+ bitmap = bitmap!!,
+ contentDescription = contentDescription,
+ modifier = Modifier.fillMaxSize(),
+ contentScale = ContentScale.Crop
+ )
+ } else {
+ Icon(
+ painter = painterResource(HugeIcons.Attachment01Icon),
+ contentDescription = contentDescription,
+ modifier = Modifier.size(24.dp),
+ tint = EnsuColor.textMuted()
+ )
+ }
+
+ if (isUploading) {
+ Box(
+ modifier = Modifier
+ .fillMaxSize()
+ .background(Color.Black.copy(alpha = 0.18f)),
+ contentAlignment = Alignment.Center
+ ) {
+ CircularProgressIndicator(
+ modifier = Modifier.size(22.dp),
+ strokeWidth = 2.dp,
+ color = Color.White
+ )
+ }
+ }
+
+ if (onDelete != null) {
+ IconButton(
+ onClick = {
+ haptic.perform(HapticFeedbackType.LongPress)
+ onDelete()
+ },
+ modifier = Modifier
+ .align(Alignment.TopEnd)
+ .padding(4.dp)
+ .size(28.dp)
+ .background(Color.Black.copy(alpha = 0.42f), CircleShape)
+ ) {
+ Icon(
+ painter = painterResource(HugeIcons.Cancel01Icon),
+ contentDescription = "Remove image",
+ modifier = Modifier.size(12.dp),
+ tint = Color.White
+ )
+ }
+ }
+ }
+}
+
+private suspend fun decodeSampledImage(
+ path: String?,
+ targetWidthPx: Int,
+ targetHeightPx: Int
+): ImageBitmap? = withContext(Dispatchers.IO) {
+ if (path.isNullOrBlank() || targetWidthPx <= 0 || targetHeightPx <= 0) {
+ return@withContext null
+ }
+
+ val bounds = BitmapFactory.Options().apply {
+ inJustDecodeBounds = true
+ }
+ BitmapFactory.decodeFile(path, bounds)
+ if (bounds.outWidth <= 0 || bounds.outHeight <= 0) {
+ return@withContext null
+ }
+
+ val options = BitmapFactory.Options().apply {
+ inSampleSize = calculateInSampleSize(
+ width = bounds.outWidth,
+ height = bounds.outHeight,
+ targetWidth = targetWidthPx,
+ targetHeight = targetHeightPx
+ )
+ }
+
+ BitmapFactory.decodeFile(path, options)?.asImageBitmap()
+}
+
+private fun calculateInSampleSize(
+ width: Int,
+ height: Int,
+ targetWidth: Int,
+ targetHeight: Int
+): Int {
+ var inSampleSize = 1
+ if (height > targetHeight || width > targetWidth) {
+ val halfHeight = height / 2
+ val halfWidth = width / 2
+ while (halfHeight / inSampleSize >= targetHeight && halfWidth / inSampleSize >= targetWidth) {
+ inSampleSize *= 2
+ }
+ }
+ return inSampleSize
+}
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift
index 798f6e6ab4..bc01ad9f01 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageInputView.swift
@@ -65,7 +65,10 @@ struct MessageInputView: View {
VStack(alignment: .leading, spacing: inputStackSpacing) {
if !attachments.isEmpty {
- let maxAttachmentHeight = CGFloat(3) * 40 + CGFloat(2) * EnsuSpacing.sm
+ let hasImageAttachment = attachments.contains { $0.kind == .image }
+ let maxAttachmentHeight = hasImageAttachment
+ ? CGFloat(2) * 76 + EnsuSpacing.sm
+ : CGFloat(3) * 40 + CGFloat(2) * EnsuSpacing.sm
let shouldScroll = attachments.count > 4
Group {
@@ -329,13 +332,25 @@ struct MessageInputView: View {
private var attachmentFlowLayout: some View {
FlowLayout(spacing: EnsuSpacing.sm) {
ForEach(attachments) { attachment in
- AttachmentChip(
- name: attachment.name,
- size: attachment.formattedSize,
- icon: attachment.iconName,
- isUploading: attachment.isUploading
- ) {
- onRemoveAttachment(attachment)
+ if attachment.kind == .image, attachment.url != nil {
+ ImageAttachmentThumbnail(
+ url: attachment.url,
+ accessibilityLabel: attachment.name,
+ width: 76,
+ height: 76,
+ isUploading: attachment.isUploading
+ ) {
+ onRemoveAttachment(attachment)
+ }
+ } else {
+ AttachmentChip(
+ name: attachment.name,
+ size: attachment.formattedSize,
+ icon: attachment.iconName,
+ isUploading: attachment.isUploading
+ ) {
+ onRemoveAttachment(attachment)
+ }
}
}
}
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageList/MessageListComponents.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageList/MessageListComponents.swift
index 47e6374e24..399fbda2d2 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageList/MessageListComponents.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Chat/MessageList/MessageListComponents.swift
@@ -103,15 +103,29 @@ struct UserMessageBubbleView: View {
if !message.attachments.isEmpty {
FlowLayout(spacing: EnsuSpacing.sm) {
ForEach(message.attachments) { attachment in
- AttachmentChip(
- name: attachment.name,
- size: attachment.formattedSize,
- icon: attachment.iconName,
- isUploading: attachment.isUploading
- )
- .onTapGesture {
- hapticTap()
- onOpenAttachment(attachment)
+ if attachment.kind == .image, attachment.url != nil {
+ ImageAttachmentThumbnail(
+ url: attachment.url,
+ accessibilityLabel: attachment.name,
+ width: 164,
+ height: 124,
+ isUploading: attachment.isUploading
+ )
+ .onTapGesture {
+ hapticTap()
+ onOpenAttachment(attachment)
+ }
+ } else {
+ AttachmentChip(
+ name: attachment.name,
+ size: attachment.formattedSize,
+ icon: attachment.iconName,
+ isUploading: attachment.isUploading
+ )
+ .onTapGesture {
+ hapticTap()
+ onOpenAttachment(attachment)
+ }
}
}
}
diff --git a/mobile/native/darwin/Apps/Ensu/Ensu/Components/EnsuComponents.swift b/mobile/native/darwin/Apps/Ensu/Ensu/Components/EnsuComponents.swift
index 84c42c1807..ff9fa91c35 100644
--- a/mobile/native/darwin/Apps/Ensu/Ensu/Components/EnsuComponents.swift
+++ b/mobile/native/darwin/Apps/Ensu/Ensu/Components/EnsuComponents.swift
@@ -1,5 +1,10 @@
#if canImport(EnteCore)
import SwiftUI
+#if os(iOS)
+import UIKit
+#elseif os(macOS)
+import AppKit
+#endif
struct PrimaryButton: View {
let text: String
@@ -176,6 +181,94 @@ struct AttachmentChip: View {
}
}
+struct ImageAttachmentThumbnail: View {
+ let url: URL?
+ let accessibilityLabel: String
+ var width: CGFloat
+ var height: CGFloat
+ var isUploading: Bool = false
+ var onDelete: (() -> Void)? = nil
+
+ var body: some View {
+ ZStack(alignment: .topTrailing) {
+ thumbnailContent
+ .frame(width: width, height: height)
+ .background(EnsuColor.fillFaint)
+ .clipShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.card, style: .continuous))
+ .overlay {
+ RoundedRectangle(cornerRadius: EnsuCornerRadius.card, style: .continuous)
+ .stroke(EnsuColor.border.opacity(0.7), lineWidth: 0.5)
+ }
+ .contentShape(RoundedRectangle(cornerRadius: EnsuCornerRadius.card, style: .continuous))
+
+ if isUploading {
+ RoundedRectangle(cornerRadius: EnsuCornerRadius.card, style: .continuous)
+ .fill(.black.opacity(0.18))
+ .frame(width: width, height: height)
+
+ ProgressView()
+ .scaleEffect(0.8)
+ .frame(width: width, height: height)
+ }
+
+ if let onDelete {
+ Button(action: {
+ hapticTap()
+ onDelete()
+ }) {
+ Image("Cancel01Icon")
+ .resizable()
+ .scaledToFit()
+ .frame(width: 10, height: 10)
+ .foregroundStyle(.white)
+ }
+ .buttonStyle(.plain)
+ .frame(width: 28, height: 28)
+ .background(.black.opacity(0.45))
+ .clipShape(Circle())
+ .padding(4)
+ .accessibilityLabel("Remove image")
+ }
+ }
+ .accessibilityLabel(accessibilityLabel)
+ }
+
+ @ViewBuilder
+ private var thumbnailContent: some View {
+ if let image = platformImage(from: url) {
+ image
+ .resizable()
+ .scaledToFill()
+ } else {
+ Image("Attachment01Icon")
+ .resizable()
+ .scaledToFit()
+ .frame(width: 24, height: 24)
+ .foregroundStyle(EnsuColor.textMuted)
+ }
+ }
+
+ private func platformImage(from url: URL?) -> Image? {
+ guard let url else {
+ return nil
+ }
+
+ #if os(iOS)
+ guard let image = UIImage(contentsOfFile: url.path) else {
+ return nil
+ }
+ return Image(uiImage: image)
+ #elseif os(macOS)
+ guard let image = NSImage(contentsOf: url) else {
+ return nil
+ }
+ return Image(nsImage: image)
+ #else
+ return nil
+ #endif
+ }
+}
+
struct ToastView: View {
let message: String
@@ -346,6 +439,40 @@ struct AttachmentChip: View {
}
}
+struct ImageAttachmentThumbnail: View {
+ let url: URL?
+ let accessibilityLabel: String
+ var width: CGFloat
+ var height: CGFloat
+ var isUploading: Bool = false
+ var onDelete: (() -> Void)? = nil
+
+ var body: some View {
+ ZStack(alignment: .topTrailing) {
+ RoundedRectangle(cornerRadius: 12, style: .continuous)
+ .fill(Color.secondary.opacity(0.14))
+ .frame(width: width, height: height)
+ .overlay {
+ Image(systemName: "photo")
+ .foregroundStyle(.secondary)
+ }
+
+ if isUploading {
+ ProgressView()
+ .frame(width: width, height: height)
+ }
+
+ if let onDelete {
+ Button(action: onDelete) {
+ Image(systemName: "xmark")
+ }
+ .frame(width: 28, height: 28)
+ }
+ }
+ .accessibilityLabel(accessibilityLabel)
+ }
+}
+
struct ToastView: View {
let message: String
diff --git a/web/apps/ensu/src/components/chat/ChatComposer.tsx b/web/apps/ensu/src/components/chat/ChatComposer.tsx
index 781ab6ef69..2ab6803240 100644
--- a/web/apps/ensu/src/components/chat/ChatComposer.tsx
+++ b/web/apps/ensu/src/components/chat/ChatComposer.tsx
@@ -1,4 +1,5 @@
import {
+ Attachment01Icon,
Cancel01Icon,
Edit01Icon,
Navigation06Icon,
@@ -413,10 +414,9 @@ export const ChatComposer = memo(
{pendingImages.length > 0 && (
<Box
sx={{
- display: "grid",
- gridTemplateColumns:
- "repeat(2, minmax(0, 1fr))",
- gap: 0.5,
+ display: "flex",
+ flexWrap: "wrap",
+ gap: 1,
}}
>
{pendingImages.map((img) => {
@@ -426,64 +426,68 @@ export const ChatComposer = memo(
<Box
key={img.id}
sx={{
+ position: "relative",
+ width: 76,
+ height: 76,
+ borderRadius: 2,
+ bgcolor: "fill.faint",
+ border: "1px solid",
+ borderColor: "divider",
+ overflow: "hidden",
display: "flex",
alignItems: "center",
- gap: 1,
- px: 1,
- py: 0.75,
- borderRadius: 1.5,
- bgcolor: "fill.faint",
- minWidth: 0,
+ justifyContent:
+ "center",
+ color: "text.muted",
+ boxShadow:
+ "0 1px 2px rgba(0, 0, 0, 0.08)",
}}
>
- {preview && (
+ {preview ? (
<Box
component="img"
src={preview}
alt={img.name}
sx={{
- width: 40,
- height: 40,
- borderRadius: 1,
+ width: "100%",
+ height: "100%",
objectFit:
"cover",
+ display:
+ "block",
}}
/>
+ ) : (
+ <HugeiconsIcon
+ icon={
+ Attachment01Icon
+ }
+ size={22}
+ strokeWidth={1.7}
+ />
)}
- <Box
- sx={{
- flex: 1,
- minWidth: 0,
- }}
- >
- <Typography
- variant="mini"
- sx={{
- color: "text.base",
- overflow:
- "hidden",
- textOverflow:
- "ellipsis",
- whiteSpace:
- "nowrap",
- }}
- >
- {img.name}
- </Typography>
- <Typography
- variant="mini"
- sx={{
- color: "text.muted",
- }}
- >
- {formatBytes(
- img.size,
- )}
- </Typography>
- </Box>
<IconButton
aria-label="Remove image"
- sx={actionButtonSx}
+ sx={{
+ position:
+ "absolute",
+ top: 4,
+ right: 4,
+ width: 24,
+ height: 24,
+ p: 0,
+ borderRadius:
+ "999px",
+ bgcolor:
+ "rgba(0, 0, 0, 0.45)",
+ color: "#fff",
+ backdropFilter:
+ "blur(8px)",
+ "&:hover": {
+ bgcolor:
+ "rgba(0, 0, 0, 0.6)",
+ },
+ }}
onClick={() =>
removePendingImage(
img.id,
diff --git a/web/apps/ensu/src/components/chat/ChatMessageList.tsx b/web/apps/ensu/src/components/chat/ChatMessageList.tsx
index 0c60b7551f..17b19f8791 100644
--- a/web/apps/ensu/src/components/chat/ChatMessageList.tsx
+++ b/web/apps/ensu/src/components/chat/ChatMessageList.tsx
@@ -1,6 +1,7 @@
import {
ArrowLeft01Icon,
ArrowRight01Icon,
+ Attachment01Icon,
Copy01Icon,
Edit01Icon,
RepeatIcon,
@@ -10,7 +11,7 @@ import { Box, IconButton, Stack, Typography } from "@mui/material";
import type { SxProps, Theme } from "@mui/material/styles";
import { MarkdownRenderer } from "components/MarkdownRenderer";
import GeneratingRiveIndicator from "components/chat/GeneratingRiveIndicator";
-import React, { memo, useCallback, useMemo } from "react";
+import React, { memo, useCallback, useEffect, useMemo } from "react";
import {
STREAMING_SELECTION_KEY,
type BranchSwitcher,
@@ -65,46 +66,61 @@ export interface ChatMessageListProps {
actionIconProps: IconProps;
}
-interface AttachmentCardProps {
+interface ImageAttachmentThumbnailProps {
attachment: ChatAttachment;
+ previewUrl?: string;
onClick: () => void;
}
-const AttachmentCard = memo(({ attachment, onClick }: AttachmentCardProps) => {
- return (
- <Box
- onClick={onClick}
- sx={{
- display: "flex",
- alignItems: "center",
- gap: 1,
- px: 1.5,
- py: 1,
- borderRadius: 2,
- bgcolor: "fill.faint",
- border: "1px solid",
- borderColor: "divider",
- cursor: "pointer",
- width: "fit-content",
- maxWidth: "100%",
- minWidth: 0,
- }}
- >
- <Typography
- variant="small"
+const ImageAttachmentThumbnail = memo(
+ ({ attachment, previewUrl, onClick }: ImageAttachmentThumbnailProps) => {
+ return (
+ <Box
+ component="button"
+ type="button"
+ aria-label={attachment.name || "Open image attachment"}
+ onClick={onClick}
sx={{
- color: "text.base",
+ width: { xs: 148, sm: 164 },
+ height: { xs: 112, sm: 124 },
+ p: 0,
+ border: "1px solid",
+ borderColor: "divider",
+ borderRadius: 2,
+ bgcolor: "fill.faint",
+ cursor: "pointer",
overflow: "hidden",
- textOverflow: "ellipsis",
- whiteSpace: "nowrap",
- minWidth: 0,
+ display: "flex",
+ alignItems: "center",
+ justifyContent: "center",
+ color: "text.muted",
+ lineHeight: 0,
+ boxShadow: "0 1px 2px rgba(0, 0, 0, 0.08)",
}}
>
- {attachment.name ?? "Attachment"}
- </Typography>
- </Box>
- );
-});
+ {previewUrl ? (
+ <Box
+ component="img"
+ src={previewUrl}
+ alt={attachment.name || "Image attachment"}
+ sx={{
+ width: "100%",
+ height: "100%",
+ objectFit: "cover",
+ display: "block",
+ }}
+ />
+ ) : (
+ <HugeiconsIcon
+ icon={Attachment01Icon}
+ size={24}
+ strokeWidth={1.7}
+ />
+ )}
+ </Box>
+ );
+ },
+);
const AiSafetyFooter = memo(() => {
return (
@@ -140,6 +156,8 @@ interface MessageRowProps {
onRetryMessage: (message: ChatMessage) => void;
onPrevBranch: (switcher: BranchSwitcher) => void;
onNextBranch: (switcher: BranchSwitcher) => void;
+ attachmentPreviews: Record<string, string>;
+ onRequestPreview: (attachment: ChatAttachment, sessionUuid: string) => void;
parseDocumentBlocks: (text: string) => ParsedDocuments;
stripHiddenParts: (text: string) => string;
formatTime: (timestamp: number) => string;
@@ -167,6 +185,8 @@ const MessageRow = memo(
onRetryMessage,
onPrevBranch,
onNextBranch,
+ attachmentPreviews,
+ onRequestPreview,
parseDocumentBlocks,
stripHiddenParts,
formatTime,
@@ -215,6 +235,23 @@ const MessageRow = memo(
!isSelf && isStreaming && !displayText.trim();
const dots = ".".repeat(loadingDots);
+ useEffect(() => {
+ if (imageAttachments.length === 0) {
+ return;
+ }
+
+ for (const attachment of imageAttachments) {
+ if (!attachmentPreviews[attachment.id]) {
+ onRequestPreview(attachment, message.sessionUuid);
+ }
+ }
+ }, [
+ attachmentPreviews,
+ imageAttachments,
+ message.sessionUuid,
+ onRequestPreview,
+ ]);
+
return (
<Box
sx={{
@@ -245,9 +282,12 @@ const MessageRow = memo(
}}
>
{imageAttachments.map((attachment) => (
- <AttachmentCard
+ <ImageAttachmentThumbnail
key={attachment.id}
attachment={attachment}
+ previewUrl={
+ attachmentPreviews[attachment.id]
+ }
onClick={() =>
onOpenAttachment(message, attachment)
}
@@ -552,7 +592,7 @@ const MessageRow = memo(
export const ChatMessageList = memo(
({
messages,
- attachmentPreviews: _attachmentPreviews,
+ attachmentPreviews,
branchSwitchers,
loadingPhrase,
loadingDots,
@@ -568,6 +608,7 @@ export const ChatMessageList = memo(
onRetryMessage,
onPrevBranch,
onNextBranch,
+ onRequestPreview,
parseDocumentBlocks,
stripHiddenParts,
formatTime,
@@ -627,6 +668,8 @@ export const ChatMessageList = memo(
onRetryMessage={onRetryMessage}
onPrevBranch={onPrevBranch}
onNextBranch={onNextBranch}
+ attachmentPreviews={attachmentPreviews}
+ onRequestPreview={onRequestPreview}
parseDocumentBlocks={parseDocumentBlocks}
stripHiddenParts={stripHiddenParts}
formatTime={formatTime}
@@ -644,6 +687,7 @@ export const ChatMessageList = memo(
actionButtonSx,
actionIconProps,
assistantMarkdownSx,
+ attachmentPreviews,
branchSwitchers,
formatTime,
isGenerating,
@@ -655,6 +699,7 @@ export const ChatMessageList = memo(
onNextBranch,
onOpenAttachment,
onPrevBranch,
+ onRequestPreview,
onRetryMessage,
parseDocumentBlocks,
smallIconProps,