From 6e28d8808b597085b10a40308ebc0858ff9ad525 Mon Sep 17 00:00:00 2001 From: thislight Date: Tue, 29 Oct 2024 19:35:40 +0800 Subject: [PATCH] MediaAttachmentGrid: reduce closure creation --- src/timelines/MediaAttachmentGrid.tsx | 94 +++++++++++++-------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/src/timelines/MediaAttachmentGrid.tsx b/src/timelines/MediaAttachmentGrid.tsx index c018c89..c9da6a3 100644 --- a/src/timelines/MediaAttachmentGrid.tsx +++ b/src/timelines/MediaAttachmentGrid.tsx @@ -2,6 +2,7 @@ import type { mastodon } from "masto"; import { type Component, For, + Index, createMemo, createRenderEffect, createSignal, @@ -103,6 +104,31 @@ const MediaAttachmentGrid: Component<{ }; }); + const itemStyle = (item: mastodon.v1.MediaAttachment) => { + const { width, height } = constraintedSize( + item.meta?.small || { width: 1, height: 1 }, + { width: 2, height: 2 }, + itemMaxSize(), + ); + + // I don't know why mastodon does not return this + // and the condition for it to return this. + // Anyway, It is useless now. + // My hope is the FastAverageColor, but + // we may need better tool to manage the performance impact + // before using this. See #37. + // TODO: use fast average color to extract accent color + const accentColor = item.meta?.colors?.accent; + + return Object.assign( + { + width: `${width}px`, + height: `${height}px`, + }, + accentColor ? { "--media-color-accent": accentColor } : {}, + ); + }; + css` .attachments { column-count: ${columnCount().toString()}; @@ -118,74 +144,48 @@ const MediaAttachmentGrid: Component<{ } }} > - + {(item, index) => { - // I don't know why mastodon does not return this - // and the condition for it to return this. - // Anyway, It is useless now. - // My hope is the FastAverageColor, but - // we may need better tool to manage the performance impact - // before using this. See #37. - // TODO: use fast average color to extract accent color - const accentColor = item.meta?.colors?.accent; - const { width, height } = item.meta?.small || {}; - - const size = () => { - return constraintedSize( - item.meta?.small || { width: 1, height: 1 }, - { width: 2, height: 2 }, - itemMaxSize(), - ); - }; - - const style = () => { - return Object.assign( - { - width: `${size().width}px`, - height: `${size().height}px`, - }, - accentColor ? { "--media-color-accent": accentColor } : {}, - ); - }; - - switch (item.type) { + switch (item().type) { case "image": return ( {item.description ); case "video": return ( + ); };