From 78b633b9aad305c2b8ff207a714d755f69cec9d1 Mon Sep 17 00:00:00 2001 From: AntonJames-Sistence Date: Mon, 26 Aug 2024 16:10:19 -0400 Subject: [PATCH] Improve logic of Masonry grid layout --- .../SessionMessage/MessageFiles.tsx | 105 ++++++++++-------- 1 file changed, 56 insertions(+), 49 deletions(-) diff --git a/src/SessionMessages/SessionMessage/MessageFiles.tsx b/src/SessionMessages/SessionMessage/MessageFiles.tsx index 806e587..5d281a5 100644 --- a/src/SessionMessages/SessionMessage/MessageFiles.tsx +++ b/src/SessionMessages/SessionMessage/MessageFiles.tsx @@ -12,7 +12,6 @@ interface MessageFilesProps extends PropsWithChildren { files: ConversationFile[]; } - export const MessageFiles: FC = ({ files, children }) => { const { theme } = useContext(ChatContext); const Comp = children ? Slot : MessageFile; @@ -23,59 +22,67 @@ export const MessageFiles: FC = ({ files, children }) => { } // 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); + 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[] } + ); - 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 + ? images.slice(0, 3).map((image, index) => ( +
+ {image.name} + {index === 2 && ( +
setExpanded(true)} + > + +{(imageFiles.length - 3).toLocaleString()} +
+ )} +
+ )) + : images.map((image, index) => ( + + {children} + + )); + }; return ( - <> - {imageFiles.length > 3 ? ( -
- {imageFiles.slice(0, expanded ? imageFiles.length : 3).map((file, index) => ( - expanded ? ( - - {children} - - ) : ( -
- {file.name} - {index === 2 && imageFiles.length > 3 && !expanded && ( -
setExpanded(true)}> - +{(imageFiles.length - 3).toLocaleString()} -
- )} -
- ) - ))} -
- ) : ( -
- {imageFiles.map((file, index) => ( - - {children} - - ))} -
+
+ {imageFiles.length > 0 && renderImageFiles(imageFiles)} - {otherFiles.length > 0 && ( -
- {otherFiles.map((file, index) => ( - - {children} - - ))} -
- )} - + {otherFiles.length > 0 && + otherFiles.map((file, index) => ( + + {children} + + ))} +
); };