import React, { memo } from '../../lib/teact/teact'; import { withGlobal } from '../../global'; import type { ApiDimensions, ApiMessage, ApiSponsoredMessage, } from '../../api/types'; import type { MediaViewerOrigin } from '../../types'; import type { MediaViewerItem } from './helpers/getViewableMedia'; import { selectIsMessageProtected, selectTabState, } from '../../global/selectors'; import buildClassName from '../../util/buildClassName'; import stopEvent from '../../util/stopEvent'; import { ARE_WEBCODECS_SUPPORTED, IS_TOUCH_ENV } from '../../util/windowEnvironment'; import { calculateMediaViewerDimensions } from '../common/helpers/mediaDimensions'; import { renderMessageText } from '../common/helpers/renderMessageText'; import getViewableMedia from './helpers/getViewableMedia'; import useAppLayout from '../../hooks/useAppLayout'; import useLastCallback from '../../hooks/useLastCallback'; import useOldLang from '../../hooks/useOldLang'; import useControlsSignal from './hooks/useControlsSignal'; import { useMediaProps } from './hooks/useMediaProps'; import Spinner from '../ui/Spinner'; import MediaViewerFooter from './MediaViewerFooter'; import VideoPlayer from './VideoPlayer'; import './MediaViewerContent.scss'; type OwnProps = { item: MediaViewerItem; isActive?: boolean; withAnimation?: boolean; isMoving?: boolean; onClose: () => void; onFooterClick: () => void; handleSponsoredClick: () => void; }; type StateProps = { textMessage?: ApiMessage | ApiSponsoredMessage; origin?: MediaViewerOrigin; isProtected?: boolean; volume: number; isMuted: boolean; isHidden?: boolean; playbackRate: number; }; const ANIMATION_DURATION = 350; const MOBILE_VERSION_CONTROL_WIDTH = 350; const MediaViewerContent = ({ item, isActive, textMessage, origin, withAnimation, isProtected, volume, playbackRate, isMuted, isHidden, isMoving, onClose, onFooterClick, handleSponsoredClick, }: OwnProps & StateProps) => { const lang = useOldLang(); const isAvatar = item.type === 'avatar'; const isSponsoredMessage = item.type === 'sponsoredMessage'; const { media } = getViewableMedia(item) || {}; const { isVideo, isPhoto, bestImageData, bestData, dimensions, isGif, isLocal, isVideoAvatar, mediaSize, loadProgress, } = useMediaProps({ media, isAvatar, origin, delay: withAnimation ? ANIMATION_DURATION : false, }); const [, toggleControls] = useControlsSignal(); const isOpen = Boolean(media); const { isMobile } = useAppLayout(); const toggleControlsOnMove = useLastCallback(() => { toggleControls(true); }); if (!media) return undefined; if (item.type === 'avatar') { if (!isVideoAvatar) { return (
{renderPhoto( bestData, calculateMediaViewerDimensions(dimensions!, false), !isMobile && !isProtected, isProtected, )}
); } else { return (
); } } const textParts = textMessage && (textMessage.content.action?.type === 'suggestProfilePhoto' ? lang('Conversation.SuggestedPhotoTitle') : renderMessageText({ message: textMessage, forcePlayback: true, isForMediaViewer: true })); const buttonText = textMessage && 'buttonText' in textMessage ? textMessage.buttonText : undefined; const hasFooter = Boolean(textParts); const posterSize = calculateMediaViewerDimensions(dimensions!, hasFooter, isVideo); const isForceMobileVersion = isMobile || shouldForceMobileVersion(posterSize); return (
{isPhoto && renderPhoto( bestData, posterSize, !isMobile && !isProtected, isProtected, )} {isVideo && (!isActive ? renderVideoPreview( bestImageData, posterSize, !isMobile && !isProtected, isProtected, ) : ( ))} {textParts && ( )}
); }; export default memo(withGlobal( (global, { item }): StateProps => { const { volume, isMuted, playbackRate, isHidden, origin, } = selectTabState(global).mediaViewer; const message = item.type === 'message' ? item.message : undefined; const sponsoredMessage = item.type === 'sponsoredMessage' ? item.message : undefined; const textMessage = message || sponsoredMessage; return { origin, textMessage, isProtected: message && selectIsMessageProtected(global, message), volume, isMuted, isHidden, playbackRate, }; }, )(MediaViewerContent)); function renderPhoto(blobUrl?: string, imageSize?: ApiDimensions, canDrag?: boolean, isProtected?: boolean) { return blobUrl ? (
{isProtected &&
}
) : (
); } function renderVideoPreview(blobUrl?: string, imageSize?: ApiDimensions, canDrag?: boolean, isProtected?: boolean) { const wrapperStyle = imageSize && `width: ${imageSize.width}px; height: ${imageSize.height}px`; const videoStyle = `background-image: url(${blobUrl})`; return blobUrl ? (
{isProtected &&
}
{/* eslint-disable-next-line jsx-a11y/media-has-caption */}
) : (
); } function shouldForceMobileVersion(posterSize?: { width: number; height: number }) { if (!posterSize) return false; return posterSize.width < MOBILE_VERSION_CONTROL_WIDTH; }