import { Link, useLocation } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { Comment } from '@plebbit/plebbit-react-hooks';
import styles from './catalog-row.module.css';
import { getCommentMediaInfo, getHasThumbnail } from '../../lib/utils/media-utils';
import { isAllView } from '../../lib/utils/view-utils';
import useFetchGifFirstFrame from '../../hooks/use-fetch-gif-first-frame';
import useCountLinksInReplies from '../../hooks/use-count-links-in-replies';
import PostMenuDesktop from '../post/post-desktop/post-menu-desktop/';
import React, { useState } from 'react';
interface CatalogPostMediaProps {
commentMediaInfo: any;
isOutOfFeed?: boolean;
linkWidth?: number;
linkHeight?: number;
}
export const CatalogPostMedia = ({ commentMediaInfo, isOutOfFeed, linkWidth, linkHeight }: CatalogPostMediaProps) => {
const { patternThumbnailUrl, thumbnail, type, url } = commentMediaInfo || {};
const iframeThumbnail = patternThumbnailUrl || thumbnail;
const gifFrameUrl = useFetchGifFirstFrame(type === 'gif' ? url : undefined);
const [isLoaded, setIsLoaded] = useState(false);
const [hasError, setHasError] = useState(false);
const handleLoad = () => setIsLoaded(true);
const handleError = () => setHasError(true);
const loadingStyle = { display: isLoaded ? 'block' : 'none' };
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`;
}
if (type === 'audio' || isOutOfFeed) {
displayWidth = 'unset';
displayHeight = 'unset';
}
const thumbnailDimensions = { '--width': displayWidth, '--height': displayHeight } as React.CSSProperties;
let thumbnailComponent: React.ReactNode = null;
if (type === 'image' && !hasError) {
thumbnailComponent = ;
} else if (type === 'video' && !hasError) {
thumbnailComponent = thumbnail ? (
) : (
);
} else if (type === 'webpage' && !hasError) {
thumbnailComponent =
;
} else if (type === 'iframe' && iframeThumbnail && !hasError) {
thumbnailComponent =
;
} else if (type === 'gif' && gifFrameUrl && !hasError) {
thumbnailComponent =
;
} else if (type === 'audio') {
thumbnailComponent = ;
}
return (
: thumbnailComponent}