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.

177 lines
5.4 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. componentWillUnmount () {
  66. if (this.disconnect) {
  67. this.disconnect();
  68. this.disconnect = null;
  69. }
  70. }
  71. setRef = c => {
  72. this.column = c;
  73. }
  74. handleLoadMore = maxId => {
  75. const { id } = this.props.params;
  76. this.props.dispatch(expandListTimeline(id, { maxId }));
  77. }
  78. handleEditClick = () => {
  79. this.props.dispatch(openModal('LIST_EDITOR', { listId: this.props.params.id }));
  80. }
  81. handleDeleteClick = () => {
  82. const { dispatch, columnId, intl } = this.props;
  83. const { id } = this.props.params;
  84. dispatch(openModal('CONFIRM', {
  85. message: intl.formatMessage(messages.deleteMessage),
  86. confirm: intl.formatMessage(messages.deleteConfirm),
  87. onConfirm: () => {
  88. dispatch(deleteList(id));
  89. if (!!columnId) {
  90. dispatch(removeColumn(columnId));
  91. } else {
  92. this.context.router.history.push('/lists');
  93. }
  94. },
  95. }));
  96. }
  97. render () {
  98. const { shouldUpdateScroll, hasUnread, columnId, multiColumn, list } = this.props;
  99. const { id } = this.props.params;
  100. const pinned = !!columnId;
  101. const title = list ? list.get('title') : id;
  102. if (typeof list === 'undefined') {
  103. return (
  104. <Column>
  105. <div className='scrollable'>
  106. <LoadingIndicator />
  107. </div>
  108. </Column>
  109. );
  110. } else if (list === false) {
  111. return (
  112. <Column>
  113. <ColumnBackButton />
  114. <MissingIndicator />
  115. </Column>
  116. );
  117. }
  118. return (
  119. <Column ref={this.setRef} label={title}>
  120. <ColumnHeader
  121. icon='list-ul'
  122. active={hasUnread}
  123. title={title}
  124. onPin={this.handlePin}
  125. onMove={this.handleMove}
  126. onClick={this.handleHeaderClick}
  127. pinned={pinned}
  128. multiColumn={multiColumn}
  129. >
  130. <div className='column-header__links'>
  131. <button className='text-btn column-header__setting-btn' tabIndex='0' onClick={this.handleEditClick}>
  132. <Icon id='pencil' /> <FormattedMessage id='lists.edit' defaultMessage='Edit list' />
  133. </button>
  134. <button className='text-btn column-header__setting-btn' tabIndex='0' onClick={this.handleDeleteClick}>
  135. <Icon id='trash' /> <FormattedMessage id='lists.delete' defaultMessage='Delete list' />
  136. </button>
  137. </div>
  138. <hr />
  139. </ColumnHeader>
  140. <StatusListContainer
  141. trackScroll={!pinned}
  142. scrollKey={`list_timeline-${columnId}`}
  143. timelineId={`list:${id}`}
  144. onLoadMore={this.handleLoadMore}
  145. 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.' />}
  146. shouldUpdateScroll={shouldUpdateScroll}
  147. />
  148. </Column>
  149. );
  150. }
  151. }