import type { FC } from '../../../lib/teact/teact'; import React, { useCallback } from '../../../lib/teact/teact'; import type { GlobalState } from '../../../global/types'; import type { ApiMessage } from '../../../api/types'; import type { IAlbum, ISettings } from '../../../types'; import type { IAlbumLayout } from './helpers/calculateAlbumLayout'; import { AlbumRectPart } from './helpers/calculateAlbumLayout'; import { getMessageContent, getMessageHtmlId, getMessageOriginalId } from '../../../global/helpers'; import { getActions, getGlobal, withGlobal } from '../../../global'; import withSelectControl from './hocs/withSelectControl'; import type { ObserveFn } from '../../../hooks/useIntersectionObserver'; import { selectActiveDownloadIds, selectCanAutoLoadMedia, selectCanAutoPlayMedia, selectTheme, } from '../../../global/selectors'; import Photo from './Photo'; import Video from './Video'; import './Album.scss'; const PhotoWithSelect = withSelectControl(Photo); const VideoWithSelect = withSelectControl(Video); type OwnProps = { album: IAlbum; observeIntersection: ObserveFn; hasCustomAppendix?: boolean; lastSyncTime?: number; isOwn: boolean; isProtected?: boolean; albumLayout: IAlbumLayout; onMediaClick: (messageId: number) => void; }; type StateProps = { theme: ISettings['theme']; uploadsById: GlobalState['fileUploads']['byMessageLocalId']; activeDownloadIds: number[]; }; const Album: FC = ({ album, observeIntersection, hasCustomAppendix, lastSyncTime, isOwn, isProtected, albumLayout, onMediaClick, uploadsById, activeDownloadIds, theme, }) => { const { cancelSendingMessage } = getActions(); const mediaCount = album.messages.length; const handleCancelUpload = useCallback((message: ApiMessage) => { cancelSendingMessage({ chatId: message.chatId, messageId: message.id }); }, [cancelSendingMessage]); function renderAlbumMessage(message: ApiMessage, index: number) { const { photo, video } = getMessageContent(message); const fileUpload = uploadsById[getMessageOriginalId(message)]; const uploadProgress = fileUpload?.progress; const { dimensions, sides } = albumLayout.layout[index]; // Ignoring global updates is a known drawback here const canAutoLoad = selectCanAutoLoadMedia(getGlobal(), message); const canAutoPlay = selectCanAutoPlayMedia(getGlobal(), message); if (photo) { const shouldAffectAppendix = hasCustomAppendix && ( // eslint-disable-next-line no-bitwise (isOwn ? index === mediaCount - 1 : Boolean(sides & AlbumRectPart.Left && sides & AlbumRectPart.Bottom)) ); return ( ); } else if (video) { return ( ); } return undefined; } const { width: containerWidth, height: containerHeight } = albumLayout.containerStyle; return (
{album.messages.map(renderAlbumMessage)}
); }; export default withGlobal( (global, { album }): StateProps => { const { chatId } = album.mainMessage; const theme = selectTheme(global); const activeDownloadIds = selectActiveDownloadIds(global, chatId); return { theme, uploadsById: global.fileUploads.byMessageLocalId, activeDownloadIds, }; }, )(Album);