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.

98 lines
2.9 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 { fetchFavouritedStatuses, expandFavouritedStatuses } from 'flavours/glitch/actions/favourites';
  6. import Column from 'flavours/glitch/features/ui/components/column';
  7. import ColumnHeader from 'flavours/glitch/components/column_header';
  8. import { addColumn, removeColumn, moveColumn } from 'flavours/glitch/actions/columns';
  9. import StatusList from 'flavours/glitch/components/status_list';
  10. import { defineMessages, injectIntl } from 'react-intl';
  11. import ImmutablePureComponent from 'react-immutable-pure-component';
  12. import { debounce } from 'lodash';
  13. const messages = defineMessages({
  14. heading: { id: 'column.favourites', defaultMessage: 'Favourites' },
  15. });
  16. const mapStateToProps = state => ({
  17. statusIds: state.getIn(['status_lists', 'favourites', 'items']),
  18. isLoading: state.getIn(['status_lists', 'favourites', 'isLoading'], true),
  19. hasMore: !!state.getIn(['status_lists', 'favourites', 'next']),
  20. });
  21. @connect(mapStateToProps)
  22. @injectIntl
  23. export default class Favourites extends ImmutablePureComponent {
  24. static propTypes = {
  25. dispatch: PropTypes.func.isRequired,
  26. statusIds: ImmutablePropTypes.list.isRequired,
  27. intl: PropTypes.object.isRequired,
  28. columnId: PropTypes.string,
  29. multiColumn: PropTypes.bool,
  30. hasMore: PropTypes.bool,
  31. isLoading: PropTypes.bool,
  32. };
  33. componentWillMount () {
  34. this.props.dispatch(fetchFavouritedStatuses());
  35. }
  36. handlePin = () => {
  37. const { columnId, dispatch } = this.props;
  38. if (columnId) {
  39. dispatch(removeColumn(columnId));
  40. } else {
  41. dispatch(addColumn('FAVOURITES', {}));
  42. }
  43. }
  44. handleMove = (dir) => {
  45. const { columnId, dispatch } = this.props;
  46. dispatch(moveColumn(columnId, dir));
  47. }
  48. handleHeaderClick = () => {
  49. this.column.scrollTop();
  50. }
  51. setRef = c => {
  52. this.column = c;
  53. }
  54. handleLoadMore = debounce(() => {
  55. this.props.dispatch(expandFavouritedStatuses());
  56. }, 300, { leading: true })
  57. render () {
  58. const { intl, statusIds, columnId, multiColumn, hasMore, isLoading } = this.props;
  59. const pinned = !!columnId;
  60. return (
  61. <Column ref={this.setRef} name='favourites' label={intl.formatMessage(messages.heading)}>
  62. <ColumnHeader
  63. icon='star'
  64. title={intl.formatMessage(messages.heading)}
  65. onPin={this.handlePin}
  66. onMove={this.handleMove}
  67. onClick={this.handleHeaderClick}
  68. pinned={pinned}
  69. multiColumn={multiColumn}
  70. showBackButton
  71. />
  72. <StatusList
  73. trackScroll={!pinned}
  74. statusIds={statusIds}
  75. scrollKey={`favourited_statuses-${columnId}`}
  76. hasMore={hasMore}
  77. isLoading={isLoading}
  78. onLoadMore={this.handleLoadMore}
  79. />
  80. </Column>
  81. );
  82. }
  83. }