Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions frontend/src/components/chat/MessageComposer/Attachment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect, useMemo } from 'react';
import { DefaultExtensionType, FileIcon, defaultStyles } from 'react-file-icon';

import { Card } from '@/components/ui/card';
Expand All @@ -13,9 +13,32 @@ interface AttachmentProps {
name: string;
mime: string;
children?: React.ReactNode;
file?: File;
}

const Attachment: React.FC<AttachmentProps> = ({ name, mime, children }) => {
const Attachment: React.FC<AttachmentProps> = ({
name,
mime,
children,
file
}) => {
const isImage = useMemo(() => mime.startsWith('image/'), [mime]);
const imageUrl = useMemo(() => {
if (isImage && file) {
return URL.createObjectURL(file);
}
return undefined;
}, [isImage, file]);

// Cleanup Object URL on unmount or when imageUrl changes
useEffect(() => {
return () => {
if (imageUrl) {
URL.revokeObjectURL(imageUrl);
}
};
}, [imageUrl]);

let extension: DefaultExtensionType;
if (name.includes('.')) {
extension = name.split('.').pop()!.toLowerCase() as DefaultExtensionType;
Expand All @@ -25,6 +48,30 @@ const Attachment: React.FC<AttachmentProps> = ({ name, mime, children }) => {
: ('txt' as DefaultExtensionType);
}

if (isImage && imageUrl) {
return (
<TooltipProvider delayDuration={100}>
<Tooltip>
<TooltipTrigger asChild>
<div className="relative h-[58px] w-[58px]">
{children}
<Card className="h-full p-1 flex items-center justify-center rounded-lg border overflow-hidden">
<img
src={imageUrl}
alt={name}
className="h-full w-full object-cover"
/>
</Card>
</div>
</TooltipTrigger>
<TooltipContent>
<p>{name}</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
);
}

return (
<TooltipProvider delayDuration={100}>
<Tooltip>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const Attachments = () => {
key={attachment.id}
name={attachment.name}
mime={attachment.type}
file={attachment.file}
>
{progress}
{remove}
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ const Chat = () => {
name: file.name,
size: file.size,
uploadProgress: 0,
file,
cancel: () => {
toast.info(`${t('chat.fileUpload.errors.cancelled')} ${file.name}`);
xhr.abort();
Expand Down
1 change: 1 addition & 0 deletions frontend/src/state/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface IAttachment {
uploaded?: boolean;
cancel?: () => void;
remove?: () => void;
file?: File;
}

export const attachmentsState = atom<IAttachment[]>({
Expand Down
Loading