import React from 'react' import PropTypes from 'prop-types' import { defineMessages, injectIntl } from 'react-intl' import ReactSwipeableViews from 'react-swipeable-views' import ImmutablePropTypes from 'react-immutable-proptypes' import ImmutablePureComponent from 'react-immutable-pure-component' import { CX } from '../../constants' import Video from '../video' import ExtendedVideoPlayer from '../extended_video_player' import Button from '../button' import ImageLoader from '../image_loader' import Pagination from '../pagination' export const previewState = 'previewMediaModal' class MediaModal extends ImmutablePureComponent { static contextTypes = { router: PropTypes.object, } state = { index: null, navigationHidden: false, } handleSwipe = (index) => { if (!this.props.media) return this.setState({ index: index % this.props.media.size }) } handleNextClick = () => { if (!this.props.media) return this.setState({ index: (this.getIndex() + 1) % this.props.media.size }) } handlePrevClick = () => { if (!this.props.media) return this.setState({ index: (this.props.media.size + this.getIndex() - 1) % this.props.media.size }) } handleChangeIndex = (i) => { if (!this.props.media) return this.setState({ index: i % this.props.media.size }) } handleKeyDown = (e) => { if (!this.props.media) return switch (e.key) { case 'ArrowLeft': this.handlePrevClick() e.preventDefault() e.stopPropagation() break case 'ArrowRight': this.handleNextClick() e.preventDefault() e.stopPropagation() break } } componentDidMount() { window.addEventListener('keydown', this.handleKeyDown, false) if (this.context.router) { const history = this.context.router.history history.push(history.location.pathname, previewState) this.unlistenHistory = history.listen(() => { this.props.onClose() }) } } componentWillUnmount() { window.removeEventListener('keydown', this.handleKeyDown) if (this.context.router) { this.unlistenHistory() if (this.context.router.history.location.state === previewState) { this.context.router.history.goBack() } } } getIndex() { return this.state.index !== null ? this.state.index : this.props.index } toggleNavigation = () => { this.setState(prevState => ({ navigationHidden: !prevState.navigationHidden, })) } handleStatusClick = e => { if (e.button === 0 && !(e.ctrlKey || e.metaKey)) { e.preventDefault() this.context.router.history.push(`/${this.props.status.getIn(['account', 'acct'])}/posts/${this.props.status.get('id')}`) } } render() { const { media, src, alt, status, intl, onClose, } = this.props const { navigationHidden } = this.state const index = this.getIndex() const content = !media ? : media.map((image) => { const width = image.getIn(['meta', 'original', 'width']) || null const height = image.getIn(['meta', 'original', 'height']) || null if (image.get('type') === 'image') { return ( ) } else if (image.get('type') === 'video') { const { time } = this.props return (