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

124 lines
4.9 KiB
TypeScript
Raw Normal View History

2024-04-01 14:24:10 +02:00
import React, { useState } from 'react';
2024-03-29 13:54:11 +01:00
import styles from './media.module.css';
import { CommentMediaInfo } from '../../lib/utils/media-utils';
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
2024-04-01 14:24:10 +02:00
import Embed from '../embed';
import { useTranslation } from 'react-i18next';
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-03-29 20:43:58 +01:00
isMobile: boolean;
2024-03-29 13:54:11 +01:00
isReply: boolean;
linkHeight?: number;
linkWidth?: number;
2024-04-01 14:24:10 +02:00
showThumbnail: boolean;
setShowThumbnail: (showThumbnail: boolean) => void;
2024-03-29 13:54:11 +01:00
toggleExpanded?: () => void;
}
interface ThumbnailProps {
style: React.CSSProperties;
children: React.ReactNode;
type?: 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, type }: ThumbnailProps) => (
2024-03-30 16:44:37 +01:00
<span className={styles.thumbnailSmall} style={style}>
{children}
</span>
);
2024-04-01 14:24:10 +02:00
const Thumbnail = ({ commentMediaInfo, isMobile, isReply, linkHeight, linkWidth }: MediaProps) => {
2024-03-30 16:44:37 +01:00
let displayWidth, displayHeight;
const maxThumbnailSize = isMobile || isReply ? 125 : 250;
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-03-30 16:44:37 +01:00
let mediaComponent: React.ReactNode = null;
const iframeThumbnail = commentMediaInfo?.patternThumbnailUrl || commentMediaInfo?.thumbnail;
2024-03-29 13:54:11 +01:00
const gifFrameUrl = useFetchGifFirstFrame(commentMediaInfo?.type === 'gif' ? commentMediaInfo.url : undefined);
if (commentMediaInfo?.type === 'image') {
mediaComponent = <img src={commentMediaInfo.url} alt='' />;
} else if (commentMediaInfo?.type === 'video') {
mediaComponent = commentMediaInfo.thumbnail ? <img src={commentMediaInfo.thumbnail} alt='' /> : <video src={`${commentMediaInfo.url}#t=0.001`} />;
} else if (commentMediaInfo?.type === 'webpage') {
mediaComponent = <img src={commentMediaInfo.thumbnail} alt='' />;
} else if (commentMediaInfo?.type === 'iframe') {
mediaComponent = iframeThumbnail ? <img src={iframeThumbnail} alt='' /> : null;
} else if (commentMediaInfo?.type === 'gif' && gifFrameUrl) {
mediaComponent = <img src={gifFrameUrl} alt='' />;
}
2024-03-30 16:44:37 +01:00
const thumbnailStyle = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties;
return isMobile || isReply ? (
<ThumbnailSmall style={thumbnailStyle} type={commentMediaInfo?.type}>
{mediaComponent}
</ThumbnailSmall>
2024-03-30 16:44:37 +01:00
) : (
<ThumbnailBig style={thumbnailStyle}>{mediaComponent}</ThumbnailBig>
2024-03-29 13:54:11 +01:00
);
};
2024-04-01 14:24:10 +02:00
const Media = ({ commentMediaInfo, isMobile, isReply, linkHeight, linkWidth, showThumbnail, setShowThumbnail }: MediaProps) => {
const { t } = useTranslation();
2024-04-01 14:24:10 +02:00
return (
<span className={styles.content}>
<span className={`${showThumbnail ? styles.show : styles.hide} ${styles.thumbnail}`} onClick={() => setShowThumbnail(false)}>
<Thumbnail
commentMediaInfo={commentMediaInfo}
isMobile={isMobile}
isReply={isReply}
linkHeight={linkHeight}
linkWidth={linkWidth}
showThumbnail={showThumbnail}
setShowThumbnail={setShowThumbnail}
/>
{isMobile && commentMediaInfo?.type && <div className={styles.fileInfo}>{commentMediaInfo.type}</div>}
2024-04-01 14:24:10 +02:00
</span>
<span className={`${showThumbnail ? styles.hide : styles.show} ${styles.media}`}>
{commentMediaInfo?.type === 'iframe' ? (
<Embed url={commentMediaInfo.url} />
) : commentMediaInfo?.type === 'gif' ? (
<img src={commentMediaInfo.url} alt='' onClick={() => setShowThumbnail(true)} />
) : commentMediaInfo?.type === 'video' ? (
<video src={commentMediaInfo.url} controls autoPlay loop muted />
) : commentMediaInfo?.type === 'image' ? (
<img src={commentMediaInfo.url} alt='' onClick={() => setShowThumbnail(true)} />
) : commentMediaInfo?.type === 'webpage' ? (
2024-04-01 15:33:30 +02:00
<img src={commentMediaInfo.thumbnail} alt='' onClick={() => setShowThumbnail(true)} />
2024-04-01 14:24:10 +02:00
) : null}
{isMobile && commentMediaInfo?.type && (
<div className={styles.fileInfo}>
<a href={commentMediaInfo.url} target='_blank' rel='noopener noreferrer'>
{commentMediaInfo.url.length > 30 ? commentMediaInfo?.url.slice(0, 30) + '...' : commentMediaInfo?.url}
</a>{' '}
({commentMediaInfo?.type})
</div>
)}
{isMobile && (commentMediaInfo?.type === 'iframe' || commentMediaInfo?.type === 'video') && (
<div className={styles.closeButton}>
<span className='button' onClick={() => setShowThumbnail(true)}>
{t('close')}
</span>
</div>
)}
2024-04-01 14:24:10 +02:00
</span>
</span>
);
2024-03-29 13:54:11 +01:00
};
2024-04-01 14:24:10 +02:00
export default Media;