diff --git a/src/components/comment-media/comment-media.tsx b/src/components/comment-media/comment-media.tsx index c81e97ba..78f6fe3c 100644 --- a/src/components/comment-media/comment-media.tsx +++ b/src/components/comment-media/comment-media.tsx @@ -29,18 +29,14 @@ interface MediaProps { const Thumbnail = ({ commentMediaInfo, deleted, displayHeight, displayWidth, isFloatingEmbed, isOutOfFeed, isReply, removed, spoiler, setShowThumbnail }: MediaProps) => { const isMobile = useIsMobile(); const { patternThumbnailUrl, thumbnail, type, url } = commentMediaInfo || {}; - const [hasError, setHasError] = useState(false); - const handleError = () => setHasError(true); let thumbnailComponent: React.ReactNode = null; const iframeThumbnail = patternThumbnailUrl || thumbnail; const gifFrameUrl = useFetchGifFirstFrame(type === 'gif' ? url : undefined); const hasThumbnail = getHasThumbnail(commentMediaInfo, url); - if (type === 'gif' && gifFrameUrl) { - thumbnailComponent = setShowThumbnail(false)} />; - } else if (type === 'image') { - thumbnailComponent = setShowThumbnail(false)} />; + if (type === 'gif') { + thumbnailComponent = setShowThumbnail(false)} />; } else if (type === 'video') { thumbnailComponent = thumbnail ? ( @@ -61,7 +57,7 @@ const Thumbnail = ({ commentMediaInfo, deleted, displayHeight, displayWidth, isF const linkWithoutThumbnail = url && new URL(url); - return hasError || deleted || removed ? ( + return deleted || removed ? ( ) : spoiler ? ( setShowThumbnail(false)} /> @@ -106,8 +102,6 @@ const Media = ({ commentMediaInfo, isReply, setShowThumbnail }: MediaProps) => { setShowThumbnail(true)} /> ) : type === 'video' ? ( - ) : type === 'image' ? ( - setShowThumbnail(true)} /> ) : type === 'webpage' ? ( setShowThumbnail(true)} /> ) : null} @@ -131,12 +125,75 @@ const Media = ({ commentMediaInfo, isReply, setShowThumbnail }: MediaProps) => { ); }; +interface ImageProps { + commentMediaInfo: CommentMediaInfo; + displayHeight: string; + displayWidth: string; + isOutOfFeed: boolean; + post: Comment | undefined; +} + +const Image = ({ commentMediaInfo, displayHeight, displayWidth, isOutOfFeed, post }: ImageProps) => { + const { t } = useTranslation(); + const { parentCid } = post || {}; + const { type, url } = commentMediaInfo || {}; + const isReply = parentCid; + const isMobile = useIsMobile(); + const [isImageExpanded, setIsImageExpanded] = useState(false); + const { fitExpandedImagesToScreen } = useExpandedMediaStore(); + const mediaDimensions = getMediaDimensions(commentMediaInfo); + const mediaClass = `${isMobile ? styles.mediaMobile : isReply ? styles.mediaDesktopReply : styles.mediaDesktopOp} ${ + fitExpandedImagesToScreen ? styles.fitToScreen : '' + }`; + const thumbnailSmallPadding = isMobile ? styles.thumbnailMobile : styles.thumbnailReplyDesktop; + const thumbnailDimensions = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties; + + const [hasError, setHasError] = useState(false); + const handleError = () => setHasError(true); + + return isMobile ? ( + + + {hasError ? ( + + ) : ( + setIsImageExpanded(!isImageExpanded)} /> + )} + + {isImageExpanded && type && ( + + + {url && url.length > 30 ? url.slice(0, 30) + '...' : url} + {' '} + ({getDisplayMediaInfoType(type, t)} + {mediaDimensions && `, ${mediaDimensions}`}) + + )} + {type && !isImageExpanded && {`${post?.spoiler ? `${t('spoiler')} - ` : ''} ${getDisplayMediaInfoType(type, t)}`}} + + ) : ( + + {hasError ? ( + + ) : ( + setIsImageExpanded(!isImageExpanded)} /> + )} + + ); +}; + const CommentMedia = ({ commentMediaInfo, isFloatingEmbed, post, showThumbnail, setShowThumbnail }: MediaProps) => { const { deleted, linkHeight, linkWidth, parentCid, removed, spoiler } = post || {}; const isReply = parentCid; const { t } = useTranslation(); const isMobile = useIsMobile(); - const { url, thumbnailWidth, thumbnailHeight } = commentMediaInfo || {}; + const { thumbnailHeight, thumbnailWidth, url } = commentMediaInfo || {}; let type = commentMediaInfo?.type; const gifFrameUrl = useFetchGifFirstFrame(url); @@ -172,45 +229,11 @@ const CommentMedia = ({ commentMediaInfo, isFloatingEmbed, post, showThumbnail, const { isDescription, isRules } = post || {}; // custom properties, not from api const isOutOfFeed = isDescription || isRules || isFloatingEmbed; // virtuoso wrapper unneeded - const [isImageExpanded, setIsImageExpanded] = useState(false); - const { fitExpandedImagesToScreen } = useExpandedMediaStore(); - const mediaDimensions = getMediaDimensions(commentMediaInfo); - const mediaClass = `${isMobile ? styles.mediaMobile : isReply ? styles.mediaDesktopReply : styles.mediaDesktopOp} ${ - fitExpandedImagesToScreen ? styles.fitToScreen : '' - }`; - const thumbnailSmallPadding = isMobile ? styles.thumbnailMobile : styles.thumbnailReplyDesktop; - const thumbnailDimensions = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties; - return ( {commentMediaInfo?.type === 'image' ? ( - isMobile ? ( - - - setIsImageExpanded(!isImageExpanded)} /> - - {isImageExpanded && type && ( - - - {url && url.length > 30 ? url.slice(0, 30) + '...' : url} - {' '} - ({getDisplayMediaInfoType(type, t)} - {mediaDimensions && `, ${mediaDimensions}`}) - - )} - {type && !isImageExpanded && {`${post?.spoiler ? `${t('spoiler')} - ` : ''} ${getDisplayMediaInfoType(type, t)}`}} - - ) : ( - - setIsImageExpanded(!isImageExpanded)} /> - - ) + // images just enlarge when clicked, so they don't need two separate components + ) : ( <>