import React from 'react'; import { connect } from 'react-redux'; import ImmutablePropTypes from 'react-immutable-proptypes'; import PropTypes from 'prop-types'; import { fetchAccount, fetchAccountByUsername, } from 'gabsocial/actions/accounts'; import { expandAccountMediaTimeline } from '../../actions/timelines'; import LoadingIndicator from 'gabsocial/components/loading_indicator'; import Column from '../ui/components/column'; import ImmutablePureComponent from 'react-immutable-pure-component'; import { getAccountGallery } from 'gabsocial/selectors'; import MediaItem from './components/media_item'; import LoadMore from 'gabsocial/components/load_more'; import MissingIndicator from 'gabsocial/components/missing_indicator'; import { openModal } from 'gabsocial/actions/modal'; import { NavLink } from 'react-router-dom'; import { FormattedMessage } from 'react-intl'; import { me } from 'gabsocial/initial_state'; const mapStateToProps = (state, { params: { username }, withReplies = false }) => { const accounts = state.getIn(['accounts']); const accountFetchError = (state.getIn(['accounts', -1, 'username'], '').toLowerCase() == username.toLowerCase()); let accountId = -1; let accountUsername = username; if (accountFetchError) { accountId = null; } else { let account = accounts.find(acct => username.toLowerCase() == acct.getIn(['acct'], '').toLowerCase()); accountId = account ? account.getIn(['id'], null) : -1; accountUsername = account ? account.getIn(['acct'], '') : ''; } const isBlocked = state.getIn(['relationships', accountId, 'blocked_by'], false); const isLocked = state.getIn(['accounts', accountId, 'locked'], false); const isFollowing = state.getIn(['relationships', accountId, 'following'], false); const unavailable = (me == accountId) ? false : (isBlocked || (isLocked && !isFollowing)); return { accountId, unavailable, accountUsername, isAccount: !!state.getIn(['accounts', accountId]), attachments: getAccountGallery(state, accountId), isLoading: state.getIn(['timelines', `account:${accountId}:media`, 'isLoading']), hasMore: state.getIn(['timelines', `account:${accountId}:media`, 'hasMore']), }; }; class LoadMoreMedia extends ImmutablePureComponent { static propTypes = { maxId: PropTypes.string, onLoadMore: PropTypes.func.isRequired, }; handleLoadMore = () => { this.props.onLoadMore(this.props.maxId); } render () { return ( ); } } export default @connect(mapStateToProps) class AccountGallery extends ImmutablePureComponent { static propTypes = { params: PropTypes.object.isRequired, dispatch: PropTypes.func.isRequired, attachments: ImmutablePropTypes.list.isRequired, isLoading: PropTypes.bool, hasMore: PropTypes.bool, isAccount: PropTypes.bool, unavailable: PropTypes.bool, }; state = { width: 323, }; componentDidMount () { const { params: { username }, accountId, withReplies } = this.props; if (accountId && accountId !== -1) { this.props.dispatch(fetchAccount(accountId)); this.props.dispatch(expandAccountMediaTimeline(accountId)); } else { this.props.dispatch(fetchAccountByUsername(username)); } } componentWillReceiveProps (nextProps) { if (nextProps.accountId && nextProps.accountId !== -1 && (nextProps.accountId !== this.props.accountId && nextProps.accountId)) { this.props.dispatch(fetchAccount(nextProps.params.accountId)); this.props.dispatch(expandAccountMediaTimeline(nextProps.accountId)); } } handleScrollToBottom = () => { if (this.props.hasMore) { this.handleLoadMore(this.props.attachments.size > 0 ? this.props.attachments.last().getIn(['status', 'id']) : undefined); } } handleScroll = e => { const { scrollTop, scrollHeight, clientHeight } = e.target; const offset = scrollHeight - scrollTop - clientHeight; if (150 > offset && !this.props.isLoading) { this.handleScrollToBottom(); } } handleLoadMore = maxId => { if (this.props.accountId && this.props.accountId !== -1) { this.props.dispatch(expandAccountMediaTimeline(this.props.accountId, { maxId })); } }; handleLoadOlder = e => { e.preventDefault(); this.handleScrollToBottom(); } handleOpenMedia = attachment => { if (attachment.get('type') === 'video') { this.props.dispatch(openModal('VIDEO', { media: attachment, status: attachment.get('status') })); } else { const media = attachment.getIn(['status', 'media_attachments']); const index = media.findIndex(x => x.get('id') === attachment.get('id')); this.props.dispatch(openModal('MEDIA', { media, index, status: attachment.get('status') })); } } handleRef = c => { if (c) { this.setState({ width: c.offsetWidth }); } } render () { const { attachments, isLoading, hasMore, isAccount, accountId, unavailable, accountUsername } = this.props; const { width } = this.state; if (!isAccount && accountId !== -1) { return ( ); } if (accountId == -1 || (!attachments && isLoading)) { return ( ); } let loadOlder = null; if (hasMore && !(isLoading && attachments.size === 0)) { loadOlder = ; } if (unavailable) { return (
); } return (
{attachments.map((attachment, index) => attachment === null ? ( 0 ? attachments.getIn(index - 1, 'id') : null} onLoadMore={this.handleLoadMore} /> ) : ( ))} { attachments.size == 0 &&
} {loadOlder}
{isLoading && attachments.size === 0 && (
)}
); } }