2020-08-17 21:07:16 +01:00
|
|
|
import React from 'react'
|
2020-08-17 21:59:29 +01:00
|
|
|
import PropTypes from 'prop-types'
|
2020-08-17 21:39:25 +01:00
|
|
|
import { connect } from 'react-redux'
|
2020-02-28 15:20:47 +00:00
|
|
|
import { defineMessages, injectIntl } from 'react-intl'
|
|
|
|
import ImmutablePureComponent from 'react-immutable-pure-component'
|
|
|
|
import ImmutablePropTypes from 'react-immutable-proptypes'
|
2020-03-07 04:53:28 +00:00
|
|
|
import { expandAccountMediaTimeline } from '../../actions/timelines'
|
|
|
|
import { getAccountGallery } from '../../selectors'
|
2020-02-28 15:20:47 +00:00
|
|
|
import PanelLayout from './panel_layout'
|
2020-03-07 04:53:28 +00:00
|
|
|
import MediaItem from '../media_item'
|
2020-07-28 21:11:51 +01:00
|
|
|
import MediaGalleryPanelPlaceholder from '../placeholder/media_gallery_panel_placeholder'
|
2020-02-24 21:56:07 +00:00
|
|
|
|
2020-02-28 15:20:47 +00:00
|
|
|
class MediaGalleryPanel extends ImmutablePureComponent {
|
2020-02-24 21:56:07 +00:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
state = {
|
|
|
|
fetched: false,
|
|
|
|
}
|
2020-03-07 04:53:28 +00:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
static getDerivedStateFromProps(nextProps, prevState) {
|
|
|
|
if (nextProps.shouldLoad && !prevState.fetched) {
|
|
|
|
return { fetched: true }
|
2020-03-07 04:53:28 +00:00
|
|
|
}
|
2020-11-15 18:48:32 +00:00
|
|
|
|
|
|
|
return null
|
2020-03-07 04:53:28 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (!prevState.fetched && this.state.fetched && this.props.isLazy) {
|
|
|
|
this.props.dispatch(expandAccountMediaTimeline(this.props.accountId, { limit: 8 }))
|
2020-03-07 04:53:28 +00:00
|
|
|
}
|
2020-02-24 21:56:07 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
componentDidMount() {
|
|
|
|
const { accountId, isLazy } = this.props
|
|
|
|
|
|
|
|
if (!isLazy && !!accountId && accountId !== -1) {
|
|
|
|
this.props.dispatch(expandAccountMediaTimeline(accountId, { limit: 8 }))
|
|
|
|
this.setState({ fetched: true })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-24 21:56:07 +00:00
|
|
|
render() {
|
2020-03-07 04:53:28 +00:00
|
|
|
const {
|
|
|
|
account,
|
2020-07-28 21:11:51 +01:00
|
|
|
attachments,
|
|
|
|
intl,
|
|
|
|
isLoading,
|
2020-03-07 04:53:28 +00:00
|
|
|
} = this.props
|
2020-11-15 18:48:32 +00:00
|
|
|
const { fetched } = this.state
|
2020-03-07 04:53:28 +00:00
|
|
|
|
2020-11-15 18:48:32 +00:00
|
|
|
if (!attachments && fetched) return null
|
2020-02-24 21:56:07 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<PanelLayout
|
2020-03-07 04:53:28 +00:00
|
|
|
noPadding
|
2020-02-24 21:56:07 +00:00
|
|
|
title={intl.formatMessage(messages.title)}
|
2020-07-28 21:11:51 +01:00
|
|
|
headerButtonTitle={!!account ? intl.formatMessage(messages.show_all) : undefined}
|
2020-12-16 07:39:07 +00:00
|
|
|
headerButtonTo={!!account ? `/${account.get('acct')}/albums` : undefined}
|
2020-04-29 23:32:49 +01:00
|
|
|
>
|
2020-12-22 06:36:38 +00:00
|
|
|
<div className={[_s.d, _s.flexRow, _s.flexWrap, _s.aiCenter, _s.jcCenter].join(' ')}>
|
2020-07-28 21:11:51 +01:00
|
|
|
{
|
|
|
|
!!account && attachments.size > 0 &&
|
|
|
|
attachments.slice(0, 16).map((attachment, i) => (
|
|
|
|
<MediaItem
|
|
|
|
isSmall
|
|
|
|
key={attachment.get('id')}
|
|
|
|
attachment={attachment}
|
|
|
|
account={account}
|
|
|
|
/>
|
|
|
|
))
|
|
|
|
}
|
|
|
|
{
|
|
|
|
!account || (attachments.size === 0 && isLoading) &&
|
2020-08-18 21:49:11 +01:00
|
|
|
<div className={[_s.d, _s.w100PC].join(' ')}>
|
2020-07-28 21:11:51 +01:00
|
|
|
<MediaGalleryPanelPlaceholder />
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
</div>
|
2020-02-24 21:56:07 +00:00
|
|
|
</PanelLayout>
|
2020-02-28 15:20:47 +00:00
|
|
|
)
|
|
|
|
}
|
2020-05-09 03:17:19 +01:00
|
|
|
}
|
2020-08-19 01:22:15 +01:00
|
|
|
|
|
|
|
const messages = defineMessages({
|
|
|
|
title: { id: 'media_gallery_panel.title', defaultMessage: 'Media' },
|
|
|
|
show_all: { id: 'media_gallery_panel.all', defaultMessage: 'Show all' },
|
|
|
|
})
|
|
|
|
|
|
|
|
const mapStateToProps = (state, { account }) => {
|
|
|
|
const accountId = !!account ? account.get('id') : -1
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountId,
|
|
|
|
isLoading: state.getIn(['timelines', `account:${accountId}:media`, 'isLoading'], true),
|
|
|
|
attachments: getAccountGallery(state, accountId),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaGalleryPanel.propTypes = {
|
|
|
|
dispatch: PropTypes.func.isRequired,
|
|
|
|
accountId: PropTypes.string,
|
|
|
|
account: ImmutablePropTypes.map,
|
|
|
|
isLoading: PropTypes.bool,
|
|
|
|
attachments: ImmutablePropTypes.list.isRequired,
|
|
|
|
intl: PropTypes.object.isRequired,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default injectIntl(connect(mapStateToProps)(MediaGalleryPanel))
|