import { List as ImmutableList } from 'immutable'; import React, { useEffect, useState } from 'react'; import { FormattedMessage, defineMessages, useIntl } from 'react-intl'; import { fetchFavourites, fetchReactions } from 'soapbox/actions/interactions'; import FilterBar from 'soapbox/components/filter_bar'; import ScrollableList from 'soapbox/components/scrollable_list'; import { Modal, Spinner } from 'soapbox/components/ui'; import AccountContainer from 'soapbox/containers/account_container'; import { useAppDispatch, useAppSelector } from 'soapbox/hooks'; const messages = defineMessages({ close: { id: 'lightbox.close', defaultMessage: 'Close' }, all: { id: 'reactions.all', defaultMessage: 'All' }, }); interface IReactionsModal { onClose: (string: string) => void, statusId: string, reaction?: string, } const ReactionsModal: React.FC = ({ onClose, statusId, reaction: initialReaction }) => { const dispatch = useAppDispatch(); const intl = useIntl(); const [reaction, setReaction] = useState(initialReaction); const reactions = useAppSelector, count: number, name: string, }>>((state) => { const favourites = state.user_lists.getIn(['favourited_by', statusId]); const reactions = state.user_lists.getIn(['reactions', statusId]); return favourites && reactions && ImmutableList(favourites ? [{ accounts: favourites, count: favourites.size, name: '👍' }] : []).concat(reactions || []); }); const fetchData = () => { dispatch(fetchFavourites(statusId)); dispatch(fetchReactions(statusId)); }; const onClickClose = () => { onClose('REACTIONS'); }; const renderFilterBar = () => { const items = [ { text: intl.formatMessage(messages.all), action: () => setReaction(''), name: 'all', }, ]; reactions.forEach(reaction => items.push( { text: `${reaction.name} ${reaction.count}`, action: () => setReaction(reaction.name), name: reaction.name, }, )); return ; }; useEffect(() => { fetchData(); }, []); const accounts = reactions && (reaction ? reactions.find(({ name }) => name === reaction)?.accounts.map(account => ({ id: account, reaction: reaction })) : reactions.map(({ accounts, name }) => accounts.map(account => ({ id: account, reaction: name }))).flat()); let body; if (!accounts) { body = ; } else { const emptyMessage = ; body = (<> {reactions.length > 0 && renderFilterBar()} {accounts.map((account) => , )} ); } return ( } onClose={onClickClose} > {body} ); }; export default ReactionsModal;