From 2dd1dc2ba3dc0f48802dd0d82aab61aeec645c5e Mon Sep 17 00:00:00 2001
From: niki <295591807+nikiwastaken@users.noreply.github.com>
Date: Fri, 3 Jul 2026 12:30:24 +0000
Subject: [PATCH] Change gallery to be a grid
---
src/app/components/RenderMessageContent.tsx | 15 +-
src/app/components/media/media.css.ts | 2 +-
src/app/components/message/MGallery.css.ts | 49 +++-
src/app/components/message/MGallery.tsx | 212 +++++++++---------
.../components/message/MsgTypeRenderers.tsx | 7 +-
5 files changed, 157 insertions(+), 128 deletions(-)
diff --git a/src/app/components/RenderMessageContent.tsx b/src/app/components/RenderMessageContent.tsx
index f6af3da09..acb16fa12 100644
--- a/src/app/components/RenderMessageContent.tsx
+++ b/src/app/components/RenderMessageContent.tsx
@@ -10,7 +10,7 @@ import { useSetting } from '$state/hooks/settings';
import { settingsAtom, CaptionPosition } from '$state/settings';
import type { HTMLReactParserOptions } from 'html-react-parser';
import type { Opts } from 'linkifyjs';
-import { Box, config, toRem } from 'folds';
+import { Box, config } from 'folds';
import {
AudioContent,
DownloadFile,
@@ -264,16 +264,11 @@ function RenderMessageContentInternal({
style={{
display: 'flex',
flexDirection: attachmentDirection,
+ height: '100%',
+ width: '100%',
}}
>
-
- {attachment}
-
+ {attachment}
{renderCaption()}
);
@@ -283,7 +278,7 @@ function RenderMessageContentInternal({
renderCaptionedAttachment(
& { msgtype: MsgType.File }}
- fitParent={isGallery}
+ //fitParent={isGallery}
renderFileContent={({ body, mimeType, info, encInfo, url }) => (
ReactNode;
};
+type PartitionedMediaItem = {
+ items: (IGalleryImageItem | IGalleryVideoItem)[];
+ type: 'ThreeByThree' | 'TwoByTwo' | 'OneByOne' | 'ThreeItems';
+};
+
export function MGallery({ content, renderItem, renderCaption }: MGalleryProps) {
- const scrollRef = useRef(null);
- const backAnchorRef = useRef(null);
- const frontAnchorRef = useRef(null);
- const [backVisible, setBackVisible] = useState(true);
- const [frontVisible, setFrontVisible] = useState(true);
-
- const intersectionObserver = useIntersectionObserver(
- useCallback((entries) => {
- const backAnchor = backAnchorRef.current;
- const frontAnchor = frontAnchorRef.current;
- const backEntry = backAnchor && getIntersectionObserverEntry(backAnchor, entries);
- const frontEntry = frontAnchor && getIntersectionObserverEntry(frontAnchor, entries);
- if (backEntry) {
- setBackVisible(backEntry.isIntersecting);
- }
- if (frontEntry) {
- setFrontVisible(frontEntry.isIntersecting);
- }
- }, []),
- useCallback(
- () => ({
- root: scrollRef.current,
- rootMargin: '10px',
- }),
- []
- )
- );
-
- useEffect(() => {
- const backAnchor = backAnchorRef.current;
- const frontAnchor = frontAnchorRef.current;
- if (backAnchor) intersectionObserver?.observe(backAnchor);
- if (frontAnchor) intersectionObserver?.observe(frontAnchor);
- return () => {
- if (backAnchor) intersectionObserver?.unobserve(backAnchor);
- if (frontAnchor) intersectionObserver?.unobserve(frontAnchor);
- };
- }, [intersectionObserver]);
-
- const handleScrollBack = () => {
- const scroll = scrollRef.current;
- if (!scroll) return;
- const { offsetWidth, scrollLeft } = scroll;
- scroll.scrollTo({
- left: scrollLeft - offsetWidth / 1.3,
- behavior: 'smooth',
- });
- };
- const handleScrollFront = () => {
- const scroll = scrollRef.current;
- if (!scroll) return;
- const { offsetWidth, scrollLeft } = scroll;
- scroll.scrollTo({
- left: scrollLeft + offsetWidth / 1.3,
- behavior: 'smooth',
- });
- };
-
const items = content.itemtypes;
+ let mediaItems = items.filter(
+ (item) => item.itemtype == MsgType.Video || item.itemtype == MsgType.Image
+ );
+ const columnItems = items.filter(
+ (item) => item.itemtype == MsgType.File || item.itemtype == MsgType.Audio
+ );
+
+ let partitionedMediaItems: PartitionedMediaItem[] = [];
+
+ while (mediaItems.length > 0) {
+ if (mediaItems.length >= 9) {
+ partitionedMediaItems.unshift({
+ items: mediaItems.slice(-9),
+ type: 'ThreeByThree',
+ });
+ mediaItems = mediaItems.slice(0, mediaItems.length - 9);
+ continue;
+ }
+ if (mediaItems.length >= 6) {
+ partitionedMediaItems.unshift({
+ items: mediaItems.slice(-6),
+ type: 'ThreeByThree',
+ });
+ mediaItems = mediaItems.slice(0, mediaItems.length - 6);
+ continue;
+ }
+ if (mediaItems.length >= 4) {
+ partitionedMediaItems.unshift({
+ items: mediaItems.slice(-4),
+ type: 'TwoByTwo',
+ });
+ mediaItems = mediaItems.slice(0, mediaItems.length - 4);
+ continue;
+ }
+ if (mediaItems.length > 3) {
+ partitionedMediaItems.unshift({
+ items: mediaItems.slice(-3),
+ type: 'ThreeByThree',
+ });
+ mediaItems = mediaItems.slice(0, mediaItems.length - 3);
+ continue;
+ }
+ if (mediaItems.length == 3) {
+ partitionedMediaItems.unshift({
+ items: mediaItems.slice(-3),
+ type: 'ThreeItems',
+ });
+ mediaItems = mediaItems.slice(0, mediaItems.length - 3);
+ continue;
+ }
+ if (mediaItems.length >= 2) {
+ partitionedMediaItems.unshift({
+ items: mediaItems.slice(-2),
+ type: 'TwoByTwo',
+ });
+ mediaItems = mediaItems.slice(0, mediaItems.length - 2);
+ continue;
+ }
+ if (mediaItems.length >= 1) {
+ partitionedMediaItems.unshift({
+ items: mediaItems.slice(-1),
+ type: 'OneByOne',
+ });
+ mediaItems = mediaItems.slice(0, mediaItems.length - 1);
+ continue;
+ }
+ }
+
return (
-
-
-
-
-
- {!backVisible && (
- <>
-
-
+
+
+ {partitionedMediaItems.map((item) => (
+
+ {item.items.map((mediaItem, index) => (
+
-
-
- >
- )}
-
- {items.map((item, index) => (
-
- {renderItem(galleryItemToContent(item), index)}
+ {renderItem(galleryItemToContent(mediaItem), index)}
))}
- {!frontVisible && (
- <>
-
-
-
-
- >
- )}
-
-
-
-
+
+ ))}
+
+
+ {columnItems.map((item, index) => (
+
+ {renderItem(galleryItemToContent(item), index)}
+
+ ))}
+
{renderCaption?.()}
diff --git a/src/app/components/message/MsgTypeRenderers.tsx b/src/app/components/message/MsgTypeRenderers.tsx
index 71a3add1f..e703aa252 100644
--- a/src/app/components/message/MsgTypeRenderers.tsx
+++ b/src/app/components/message/MsgTypeRenderers.tsx
@@ -454,15 +454,15 @@ export function MImage({ content, renderImageContent, outlined, fitParent }: MIm
flexGrow: 1,
flexShrink: 0,
width: fitParent ? '100%' : toRem(displayWidth),
- height: fitParent ? MAX_SIZE : 'auto',
+ height: fitParent ? '100%' : 'auto',
}}
outlined={outlined}
>
@@ -545,6 +545,7 @@ export function MVideo({