93 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-02-28 10:20:47 -05:00
import { defineMessages, injectIntl } from 'react-intl'
import ImmutablePureComponent from 'react-immutable-pure-component'
import ImmutablePropTypes from 'react-immutable-proptypes'
2020-03-06 23:53:28 -05:00
import { expandAccountMediaTimeline } from '../../actions/timelines'
import { getAccountGallery } from '../../selectors'
2020-02-28 10:20:47 -05:00
import PanelLayout from './panel_layout'
2020-03-06 23:53:28 -05:00
import MediaItem from '../media_item'
import MediaGalleryPanelPlaceholder from '../placeholder/media_gallery_panel_placeholder'
2020-02-24 16:56:07 -05:00
const messages = defineMessages({
2020-02-28 10:20:47 -05:00
title: { id: 'media_gallery_panel.title', defaultMessage: 'Media' },
show_all: { id: 'media_gallery_panel.all', defaultMessage: 'Show all' },
})
2020-02-24 16:56:07 -05:00
2020-03-06 23:53:28 -05:00
const mapStateToProps = (state, { account }) => {
const accountId = !!account ? account.get('id') : -1
2020-02-24 16:56:07 -05:00
return {
2020-03-06 23:53:28 -05:00
accountId,
isLoading: state.getIn(['timelines', `account:${accountId}:media`, 'isLoading'], true),
2020-03-06 23:53:28 -05:00
attachments: getAccountGallery(state, accountId),
2020-02-24 16:56:07 -05:00
}
2020-02-28 10:20:47 -05:00
}
2020-02-24 16:56:07 -05:00
2020-02-25 11:04:44 -05:00
export default
2020-03-06 23:53:28 -05:00
@connect(mapStateToProps)
2020-02-24 16:56:07 -05:00
@injectIntl
2020-02-28 10:20:47 -05:00
class MediaGalleryPanel extends ImmutablePureComponent {
2020-02-24 16:56:07 -05:00
static propTypes = {
2020-03-06 23:53:28 -05:00
dispatch: PropTypes.func.isRequired,
accountId: PropTypes.string,
2020-03-24 00:39:12 -04:00
account: ImmutablePropTypes.map,
isLoading: PropTypes.bool,
2020-03-06 23:53:28 -05:00
attachments: ImmutablePropTypes.list.isRequired,
2020-02-24 16:56:07 -05:00
intl: PropTypes.object.isRequired,
2020-02-28 10:20:47 -05:00
}
2020-02-24 16:56:07 -05:00
2020-02-28 10:20:47 -05:00
componentDidMount() {
2020-03-06 23:53:28 -05:00
const { accountId } = this.props
2020-04-11 18:29:19 -04:00
if (accountId && accountId !== -1) {
2020-04-06 21:53:23 -04:00
this.props.dispatch(expandAccountMediaTimeline(accountId, { limit: 8 }))
2020-03-06 23:53:28 -05:00
}
}
componentWillReceiveProps(nextProps) {
if (nextProps.accountId && nextProps.accountId !== this.props.accountId) {
2020-04-06 21:53:23 -04:00
this.props.dispatch(expandAccountMediaTimeline(nextProps.accountId, { limit: 8 }))
2020-03-06 23:53:28 -05:00
}
2020-02-24 16:56:07 -05:00
}
render() {
2020-03-06 23:53:28 -05:00
const {
account,
attachments,
intl,
isLoading,
2020-03-06 23:53:28 -05:00
} = this.props
if (!attachments) return null
2020-02-24 16:56:07 -05:00
return (
<PanelLayout
2020-03-06 23:53:28 -05:00
noPadding
2020-02-24 16:56:07 -05:00
title={intl.formatMessage(messages.title)}
headerButtonTitle={!!account ? intl.formatMessage(messages.show_all) : undefined}
headerButtonTo={!!account ? `/${account.get('acct')}/media` : undefined}
2020-04-29 18:32:49 -04:00
>
<div className={[_s.default, _s.flexRow, _s.flexWrap, _s.px10, _s.py10].join(' ')}>
{
!!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) &&
<div className={[_s.default, _s.width100PC].join(' ')}>
<MediaGalleryPanelPlaceholder />
</div>
}
</div>
2020-02-24 16:56:07 -05:00
</PanelLayout>
2020-02-28 10:20:47 -05:00
)
}
2020-05-08 22:17:19 -04:00
}