From e8c60fc5bec7ce7ee2afa809b1192a979e129027 Mon Sep 17 00:00:00 2001 From: "plebeius.eth" Date: Tue, 9 Apr 2024 19:04:21 +0200 Subject: [PATCH] fix(comment media): rename, refactor, fix performance --- .../comment-media.module.css} | 14 +-- .../comment-media.tsx} | 97 +++++++++++-------- src/components/comment-media/index.ts | 1 + src/components/media/index.ts | 1 - src/components/post/post.tsx | 12 ++- 5 files changed, 67 insertions(+), 58 deletions(-) rename src/components/{media/media.module.css => comment-media/comment-media.module.css} (94%) rename src/components/{media/media.tsx => comment-media/comment-media.tsx} (52%) create mode 100644 src/components/comment-media/index.ts delete mode 100644 src/components/media/index.ts diff --git a/src/components/media/media.module.css b/src/components/comment-media/comment-media.module.css similarity index 94% rename from src/components/media/media.module.css rename to src/components/comment-media/comment-media.module.css index d916205c..d44259a7 100644 --- a/src/components/media/media.module.css +++ b/src/components/comment-media/comment-media.module.css @@ -2,6 +2,8 @@ background-color: var(--media-thumbnail-background-color); width: var(--width); height: var(--height); + max-width: 250px; + max-height: 250px; margin: 3px 20px 5px 0; display: flex; justify-content: center; @@ -12,6 +14,8 @@ background-color: var(--media-thumbnail-background-color); width: var(--width); height: var(--height); + max-width: 125px; + max-height: 125px; margin: 3px 10px 5px 5px; display: flex; justify-content: center; @@ -22,16 +26,6 @@ float: left; } -.thumbnailBig img, .thumbnailBig video { - max-width: 250px; - max-height: 250px; -} - -.thumbnailSmall img, .thumbnailSmall video { - max-width: 125px; - max-height: 125px; -} - .hide { display: none !important; } diff --git a/src/components/media/media.tsx b/src/components/comment-media/comment-media.tsx similarity index 52% rename from src/components/media/media.tsx rename to src/components/comment-media/comment-media.tsx index 79fbb8fc..e944a3a4 100644 --- a/src/components/media/media.tsx +++ b/src/components/comment-media/comment-media.tsx @@ -1,5 +1,5 @@ import React from 'react'; -import styles from './media.module.css'; +import styles from './comment-media.module.css'; import { CommentMediaInfo } from '../../lib/utils/media-utils'; import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame'; import Embed from '../embed'; @@ -8,10 +8,11 @@ import { useTranslation } from 'react-i18next'; interface MediaProps { commentMediaInfo?: CommentMediaInfo; isMobile: boolean; + isOutOfFeed?: boolean; // virtuoso wrapper unneeded isReply: boolean; linkHeight?: number; linkWidth?: number; - showThumbnail: boolean; + showThumbnail?: boolean; setShowThumbnail: (showThumbnail: boolean) => void; toggleExpanded?: () => void; } @@ -34,7 +35,7 @@ const ThumbnailSmall = ({ style, children, thumbnailSmallPadding }: ThumbnailPro ); -const Thumbnail = ({ commentMediaInfo, isMobile, isReply, linkHeight, linkWidth, setShowThumbnail }: MediaProps) => { +const Thumbnail = ({ commentMediaInfo, isMobile, isOutOfFeed, isReply, linkHeight, linkWidth, setShowThumbnail }: MediaProps) => { let displayWidth, displayHeight; const maxThumbnailSize = isMobile || isReply ? 125 : 250; if (linkWidth && linkHeight) { @@ -51,25 +52,30 @@ const Thumbnail = ({ commentMediaInfo, isMobile, isReply, linkHeight, linkWidth, displayHeight = '75px'; } - let mediaComponent: React.ReactNode = null; + if (isOutOfFeed) { + displayWidth = 'unset'; + displayHeight = 'unset'; + } + + let thumbnailComponent: React.ReactNode = null; const iframeThumbnail = commentMediaInfo?.patternThumbnailUrl || commentMediaInfo?.thumbnail; const gifFrameUrl = useFetchGifFirstFrame(commentMediaInfo?.type === 'gif' ? commentMediaInfo.url : undefined); if (commentMediaInfo?.type === 'image') { - mediaComponent = setShowThumbnail(false)} />; + thumbnailComponent = setShowThumbnail(false)} />; } else if (commentMediaInfo?.type === 'video') { - mediaComponent = commentMediaInfo.thumbnail ? ( + thumbnailComponent = commentMediaInfo.thumbnail ? ( ) : (