Files
5chan/src/components/catalog-row/catalog-row.tsx
T

125 lines
4.2 KiB
TypeScript
Raw Normal View History

2024-05-17 17:25:25 +02:00
import { Link, useLocation } from 'react-router-dom';
2024-04-12 20:48:46 +02:00
import { useTranslation } from 'react-i18next';
2024-04-12 14:28:06 +02:00
import { Comment } from '@plebbit/plebbit-react-hooks';
2024-04-12 17:47:30 +02:00
import { getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils';
2024-05-17 17:25:25 +02:00
import { isAllView } from '../../lib/utils/view-utils';
2024-04-12 17:47:30 +02:00
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
import styles from './catalog-row.module.css';
2024-05-28 16:44:56 +02:00
import PostMenu from '../post/post-menu/post-menu';
2024-04-12 17:47:30 +02:00
2024-05-02 22:09:41 +02:00
export const CatalogPostMedia = ({ commentMediaInfo }: { commentMediaInfo: any }) => {
2024-04-12 17:47:30 +02:00
const { patternThumbnailUrl, thumbnail, type, url } = commentMediaInfo || {};
const iframeThumbnail = patternThumbnailUrl || thumbnail;
const gifFrameUrl = useFetchGifFirstFrame(type === 'gif' ? url : undefined);
let thumbnailComponent: React.ReactNode = null;
if (type === 'image') {
thumbnailComponent = <img src={url} alt='' />;
} else if (type === 'video') {
thumbnailComponent = thumbnail ? <img src={thumbnail} alt='' /> : <video src={`${url}#t=0.001`} />;
} else if (type === 'webpage') {
thumbnailComponent = <img src={thumbnail} alt='' />;
} else if (type === 'iframe') {
thumbnailComponent = iframeThumbnail ? <img src={iframeThumbnail} alt='' /> : null;
} else if (type === 'gif' && gifFrameUrl) {
thumbnailComponent = <img src={gifFrameUrl} alt='' />;
} else if (type === 'audio') {
thumbnailComponent = <audio src={url} controls />;
}
return thumbnailComponent;
};
2024-04-12 14:28:06 +02:00
2024-05-28 16:44:56 +02:00
const CatalogPost = ({ post }: { post: Comment }) => {
2024-04-12 20:48:46 +02:00
const { t } = useTranslation();
const { cid, content, isDescription, isRules, link, linkHeight, linkWidth, locked, pinned, replyCount, subplebbitAddress, title } = post || {};
2024-04-12 17:47:30 +02:00
const commentMediaInfo = getCommentMediaInfo(post);
const { type } = commentMediaInfo || {};
const hasThumbnail = getHasThumbnail(commentMediaInfo, link);
2024-04-12 14:28:06 +02:00
2024-04-12 17:47:30 +02:00
let displayWidth, displayHeight;
const maxThumbnailSize = 150;
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`;
}
2024-04-14 21:28:48 +02:00
if (type === 'audio' || isDescription || isRules) {
2024-04-12 20:48:46 +02:00
displayWidth = 'unset';
displayHeight = 'unset';
}
2024-04-12 17:47:30 +02:00
const thumbnailDimensions = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties;
2024-05-17 17:25:25 +02:00
const location = useLocation();
const isInAllView = isAllView(location.pathname);
const postLink = isInAllView && isDescription ? `/p/all/description` : `/p/${subplebbitAddress}/${isDescription ? 'description' : isRules ? 'rules' : `c/${cid}`}`;
2024-04-12 17:47:30 +02:00
const linkCount = useCountLinksInReplies(post);
2024-04-12 20:48:46 +02:00
const threadIcons = (
<div className={styles.threadIcons}>
{pinned && <span className={styles.stickyIcon} title={t('sticky')} />}
{locked && <span className={styles.closedIcon} title={t('closed')} />}
</div>
);
2024-04-12 17:47:30 +02:00
return (
<div className={styles.post}>
2024-04-12 20:48:46 +02:00
{hasThumbnail ? (
2024-04-12 17:47:30 +02:00
<Link to={postLink}>
<div className={styles.mediaPaddingWrapper}>
2024-04-12 20:48:46 +02:00
{threadIcons}
2024-04-12 17:47:30 +02:00
<div className={styles.mediaWrapper} style={thumbnailDimensions}>
2024-05-02 22:09:41 +02:00
<CatalogPostMedia commentMediaInfo={commentMediaInfo} />
2024-04-12 17:47:30 +02:00
</div>
</div>
</Link>
2024-04-12 20:48:46 +02:00
) : (
threadIcons
2024-04-12 17:47:30 +02:00
)}
<div className={styles.meta}>
R: <b>{replyCount || '0'}</b>
{linkCount > 0 && (
<span>
{' '}
/ L: <b>{linkCount}</b>
</span>
)}
2024-05-28 16:44:56 +02:00
<span className={styles.postMenu}>
<PostMenu post={post} />
</span>
2024-04-12 17:47:30 +02:00
</div>
<Link to={postLink}>
<div className={styles.teaser}>
<b>{title && `${title}${content ? ': ' : ''}`}</b>
{content}
</div>
</Link>
</div>
);
2024-04-12 14:28:06 +02:00
};
interface CatalogRowProps {
2024-04-14 16:45:59 +02:00
index?: number;
2024-04-12 14:28:06 +02:00
row: Comment[];
}
2024-04-14 16:45:59 +02:00
const CatalogRow = ({ row }: CatalogRowProps) => {
return (
<div className={styles.row}>
{row.map((post, index) => (
2024-05-28 16:44:56 +02:00
<CatalogPost key={index} post={post} />
))}
</div>
);
2024-04-12 14:28:06 +02:00
};
export default CatalogRow;