Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Masonry Grid Layout for Uploaded Images with Expandable View #40

Merged
merged 7 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion .storybook/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@tailwind utilities;
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/ChatInput/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface ChatInputProps {
sendIcon?: ReactElement;

/**
* Icon to show for send.
* Icon to show for stop.
*/
stopIcon?: ReactElement;

Expand Down
66 changes: 60 additions & 6 deletions src/SessionMessages/SessionMessage/MessageFiles.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChatContext } from '@/ChatContext';
import { ConversationFile } from '@/types';
import { cn } from 'reablocks';
import { FC, PropsWithChildren, useContext } from 'react';
import { FC, PropsWithChildren, useContext, useState } from 'react';
import { MessageFile } from './MessageFile';
import { Slot } from '@radix-ui/react-slot';

Expand All @@ -15,20 +15,74 @@ interface MessageFilesProps extends PropsWithChildren {
export const MessageFiles: FC<MessageFilesProps> = ({ files, children }) => {
const { theme } = useContext(ChatContext);
const Comp = children ? Slot : MessageFile;
const [expanded, setExpanded] = useState<boolean>(false);

if (!files || files.length === 0) {
return null;
}

// Group image and other files
const { imageFiles, otherFiles } = files.reduce(
(acc, file) => {
if (file.type.startsWith('image/')) {
acc.imageFiles.push(file);
} else {
acc.otherFiles.push(file);
}

return acc;
},
{
imageFiles: [] as ConversationFile[],
otherFiles: [] as ConversationFile[]
}
);

// Renders the image files based on the current expansion state
const renderImageFiles = (images: ConversationFile[]) => {
return !expanded && imageFiles.length > 3
amcdnl marked this conversation as resolved.
Show resolved Hide resolved
? images.slice(0, 3).map((image, index) => (
<figure
key={index}
className={index === 0 ? 'col-span-2 row-span-2' : 'relative'}
>
<img
src={image.url}
alt={image.name}
className="relative w-full h-full object-cover rounded-lg"
/>
{index === 2 && (
<div
className="absolute top-0 left-0 w-full h-full flex justify-center items-center bg-black bg-opacity-50 rounded-lg cursor-pointer"
onClick={() => setExpanded(true)}
>
+{(imageFiles.length - 3).toLocaleString()}
</div>
)}
</figure>
))
: images.map((image, index) => (
<Comp key={index} {...image}>
{children}
</Comp>
));
};

return (
files.length > 0 && (
<div className={cn(theme.messages.message.files.base)}>
{files.map((file, index) => (
<div
className={cn(
theme.messages.message.files.base,
imageFiles.length < 4 || expanded ? '' : 'grid grid-cols-3 gap-2 w-1/2'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here we can use truncateImages instead

)}
>
{imageFiles.length > 0 && renderImageFiles(imageFiles)}

{otherFiles.length > 0 &&
otherFiles.map((file, index) => (
<Comp key={index} {...file}>
{children}
</Comp>
))}
</div>
)
</div>
);
};
Loading