import React, { useState } from 'react'; import { useTranslation } from 'react-i18next'; import { Comment } from '@plebbit/plebbit-react-hooks'; import styles from './comment-media.module.css'; import { CommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail, getMediaDimensions } from '../../lib/utils/media-utils'; import { getHostname } from '../../lib/utils/url-utils'; import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame'; import useIsMobile from '../../hooks/use-is-mobile'; import Embed, { canEmbed } from '../embed'; interface MediaProps { commentMediaInfo?: CommentMediaInfo; post?: Comment; isFloatingEmbed?: boolean; isReply?: boolean; linkHeight?: number; linkWidth?: number; showThumbnail?: boolean; setShowThumbnail: (showThumbnail: boolean) => void; } const Thumbnail = ({ commentMediaInfo, isFloatingEmbed, post, setShowThumbnail }: MediaProps) => { const { deleted, linkHeight, linkWidth, parentCid, removed, spoiler } = post || {}; const { isDescription, isRules } = post || {}; // custom properties, not from api const isOutOfFeed = isDescription || isRules || isFloatingEmbed; // virtuoso wrapper unneeded const isReply = parentCid; const isDeleted = deleted || removed; const { patternThumbnailUrl, thumbnail, type, url } = commentMediaInfo || {}; const [hasError, setHasError] = useState(false); const handleError = () => setHasError(true); let displayWidth, displayHeight; const isMobile = useIsMobile(); const maxThumbnailSize = isMobile || isReply ? 125 : 250; if (linkWidth && linkHeight) { let scale = Math.min(1, maxThumbnailSize / Math.max(linkWidth, linkHeight)); displayWidth = `${linkWidth * scale}px`; displayHeight = `${linkHeight * scale}px`; } else { displayWidth = `${maxThumbnailSize}px`; displayHeight = `${maxThumbnailSize}px`; } if (type === 'audio') { displayWidth = '100%'; displayHeight = '100%'; } 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)} />; } else if (type === 'video') { thumbnailComponent = thumbnail ? ( ) : ( // show first frame of the video, as a workaround for Safari not loading thumbnails setShowThumbnail(false)} /> ); } else if (type === 'webpage') { thumbnailComponent = setShowThumbnail(false)} />; } else if (type === 'iframe') { thumbnailComponent = iframeThumbnail ? setShowThumbnail(false)} /> : null; } else if (type === 'audio') { thumbnailComponent = ; } const thumbnailSmallPadding = isMobile ? styles.thumbnailMobile : styles.thumbnailReplyDesktop; const thumbnailDimensions = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties; const linkWithoutThumbnail = url && new URL(url); return hasError || isDeleted ? ( ) : spoiler ? ( setShowThumbnail(false)} /> ) : isOutOfFeed ? ( {thumbnailComponent} ) : isMobile || isReply ? ( {thumbnailComponent} {isMobile && !hasThumbnail && linkWithoutThumbnail && (canEmbed(linkWithoutThumbnail) ? ( setShowThumbnail(false)}>{getHostname(url)} ) : ( {getHostname(url) || (url.length > 30 ? url.slice(0, 30) + '...' : url)} ))} ) : ( {thumbnailComponent} ); }; const Media = ({ commentMediaInfo, isReply, setShowThumbnail }: MediaProps) => { const { t } = useTranslation(); const { thumbnail, type, url } = commentMediaInfo || {}; const isMobile = useIsMobile(); const mediaClass = isMobile ? styles.mediaMobile : isReply ? styles.mediaDesktopReply : styles.mediaDesktopOp; const mediaDimensions = getMediaDimensions(commentMediaInfo); return ( {type === 'iframe' && url ? ( ) : type === 'gif' ? ( setShowThumbnail(true)} /> ) : type === 'video' ? ( ) : type === 'image' ? ( setShowThumbnail(true)} /> ) : type === 'webpage' ? ( setShowThumbnail(true)} /> ) : null} {isMobile && type && ( {url && url.length > 30 ? url.slice(0, 30) + '...' : url} {' '} ({getDisplayMediaInfoType(type, t)} {mediaDimensions && `, ${mediaDimensions}`}) )} {isMobile && (type === 'iframe' || type === 'video' || type === 'audio') && ( setShowThumbnail(true)}> {t('close')} )} ); }; const CommentMedia = ({ commentMediaInfo, isFloatingEmbed, post, showThumbnail, setShowThumbnail }: MediaProps) => { const { t } = useTranslation(); const isMobile = useIsMobile(); const { url } = commentMediaInfo || {}; let type = commentMediaInfo?.type; const gifFrameUrl = useFetchGifFirstFrame(url); if (type === 'gif' && gifFrameUrl) { type = 'animated gif'; } else if (type === 'gif' && !gifFrameUrl) { type = 'static gif'; } return ( {url && } {isMobile && type && {`${post?.spoiler ? `${t('spoiler')} - ` : ''} ${getDisplayMediaInfoType(type, t)}`}} {!showThumbnail && } ); }; export default CommentMedia;