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 3 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
37 changes: 37 additions & 0 deletions .storybook/index.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

.masonry-grid {
amcdnl marked this conversation as resolved.
Show resolved Hide resolved
display: grid;
grid-template-columns: repeat(3, 5rem);
grid-template-rows: 5rem 5rem;
}

.grid-item-large {
grid-column: span 2;
grid-row: span 2;
}

.masonry-item {
position: relative;
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 10px;
}

.grid-item-small:nth-child(3) {
position: relative;
}

.masonry-button-overlay {
position: absolute;
top: 0;
left: 0;
width: 5rem;
height: 5rem;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
border-radius: 10px;
cursor: pointer;
}
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
60 changes: 50 additions & 10 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 @@ -12,23 +12,63 @@ interface MessageFilesProps extends PropsWithChildren {
files: ConversationFile[];
}


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;
}

// Filter image files
const imageFiles = files.filter(file => file.type.startsWith('image/'));
amcdnl marked this conversation as resolved.
Show resolved Hide resolved
const otherFiles = files.filter(file => !file.type.startsWith('image/'));

return (
files.length > 0 && (
<div className={cn(theme.messages.message.files.base)}>
{files.map((file, index) => (
<Comp key={index} {...file}>
{children}
</Comp>
))}
</div>
)
<>
{imageFiles.length > 3 ? (
amcdnl marked this conversation as resolved.
Show resolved Hide resolved
<div className={cn(theme.messages.message.files.base, expanded ? '' : 'masonry-grid')}>
{imageFiles.slice(0, expanded ? imageFiles.length : 3).map((file, index) => (
expanded ? (
<Comp key={index} {...file}>
{children}
</Comp>
) : (
<figure
key={index}
className={index === 0 ? "grid-item-large" : "grid-item-small"}
>
<img src={file.url} alt={file.name} className="masonry-item" />
{index === 2 && imageFiles.length > 3 && !expanded && (
<div className="masonry-button-overlay" onClick={() => setExpanded(true)}>
+{imageFiles.length - 3}
amcdnl marked this conversation as resolved.
Show resolved Hide resolved
</div>
)}
</figure>
)
))}
</div>
) : (
<div className={cn(theme.messages.message.files.base)}>
{imageFiles.map((file, index) => (
<Comp key={index} {...file}>
{children}
</Comp>
))}
</div>
)}

{otherFiles.length > 0 && (
amcdnl marked this conversation as resolved.
Show resolved Hide resolved
<div className={cn(theme.messages.message.files.base)}>
{otherFiles.map((file, index) => (
<Comp key={index} {...file}>
{children}
</Comp>
))}
</div>
)}
</>
);
};
Loading