import React, { FC, memo, useCallback, useMemo, } from '../../lib/teact/teact'; import { getDispatch, withGlobal } from '../../lib/teact/teactn'; import { ApiMessage } from '../../api/types'; import { IS_SINGLE_COLUMN_LAYOUT } from '../../util/environment'; import { getMessageMediaHash } from '../../modules/helpers'; import useLang from '../../hooks/useLang'; import useMediaWithLoadProgress from '../../hooks/useMediaWithLoadProgress'; import { selectIsDownloading, selectIsMessageProtected } from '../../modules/selectors'; import Button from '../ui/Button'; import DropdownMenu from '../ui/DropdownMenu'; import MenuItem from '../ui/MenuItem'; import ProgressSpinner from '../ui/ProgressSpinner'; import './MediaViewerActions.scss'; type StateProps = { isDownloading: boolean; isProtected?: boolean; }; type OwnProps = { mediaData?: string; isVideo: boolean; isZoomed: boolean; message?: ApiMessage; fileName?: string; isAvatar?: boolean; onCloseMediaViewer: NoneToVoidFunction; onForward: NoneToVoidFunction; onZoomToggle: NoneToVoidFunction; }; const MediaViewerActions: FC = ({ mediaData, isVideo, isZoomed, message, fileName, isAvatar, isDownloading, isProtected, onCloseMediaViewer, onForward, onZoomToggle, }) => { const { downloadMessageMedia, cancelMessageMediaDownload, } = getDispatch(); const { loadProgress: downloadProgress } = useMediaWithLoadProgress( message && getMessageMediaHash(message, 'download'), !isDownloading, ); const handleDownloadClick = useCallback(() => { if (isDownloading) { cancelMessageMediaDownload({ message }); } else { downloadMessageMedia({ message }); } }, [cancelMessageMediaDownload, downloadMessageMedia, isDownloading, message]); const lang = useLang(); const MenuButton: FC<{ onTrigger: () => void; isOpen?: boolean }> = useMemo(() => { return ({ onTrigger, isOpen }) => ( ); }, []); function renderDownloadButton() { if (isProtected) { return undefined; } return isVideo ? ( ) : ( ); } if (IS_SINGLE_COLUMN_LAYOUT) { if (isProtected) { return undefined; } return (
{!isAvatar && ( {lang('Forward')} )} {isVideo ? ( {isDownloading ? `${Math.round(downloadProgress * 100)}% Downloading...` : 'Download'} ) : ( {lang('AccActionDownload')} )} {isDownloading && }
); } return (
{!isAvatar && !isProtected && ( )} {renderDownloadButton()}
); }; export default memo(withGlobal( (global, { message }): StateProps => { const isDownloading = message ? selectIsDownloading(global, message) : false; const isProtected = selectIsMessageProtected(global, message); return { isDownloading, isProtected, }; }, )(MediaViewerActions));