import React, { FC, useCallback } from '../../../lib/teact/teact'; import { GlobalActions, GlobalState } from '../../../global/types'; import { ApiMessage } from '../../../api/types'; import { IAlbum } from '../../../types'; import { AlbumRectPart, IAlbumLayout } from './helpers/calculateAlbumLayout'; import { getMessageContent } from '../../../modules/helpers'; import { withGlobal } from '../../../lib/teact/teactn'; import { pick } from '../../../util/iteratees'; import withSelectControl from './hocs/withSelectControl'; import { ObserveFn } from '../../../hooks/useIntersectionObserver'; 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; shouldAutoLoad?: boolean; shouldAutoPlay?: boolean; hasCustomAppendix?: boolean; lastSyncTime?: number; isOwn: boolean; albumLayout: IAlbumLayout; onMediaClick: (messageId: number) => void; }; type StateProps = { uploadsById: GlobalState['fileUploads']['byMessageLocalId']; }; type DispatchProps = Pick; const Album: FC = ({ album, observeIntersection, shouldAutoLoad, shouldAutoPlay, hasCustomAppendix, lastSyncTime, isOwn, albumLayout, onMediaClick, uploadsById, cancelSendingMessage, }) => { 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[message.previousLocalId || message.id]; const uploadProgress = fileUpload ? fileUpload.progress : undefined; const { dimensions, sides } = albumLayout.layout[index]; 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): StateProps => { return { uploadsById: global.fileUploads.byMessageLocalId, }; }, (setGlobal, actions): DispatchProps => pick(actions, [ 'cancelSendingMessage', ]), )(Album);