Files
5chan/src/components/comment-media/comment-media.tsx
T

173 lines
6.4 KiB
TypeScript
Raw Normal View History

2024-04-08 19:34:09 +02:00
import React from 'react';
import { useTranslation } from 'react-i18next';
import styles from './comment-media.module.css';
import { CommentMediaInfo, getDisplayMediaInfoType, getHasThumbnail } from '../../lib/utils/media-utils';
import { getHostname } from '../../lib/utils/url-utils';
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;
isOutOfFeed?: boolean; // virtuoso wrapper unneeded
2024-03-29 13:54:11 +01:00
isReply: boolean;
linkHeight?: number;
linkWidth?: number;
showThumbnail?: boolean;
2024-04-01 14:24:10 +02:00
setShowThumbnail: (showThumbnail: boolean) => void;
2024-03-29 13:54:11 +01:00
toggleExpanded?: () => void;
}
interface ThumbnailProps {
style: React.CSSProperties;
children: React.ReactNode;
thumbnailSmallPadding?: string;
}
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
const ThumbnailSmall = ({ style, children, thumbnailSmallPadding }: ThumbnailProps) => (
<span className={`${styles.thumbnailSmall} ${thumbnailSmallPadding}`} style={style}>
2024-03-30 16:44:37 +01:00
{children}
</span>
);
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;
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') {
displayWidth = '100%';
displayHeight = '100%';
2024-04-08 19:34:09 +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);
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') {
thumbnailComponent = iframeThumbnail ? <img src={iframeThumbnail} alt='' onClick={() => setShowThumbnail(false)} /> : null;
2024-04-12 17:47:30 +02:00
} else if (type === 'gif' && gifFrameUrl) {
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
}
const thumbnailSmallPadding = isMobile ? styles.thumbnailMobile : styles.thumbnailReplyDesktop;
const thumbnailDimensions = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties;
2024-03-30 16:44:37 +01:00
const linkWithoutThumbnail = url && new URL(url);
2024-03-30 16:44:37 +01:00
return isMobile || isReply ? (
<ThumbnailSmall style={thumbnailDimensions} thumbnailSmallPadding={thumbnailSmallPadding}>
{thumbnailComponent}
{isMobile &&
!hasThumbnail &&
linkWithoutThumbnail &&
(canEmbed(linkWithoutThumbnail) ? (
<span onClick={() => setShowThumbnail(false)}>{getHostname(url)}</span>
) : (
<a href={url} target='_blank' rel='noreferrer'>
{getHostname(url) || (url.length > 30 ? url.slice(0, 30) + '...' : url)}
</a>
))}
</ThumbnailSmall>
2024-03-30 16:44:37 +01:00
) : (
<ThumbnailBig style={thumbnailDimensions}>{thumbnailComponent}</ThumbnailBig>
2024-03-29 13:54:11 +01:00
);
};
const Media = ({ commentMediaInfo, isReply, setShowThumbnail }: MediaProps) => {
const { t } = useTranslation();
2024-04-12 17:47:30 +02:00
const { thumbnail, type, url } = commentMediaInfo || {};
const isMobile = useWindowWidth() < 640;
2024-04-01 21:33:48 +02:00
const mediaClass = isMobile ? styles.mediaMobile : isReply ? styles.mediaDesktopReply : styles.mediaDesktopOp;
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)} />
) : null}
2024-04-12 17:47:30 +02:00
{isMobile && type && (
<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}
</a>{' '}
({getDisplayMediaInfoType(type, t)})
</div>
)}
2024-04-12 17:47:30 +02:00
{isMobile && (type === 'iframe' || type === 'video' || type === 'audio') && (
<div className={styles.closeButton}>
<span className='button' onClick={() => setShowThumbnail(true)}>
{t('close')}
</span>
</div>
)}
</span>
);
};
const CommentMedia = ({ commentMediaInfo, isOutOfFeed, isReply, linkHeight, linkWidth, showThumbnail, setShowThumbnail }: MediaProps) => {
const { t } = useTranslation();
const isMobile = useWindowWidth() < 640;
const { type, url } = commentMediaInfo || {};
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}`}>
{url && (
<Thumbnail
commentMediaInfo={commentMediaInfo}
isOutOfFeed={isOutOfFeed}
isReply={isReply}
linkHeight={linkHeight}
linkWidth={linkWidth}
showThumbnail={showThumbnail}
setShowThumbnail={setShowThumbnail}
/>
)}
{isMobile && type && <div className={styles.fileInfo}>{getDisplayMediaInfoType(type, t)}</div>}
2024-04-01 14:24:10 +02:00
</span>
{!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
export default CommentMedia;