2024-04-08 19:34:09 +02:00
|
|
|
import React from 'react';
|
2024-04-01 14:58:07 +02:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-28 17:40:05 +02:00
|
|
|
import styles from './comment-media.module.css';
|
|
|
|
|
import { CommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail } from '../../lib/utils/media-utils';
|
2024-04-16 17:39:33 +02:00
|
|
|
import { getHostname } from '../../lib/utils/url-utils';
|
2024-04-28 17:40:05 +02:00
|
|
|
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
|
|
|
|
|
import useWindowWidth from '../../hooks/use-window-width';
|
|
|
|
|
import Embed, { canEmbed } from '../embed';
|
2024-03-29 13:54:11 +01:00
|
|
|
|
2024-04-01 14:24:10 +02:00
|
|
|
interface MediaProps {
|
2024-03-29 13:54:11 +01:00
|
|
|
commentMediaInfo?: CommentMediaInfo;
|
2024-04-09 19:04:21 +02:00
|
|
|
isOutOfFeed?: boolean; // virtuoso wrapper unneeded
|
2024-03-29 13:54:11 +01:00
|
|
|
isReply: boolean;
|
|
|
|
|
linkHeight?: number;
|
|
|
|
|
linkWidth?: number;
|
2024-04-09 19:04:21 +02:00
|
|
|
showThumbnail?: boolean;
|
2024-04-01 14:24:10 +02:00
|
|
|
setShowThumbnail: (showThumbnail: boolean) => void;
|
2024-03-29 13:54:11 +01:00
|
|
|
toggleExpanded?: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 14:58:07 +02:00
|
|
|
interface ThumbnailProps {
|
|
|
|
|
style: React.CSSProperties;
|
|
|
|
|
children: React.ReactNode;
|
2024-04-01 17:39:10 +02:00
|
|
|
thumbnailSmallPadding?: string;
|
2024-04-01 14:58:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ThumbnailBig = ({ style, children }: ThumbnailProps) => (
|
2024-03-30 16:44:37 +01:00
|
|
|
<span className={styles.thumbnailBig} style={style}>
|
|
|
|
|
{children}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
2024-03-29 13:54:11 +01:00
|
|
|
|
2024-04-01 17:39:10 +02:00
|
|
|
const ThumbnailSmall = ({ style, children, thumbnailSmallPadding }: ThumbnailProps) => (
|
|
|
|
|
<span className={`${styles.thumbnailSmall} ${thumbnailSmallPadding}`} style={style}>
|
2024-03-30 16:44:37 +01:00
|
|
|
{children}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
|
2024-04-16 13:10:25 +02:00
|
|
|
const Thumbnail = ({ commentMediaInfo, isOutOfFeed, isReply, linkHeight, linkWidth, setShowThumbnail }: MediaProps) => {
|
2024-04-12 17:47:30 +02:00
|
|
|
const { patternThumbnailUrl, thumbnail, type, url } = commentMediaInfo || {};
|
|
|
|
|
|
2024-03-30 16:44:37 +01:00
|
|
|
let displayWidth, displayHeight;
|
2024-04-16 13:10:25 +02:00
|
|
|
const isMobile = useWindowWidth() < 640;
|
2024-03-30 16:44:37 +01:00
|
|
|
const maxThumbnailSize = isMobile || isReply ? 125 : 250;
|
2024-04-12 17:47:30 +02:00
|
|
|
|
2024-03-29 13:54:11 +01:00
|
|
|
if (linkWidth && linkHeight) {
|
2024-03-29 20:43:58 +01:00
|
|
|
let scale = Math.min(1, maxThumbnailSize / Math.max(linkWidth, linkHeight));
|
2024-03-29 13:54:11 +01:00
|
|
|
displayWidth = `${linkWidth * scale}px`;
|
|
|
|
|
displayHeight = `${linkHeight * scale}px`;
|
|
|
|
|
} else {
|
2024-03-29 20:43:58 +01:00
|
|
|
displayWidth = `${maxThumbnailSize}px`;
|
|
|
|
|
displayHeight = `${maxThumbnailSize}px`;
|
2024-03-29 13:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-12 17:47:30 +02:00
|
|
|
if (type === 'audio') {
|
2024-04-16 17:39:33 +02:00
|
|
|
displayWidth = '100%';
|
|
|
|
|
displayHeight = '100%';
|
2024-04-08 19:34:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-09 19:04:21 +02:00
|
|
|
if (isOutOfFeed) {
|
|
|
|
|
displayWidth = 'unset';
|
|
|
|
|
displayHeight = 'unset';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let thumbnailComponent: React.ReactNode = null;
|
2024-04-12 17:47:30 +02:00
|
|
|
const iframeThumbnail = patternThumbnailUrl || thumbnail;
|
|
|
|
|
const gifFrameUrl = useFetchGifFirstFrame(type === 'gif' ? url : undefined);
|
2024-04-16 17:39:33 +02:00
|
|
|
const hasThumbnail = getHasThumbnail(commentMediaInfo, url);
|
2024-04-12 17:47:30 +02:00
|
|
|
|
|
|
|
|
if (type === 'image') {
|
|
|
|
|
thumbnailComponent = <img src={url} alt='' onClick={() => setShowThumbnail(false)} />;
|
|
|
|
|
} else if (type === 'video') {
|
|
|
|
|
thumbnailComponent = thumbnail ? <img src={thumbnail} alt='' /> : <video src={`${url}#t=0.001`} onClick={() => setShowThumbnail(false)} />;
|
|
|
|
|
} else if (type === 'webpage') {
|
|
|
|
|
thumbnailComponent = <img src={thumbnail} alt='' onClick={() => setShowThumbnail(false)} />;
|
|
|
|
|
} else if (type === 'iframe') {
|
2024-04-09 19:04:21 +02:00
|
|
|
thumbnailComponent = iframeThumbnail ? <img src={iframeThumbnail} alt='' onClick={() => setShowThumbnail(false)} /> : null;
|
2024-04-12 17:47:30 +02:00
|
|
|
} else if (type === 'gif' && gifFrameUrl) {
|
2024-04-09 19:04:21 +02:00
|
|
|
thumbnailComponent = <img src={gifFrameUrl} alt='' onClick={() => setShowThumbnail(false)} />;
|
2024-04-12 17:47:30 +02:00
|
|
|
} else if (type === 'audio') {
|
|
|
|
|
thumbnailComponent = <audio src={url} controls />;
|
2024-03-29 13:54:11 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-01 17:39:10 +02:00
|
|
|
const thumbnailSmallPadding = isMobile ? styles.thumbnailMobile : styles.thumbnailReplyDesktop;
|
|
|
|
|
const thumbnailDimensions = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties;
|
2024-03-30 16:44:37 +01:00
|
|
|
|
2024-04-16 17:39:33 +02:00
|
|
|
const linkWithoutThumbnail = url && new URL(url);
|
|
|
|
|
|
2024-03-30 16:44:37 +01:00
|
|
|
return isMobile || isReply ? (
|
2024-04-01 17:39:10 +02:00
|
|
|
<ThumbnailSmall style={thumbnailDimensions} thumbnailSmallPadding={thumbnailSmallPadding}>
|
2024-04-09 19:04:21 +02:00
|
|
|
{thumbnailComponent}
|
2024-04-16 21:37:42 +02:00
|
|
|
{isMobile &&
|
|
|
|
|
!hasThumbnail &&
|
|
|
|
|
linkWithoutThumbnail &&
|
|
|
|
|
(canEmbed(linkWithoutThumbnail) ? (
|
2024-04-16 17:39:33 +02:00
|
|
|
<span onClick={() => setShowThumbnail(false)}>{getHostname(url)}</span>
|
|
|
|
|
) : (
|
|
|
|
|
<a href={url} target='_blank' rel='noreferrer'>
|
2024-04-16 21:37:42 +02:00
|
|
|
{getHostname(url) || (url.length > 30 ? url.slice(0, 30) + '...' : url)}
|
2024-04-16 17:39:33 +02:00
|
|
|
</a>
|
2024-04-16 21:37:42 +02:00
|
|
|
))}
|
2024-04-01 14:58:07 +02:00
|
|
|
</ThumbnailSmall>
|
2024-03-30 16:44:37 +01:00
|
|
|
) : (
|
2024-04-09 19:04:21 +02:00
|
|
|
<ThumbnailBig style={thumbnailDimensions}>{thumbnailComponent}</ThumbnailBig>
|
2024-03-29 13:54:11 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-16 13:10:25 +02:00
|
|
|
const Media = ({ commentMediaInfo, isReply, setShowThumbnail }: MediaProps) => {
|
2024-04-01 14:58:07 +02:00
|
|
|
const { t } = useTranslation();
|
2024-04-12 17:47:30 +02:00
|
|
|
const { thumbnail, type, url } = commentMediaInfo || {};
|
2024-04-16 13:10:25 +02:00
|
|
|
const isMobile = useWindowWidth() < 640;
|
2024-04-01 21:33:48 +02:00
|
|
|
const mediaClass = isMobile ? styles.mediaMobile : isReply ? styles.mediaDesktopReply : styles.mediaDesktopOp;
|
2024-04-01 17:39:10 +02:00
|
|
|
|
2024-04-09 19:04:21 +02:00
|
|
|
return (
|
|
|
|
|
<span className={mediaClass}>
|
2024-04-12 17:47:30 +02:00
|
|
|
{type === 'iframe' && url ? (
|
|
|
|
|
<Embed url={url} />
|
|
|
|
|
) : type === 'gif' ? (
|
|
|
|
|
<img src={url} alt='' onClick={() => setShowThumbnail(true)} />
|
|
|
|
|
) : type === 'video' ? (
|
|
|
|
|
<video src={url} controls autoPlay loop muted />
|
|
|
|
|
) : type === 'image' ? (
|
|
|
|
|
<img src={url} alt='' onClick={() => setShowThumbnail(true)} />
|
|
|
|
|
) : type === 'webpage' ? (
|
|
|
|
|
<img src={thumbnail} alt='' onClick={() => setShowThumbnail(true)} />
|
2024-04-09 19:04:21 +02:00
|
|
|
) : null}
|
2024-04-12 17:47:30 +02:00
|
|
|
{isMobile && type && (
|
2024-04-09 19:04:21 +02:00
|
|
|
<div className={styles.fileInfo}>
|
2024-04-12 17:47:30 +02:00
|
|
|
<a href={url} target='_blank' rel='noopener noreferrer'>
|
|
|
|
|
{url && url.length > 30 ? url.slice(0, 30) + '...' : url}
|
2024-04-09 19:04:21 +02:00
|
|
|
</a>{' '}
|
2024-04-28 17:40:05 +02:00
|
|
|
({getDisplayMediaInfoType(type, t)})
|
2024-04-09 19:04:21 +02:00
|
|
|
</div>
|
|
|
|
|
)}
|
2024-04-12 17:47:30 +02:00
|
|
|
{isMobile && (type === 'iframe' || type === 'video' || type === 'audio') && (
|
2024-04-09 19:04:21 +02:00
|
|
|
<div className={styles.closeButton}>
|
|
|
|
|
<span className='button' onClick={() => setShowThumbnail(true)}>
|
|
|
|
|
{t('close')}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-16 13:10:25 +02:00
|
|
|
const CommentMedia = ({ commentMediaInfo, isOutOfFeed, isReply, linkHeight, linkWidth, showThumbnail, setShowThumbnail }: MediaProps) => {
|
2024-04-28 17:40:05 +02:00
|
|
|
const { t } = useTranslation();
|
2024-04-16 13:10:25 +02:00
|
|
|
const isMobile = useWindowWidth() < 640;
|
2024-04-16 21:37:42 +02:00
|
|
|
const { type, url } = commentMediaInfo || {};
|
2024-04-28 17:40:05 +02:00
|
|
|
|
2024-04-01 14:24:10 +02:00
|
|
|
return (
|
|
|
|
|
<span className={styles.content}>
|
2024-04-08 19:34:09 +02:00
|
|
|
<span className={`${showThumbnail ? styles.show : styles.hide} ${styles.thumbnail}`}>
|
2024-04-16 21:37:42 +02:00
|
|
|
{url && (
|
|
|
|
|
<Thumbnail
|
|
|
|
|
commentMediaInfo={commentMediaInfo}
|
|
|
|
|
isOutOfFeed={isOutOfFeed}
|
|
|
|
|
isReply={isReply}
|
|
|
|
|
linkHeight={linkHeight}
|
|
|
|
|
linkWidth={linkWidth}
|
|
|
|
|
showThumbnail={showThumbnail}
|
|
|
|
|
setShowThumbnail={setShowThumbnail}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2024-04-28 17:40:05 +02:00
|
|
|
{isMobile && type && <div className={styles.fileInfo}>{getDisplayMediaInfoType(type, t)}</div>}
|
2024-04-01 14:24:10 +02:00
|
|
|
</span>
|
2024-04-16 13:10:25 +02:00
|
|
|
{!showThumbnail && <Media commentMediaInfo={commentMediaInfo} isReply={isReply} setShowThumbnail={setShowThumbnail} />}
|
2024-04-01 14:24:10 +02:00
|
|
|
</span>
|
|
|
|
|
);
|
2024-03-29 13:54:11 +01:00
|
|
|
};
|
2024-04-01 14:24:10 +02:00
|
|
|
|
2024-04-09 19:04:21 +02:00
|
|
|
export default CommentMedia;
|