import { List as ImmutableList } from 'immutable'; import PropTypes from 'prop-types'; import React from 'react'; import ImmutablePropTypes from 'react-immutable-proptypes'; import { injectIntl, FormattedMessage, defineMessages } from 'react-intl'; import { connect } from 'react-redux'; import { fetchFavourites, fetchReactions } from 'soapbox/actions/interactions'; import FilterBar from 'soapbox/components/filter_bar'; import IconButton from 'soapbox/components/icon_button'; import ScrollableList from 'soapbox/components/scrollable_list'; import { Spinner } from 'soapbox/components/ui'; import AccountContainer from 'soapbox/containers/account_container'; const messages = defineMessages({ close: { id: 'lightbox.close', defaultMessage: 'Close' }, all: { id: 'reactions.all', defaultMessage: 'All' }, }); const mapStateToProps = (state, props) => { const favourites = state.getIn(['user_lists', 'favourited_by', props.statusId]); const reactions = state.getIn(['user_lists', 'reactions', props.statusId]); const allReactions = favourites && reactions && ImmutableList(favourites ? [{ accounts: favourites, count: favourites.size, name: '👍' }] : []).concat(reactions || []); return { reactions: allReactions, }; }; export default @connect(mapStateToProps) @injectIntl class ReactionsModal extends React.PureComponent { static propTypes = { onClose: PropTypes.func.isRequired, intl: PropTypes.object.isRequired, statusId: PropTypes.string.isRequired, username: PropTypes.string.isRequired, reaction: PropTypes.string, dispatch: PropTypes.func.isRequired, reactions: ImmutablePropTypes.list, }; state = { reaction: this.props.reaction, } fetchData = () => { const { dispatch, statusId } = this.props; dispatch(fetchFavourites(statusId)); dispatch(fetchReactions(statusId)); } componentDidMount() { this.fetchData(); } onClickClose = () => { this.props.onClose('REACTIONS'); }; handleFilterChange = (reaction) => () => { this.setState({ reaction }); }; renderFilterBar() { const { intl, reactions } = this.props; const { reaction } = this.state; const items = [ { text: intl.formatMessage(messages.all), action: this.handleFilterChange(''), name: 'all', }, ]; reactions.forEach(reaction => items.push( { text: `${reaction.name} ${reaction.count}`, action: this.handleFilterChange(reaction.name), name: reaction.name, }, )); return ; } render() { const { intl, reactions } = this.props; const { reaction } = this.state; const accounts = reactions && (reaction ? reactions.find(reaction => reaction.name === this.state.reaction)?.accounts.map(account => ({ id: account, reaction: this.state.reaction })) : reactions.map(reaction => reaction?.accounts.map(account => ({ id: account, reaction: reaction.name }))).flatten()); let body; if (!accounts) { body = ; } else { const emptyMessage = ; body = (<> {reactions.size > 0 && this.renderFilterBar()} {accounts.map((account) => , )} ); } return (

{body}
); } }