import PropTypes from 'prop-types'; import { defineMessages, FormattedMessage, injectIntl } from 'react-intl'; import { createSelector } from '@reduxjs/toolkit'; import { is, List as ImmutableList, Set as ImmutableSet } from 'immutable'; import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePureComponent from 'react-immutable-pure-component'; import { connect } from 'react-redux'; import { ReactComponent as CloseIcon } from '@material-symbols/svg-600/outlined/close.svg'; import { followAccount } from 'mastodon/actions/accounts'; import { Button } from 'mastodon/components/button'; import { IconButton } from 'mastodon/components/icon_button'; import Option from 'mastodon/features/report/components/option'; import { languages as preloadedLanguages } from 'mastodon/initial_state'; const messages = defineMessages({ close: { id: 'lightbox.close', defaultMessage: 'Close' }, }); const getAccountLanguages = createSelector([ (state, accountId) => state.getIn(['timelines', `account:${accountId}`, 'items'], ImmutableList()), state => state.get('statuses'), ], (statusIds, statuses) => new ImmutableSet(statusIds.map(statusId => statuses.get(statusId)).filter(status => !status.get('reblog')).map(status => status.get('language')))); const mapStateToProps = (state, { accountId }) => ({ acct: state.getIn(['accounts', accountId, 'acct']), availableLanguages: getAccountLanguages(state, accountId), selectedLanguages: ImmutableSet(state.getIn(['relationships', accountId, 'languages']) || ImmutableList()), }); const mapDispatchToProps = (dispatch, { accountId }) => ({ onSubmit (languages) { dispatch(followAccount(accountId, { languages })); }, }); class SubscribedLanguagesModal extends ImmutablePureComponent { static propTypes = { accountId: PropTypes.string.isRequired, acct: PropTypes.string.isRequired, availableLanguages: ImmutablePropTypes.setOf(PropTypes.string), selectedLanguages: ImmutablePropTypes.setOf(PropTypes.string), onClose: PropTypes.func.isRequired, languages: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)), intl: PropTypes.object.isRequired, submit: PropTypes.func.isRequired, }; static defaultProps = { languages: preloadedLanguages, }; state = { selectedLanguages: this.props.selectedLanguages, }; handleLanguageToggle = (value, checked) => { const { selectedLanguages } = this.state; if (checked) { this.setState({ selectedLanguages: selectedLanguages.add(value) }); } else { this.setState({ selectedLanguages: selectedLanguages.delete(value) }); } }; handleSubmit = () => { this.props.onSubmit(this.state.selectedLanguages.toArray()); this.props.onClose(); }; renderItem (value) { const language = this.props.languages.find(language => language[0] === value); const checked = this.state.selectedLanguages.includes(value); if (!language) { return null; } return (