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.

193 lines
5.8 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 } 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. const messages = defineMessages({
  19. deleteMessage: { id: 'confirmations.delete_list.message', defaultMessage: 'Are you sure you want to permanently delete this list?' },
  20. deleteConfirm: { id: 'confirmations.delete_list.confirm', defaultMessage: 'Delete' },
  21. });
  22. const mapStateToProps = (state, props) => ({
  23. list: state.getIn(['lists', props.params.id]),
  24. hasUnread: state.getIn(['timelines', `list:${props.params.id}`, 'unread']) > 0,
  25. });
  26. export default @connect(mapStateToProps)
  27. @injectIntl
  28. class ListTimeline extends React.PureComponent {
  29. static contextTypes = {
  30. router: PropTypes.object,
  31. };
  32. static propTypes = {
  33. params: PropTypes.object.isRequired,
  34. dispatch: PropTypes.func.isRequired,
  35. shouldUpdateScroll: PropTypes.func,
  36. columnId: PropTypes.string,
  37. hasUnread: PropTypes.bool,
  38. multiColumn: PropTypes.bool,
  39. list: PropTypes.oneOfType([ImmutablePropTypes.map, PropTypes.bool]),
  40. intl: PropTypes.object.isRequired,
  41. };
  42. handlePin = () => {
  43. const { columnId, dispatch } = this.props;
  44. if (columnId) {
  45. dispatch(removeColumn(columnId));
  46. } else {
  47. dispatch(addColumn('LIST', { id: this.props.params.id }));
  48. this.context.router.history.push('/');
  49. }
  50. }
  51. handleMove = (dir) => {
  52. const { columnId, dispatch } = this.props;
  53. dispatch(moveColumn(columnId, dir));
  54. }
  55. handleHeaderClick = () => {
  56. this.column.scrollTop();
  57. }
  58. componentDidMount () {
  59. const { dispatch } = this.props;
  60. const { id } = this.props.params;
  61. dispatch(fetchList(id));
  62. dispatch(expandListTimeline(id));
  63. this.disconnect = dispatch(connectListStream(id));
  64. }
  65. componentWillReceiveProps (nextProps) {
  66. const { dispatch } = this.props;
  67. const { id } = nextProps.params;
  68. if (id !== this.props.params.id) {
  69. if (this.disconnect) {
  70. this.disconnect();
  71. this.disconnect = null;
  72. }
  73. dispatch(fetchList(id));
  74. dispatch(expandListTimeline(id));
  75. this.disconnect = dispatch(connectListStream(id));
  76. }
  77. }
  78. componentWillUnmount () {
  79. if (this.disconnect) {
  80. this.disconnect();
  81. this.disconnect = null;
  82. }
  83. }
  84. setRef = c => {
  85. this.column = c;
  86. }
  87. handleLoadMore = maxId => {
  88. const { id } = this.props.params;
  89. this.props.dispatch(expandListTimeline(id, { maxId }));
  90. }
  91. handleEditClick = () => {
  92. this.props.dispatch(openModal('LIST_EDITOR', { listId: this.props.params.id }));
  93. }
  94. handleDeleteClick = () => {
  95. const { dispatch, columnId, intl } = this.props;
  96. const { id } = this.props.params;
  97. dispatch(openModal('CONFIRM', {
  98. message: intl.formatMessage(messages.deleteMessage),
  99. confirm: intl.formatMessage(messages.deleteConfirm),
  100. onConfirm: () => {
  101. dispatch(deleteList(id));
  102. if (!!columnId) {
  103. dispatch(removeColumn(columnId));
  104. } else {
  105. this.context.router.history.push('/lists');
  106. }
  107. },
  108. }));
  109. }
  110. render () {
  111. const { shouldUpdateScroll, hasUnread, columnId, multiColumn, list } = this.props;
  112. const { id } = this.props.params;
  113. const pinned = !!columnId;
  114. const title = list ? list.get('title') : id;
  115. if (typeof list === 'undefined') {
  116. return (
  117. <Column>
  118. <div className='scrollable'>
  119. <LoadingIndicator />
  120. </div>
  121. </Column>
  122. );
  123. } else if (list === false) {
  124. return (
  125. <Column>
  126. <ColumnBackButton multiColumn={multiColumn} />
  127. <MissingIndicator />
  128. </Column>
  129. );
  130. }
  131. return (
  132. <Column bindToDocument={!multiColumn} ref={this.setRef} label={title}>
  133. <ColumnHeader
  134. icon='list-ul'
  135. active={hasUnread}
  136. title={title}
  137. onPin={this.handlePin}
  138. onMove={this.handleMove}
  139. onClick={this.handleHeaderClick}
  140. pinned={pinned}
  141. multiColumn={multiColumn}
  142. >
  143. <div className='column-header__links'>
  144. <button className='text-btn column-header__setting-btn' tabIndex='0' onClick={this.handleEditClick}>
  145. <Icon id='pencil' /> <FormattedMessage id='lists.edit' defaultMessage='Edit list' />
  146. </button>
  147. <button className='text-btn column-header__setting-btn' tabIndex='0' onClick={this.handleDeleteClick}>
  148. <Icon id='trash' /> <FormattedMessage id='lists.delete' defaultMessage='Delete list' />
  149. </button>
  150. </div>
  151. </ColumnHeader>
  152. <StatusListContainer
  153. trackScroll={!pinned}
  154. scrollKey={`list_timeline-${columnId}`}
  155. timelineId={`list:${id}`}
  156. onLoadMore={this.handleLoadMore}
  157. 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.' />}
  158. shouldUpdateScroll={shouldUpdateScroll}
  159. bindToDocument={!multiColumn}
  160. />
  161. </Column>
  162. );
  163. }
  164. }