You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
7.0 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import PropTypes from 'prop-types';
  4. import ImmutablePropTypes from 'react-immutable-proptypes';
  5. import StatusListContainer from '../ui/containers/status_list_container';
  6. import Column from '../../components/column';
  7. import ColumnBackButton from '../../components/column_back_button';
  8. import ColumnHeader from '../../components/column_header';
  9. import { addColumn, removeColumn, moveColumn } from '../../actions/columns';
  10. import { FormattedMessage, defineMessages, injectIntl } from 'react-intl';
  11. import { connectListStream } from '../../actions/streaming';
  12. import { expandListTimeline } from '../../actions/timelines';
  13. import { fetchList, deleteList, updateList } from '../../actions/lists';
  14. import { openModal } from '../../actions/modal';
  15. import MissingIndicator from '../../components/missing_indicator';
  16. import LoadingIndicator from '../../components/loading_indicator';
  17. import Icon from 'mastodon/components/icon';
  18. import RadioButton from 'mastodon/components/radio_button';
  19. const messages = defineMessages({
  20. deleteMessage: { id: 'confirmations.delete_list.message', defaultMessage: 'Are you sure you want to permanently delete this list?' },
  21. deleteConfirm: { id: 'confirmations.delete_list.confirm', defaultMessage: 'Delete' },
  22. followed: { id: 'lists.replies_policy.followed', defaultMessage: 'Any followed user' },
  23. none: { id: 'lists.replies_policy.none', defaultMessage: 'No one' },
  24. list: { id: 'lists.replies_policy.list', defaultMessage: 'Members of the list' },
  25. });
  26. const mapStateToProps = (state, props) => ({
  27. list: state.getIn(['lists', props.params.id]),
  28. hasUnread: state.getIn(['timelines', `list:${props.params.id}`, 'unread']) > 0,
  29. });
  30. export default @connect(mapStateToProps)
  31. @injectIntl
  32. class ListTimeline extends React.PureComponent {
  33. static contextTypes = {
  34. router: PropTypes.object,
  35. };
  36. static propTypes = {
  37. params: PropTypes.object.isRequired,
  38. dispatch: PropTypes.func.isRequired,
  39. columnId: PropTypes.string,
  40. hasUnread: PropTypes.bool,
  41. multiColumn: PropTypes.bool,
  42. list: PropTypes.oneOfType([ImmutablePropTypes.map, PropTypes.bool]),
  43. intl: PropTypes.object.isRequired,
  44. };
  45. handlePin = () => {
  46. const { columnId, dispatch } = this.props;
  47. if (columnId) {
  48. dispatch(removeColumn(columnId));
  49. } else {
  50. dispatch(addColumn('LIST', { id: this.props.params.id }));
  51. this.context.router.history.push('/');
  52. }
  53. }
  54. handleMove = (dir) => {
  55. const { columnId, dispatch } = this.props;
  56. dispatch(moveColumn(columnId, dir));
  57. }
  58. handleHeaderClick = () => {
  59. this.column.scrollTop();
  60. }
  61. componentDidMount () {
  62. const { dispatch } = this.props;
  63. const { id } = this.props.params;
  64. dispatch(fetchList(id));
  65. dispatch(expandListTimeline(id));
  66. this.disconnect = dispatch(connectListStream(id));
  67. }
  68. componentWillReceiveProps (nextProps) {
  69. const { dispatch } = this.props;
  70. const { id } = nextProps.params;
  71. if (id !== this.props.params.id) {
  72. if (this.disconnect) {
  73. this.disconnect();
  74. this.disconnect = null;
  75. }
  76. dispatch(fetchList(id));
  77. dispatch(expandListTimeline(id));
  78. this.disconnect = dispatch(connectListStream(id));
  79. }
  80. }
  81. componentWillUnmount () {
  82. if (this.disconnect) {
  83. this.disconnect();
  84. this.disconnect = null;
  85. }
  86. }
  87. setRef = c => {
  88. this.column = c;
  89. }
  90. handleLoadMore = maxId => {
  91. const { id } = this.props.params;
  92. this.props.dispatch(expandListTimeline(id, { maxId }));
  93. }
  94. handleEditClick = () => {
  95. this.props.dispatch(openModal('LIST_EDITOR', { listId: this.props.params.id }));
  96. }
  97. handleDeleteClick = () => {
  98. const { dispatch, columnId, intl } = this.props;
  99. const { id } = this.props.params;
  100. dispatch(openModal('CONFIRM', {
  101. message: intl.formatMessage(messages.deleteMessage),
  102. confirm: intl.formatMessage(messages.deleteConfirm),
  103. onConfirm: () => {
  104. dispatch(deleteList(id));
  105. if (!!columnId) {
  106. dispatch(removeColumn(columnId));
  107. } else {
  108. this.context.router.history.push('/lists');
  109. }
  110. },
  111. }));
  112. }
  113. handleRepliesPolicyChange = ({ target }) => {
  114. const { dispatch } = this.props;
  115. const { id } = this.props.params;
  116. dispatch(updateList(id, undefined, false, target.value));
  117. }
  118. render () {
  119. const { hasUnread, columnId, multiColumn, list, intl } = this.props;
  120. const { id } = this.props.params;
  121. const pinned = !!columnId;
  122. const title = list ? list.get('title') : id;
  123. const replies_policy = list ? list.get('replies_policy') : undefined;
  124. if (typeof list === 'undefined') {
  125. return (
  126. <Column>
  127. <div className='scrollable'>
  128. <LoadingIndicator />
  129. </div>
  130. </Column>
  131. );
  132. } else if (list === false) {
  133. return (
  134. <Column>
  135. <ColumnBackButton multiColumn={multiColumn} />
  136. <MissingIndicator />
  137. </Column>
  138. );
  139. }
  140. return (
  141. <Column bindToDocument={!multiColumn} ref={this.setRef} label={title}>
  142. <ColumnHeader
  143. icon='list-ul'
  144. active={hasUnread}
  145. title={title}
  146. onPin={this.handlePin}
  147. onMove={this.handleMove}
  148. onClick={this.handleHeaderClick}
  149. pinned={pinned}
  150. multiColumn={multiColumn}
  151. >
  152. <div className='column-settings__row column-header__links'>
  153. <button className='text-btn column-header__setting-btn' tabIndex='0' onClick={this.handleEditClick}>
  154. <Icon id='pencil' /> <FormattedMessage id='lists.edit' defaultMessage='Edit list' />
  155. </button>
  156. <button className='text-btn column-header__setting-btn' tabIndex='0' onClick={this.handleDeleteClick}>
  157. <Icon id='trash' /> <FormattedMessage id='lists.delete' defaultMessage='Delete list' />
  158. </button>
  159. </div>
  160. { replies_policy !== undefined && (
  161. <div role='group' aria-labelledby={`list-${id}-replies-policy`}>
  162. <span id={`list-${id}-replies-policy`} className='column-settings__section'>
  163. <FormattedMessage id='lists.replies_policy.title' defaultMessage='Show replies to:' />
  164. </span>
  165. <div className='column-settings__row'>
  166. { ['none', 'list', 'followed'].map(policy => (
  167. <RadioButton name='order' key={policy} value={policy} label={intl.formatMessage(messages[policy])} checked={replies_policy === policy} onChange={this.handleRepliesPolicyChange} />
  168. ))}
  169. </div>
  170. </div>
  171. )}
  172. </ColumnHeader>
  173. <StatusListContainer
  174. trackScroll={!pinned}
  175. scrollKey={`list_timeline-${columnId}`}
  176. timelineId={`list:${id}`}
  177. onLoadMore={this.handleLoadMore}
  178. emptyMessage={<FormattedMessage id='empty_column.list' defaultMessage='There is nothing in this list yet. When members of this list post new statuses, they will appear here.' />}
  179. bindToDocument={!multiColumn}
  180. />
  181. </Column>
  182. );
  183. }
  184. }