@thor
First thing I'm noticing, is that you need a way to add another row at a certain number of attachments, and I think it's gonna be tied to when you approach a square number, like 4, 9, 16, etc.
If you look at the default Mastodon layout, it adds a second row at 3 attachments. To remain consistent, in your example, you should make the 8 attachments display on 3 rows.