From 4e438a96c47e1ec42e684a29a4ccfeb5ace2fd67 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 10 May 2026 16:43:26 +0300 Subject: [PATCH] better attachment clustering, show 4-attachment clusters in 2x2 layout instead of 1x4 --- src/components/gallery/gallery.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/components/gallery/gallery.js b/src/components/gallery/gallery.js index fafb66dbd..e3676cdc3 100644 --- a/src/components/gallery/gallery.js +++ b/src/components/gallery/gallery.js @@ -42,11 +42,17 @@ const Gallery = { if (this.size === 'hide') { return attachments.map((item) => ({ minimal: true, items: [item] })) } + console.log('<=') const rows = this.grid ? [{ grid: true, items: attachments }] : attachments .reduce( (acc, attachment, i) => { + const peek = attachments[i+1] + const nextEnd = peek == null + const nextWide = !nextEnd && !displayTypes.has(peek?.type) + + // Inserting new row if (attachment.type === 'audio') { return [ ...acc, @@ -61,18 +67,16 @@ const Gallery = { { items: [] }, ] } + const maxPerRow = 3 - const attachmentsRemaining = this.attachments.length - i + 1 const currentRow = acc[acc.length - 1].items - currentRow.push(attachment) - if ( - currentRow.length >= maxPerRow && - attachmentsRemaining > maxPerRow - ) { - return [...acc, { items: [] }] + if ((nextWide || nextEnd) && currentRow.length >= maxPerRow) { + const last = currentRow.splice(-1)[0] + return [...acc, { items: [last, attachment] }] } else { - return acc + currentRow.push(attachment) } + return acc }, [{ items: [] }], )