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.

170 lines
5.8 KiB

  1. import React from 'react';
  2. import { connect } from 'react-redux';
  3. import { defineMessages, injectIntl } from 'react-intl';
  4. import PropTypes from 'prop-types';
  5. import ImmutablePropTypes from 'react-immutable-proptypes';
  6. import Column from 'mastodon/components/column';
  7. import ColumnHeader from 'mastodon/components/column_header';
  8. import { addColumn, removeColumn, moveColumn, changeColumnParams } from 'mastodon/actions/columns';
  9. import { fetchDirectory, expandDirectory } from 'mastodon/actions/directory';
  10. import { List as ImmutableList } from 'immutable';
  11. import AccountCard from './components/account_card';
  12. import RadioButton from 'mastodon/components/radio_button';
  13. import classNames from 'classnames';
  14. import LoadMore from 'mastodon/components/load_more';
  15. import ScrollContainer from 'mastodon/containers/scroll_container';
  16. const messages = defineMessages({
  17. title: { id: 'column.directory', defaultMessage: 'Browse profiles' },
  18. recentlyActive: { id: 'directory.recently_active', defaultMessage: 'Recently active' },
  19. newArrivals: { id: 'directory.new_arrivals', defaultMessage: 'New arrivals' },
  20. local: { id: 'directory.local', defaultMessage: 'From {domain} only' },
  21. federated: { id: 'directory.federated', defaultMessage: 'From known fediverse' },
  22. });
  23. const mapStateToProps = state => ({
  24. accountIds: state.getIn(['user_lists', 'directory', 'items'], ImmutableList()),
  25. isLoading: state.getIn(['user_lists', 'directory', 'isLoading'], true),
  26. domain: state.getIn(['meta', 'domain']),
  27. });
  28. export default @connect(mapStateToProps)
  29. @injectIntl
  30. class Directory extends React.PureComponent {
  31. static contextTypes = {
  32. router: PropTypes.object,
  33. };
  34. static propTypes = {
  35. isLoading: PropTypes.bool,
  36. accountIds: ImmutablePropTypes.list.isRequired,
  37. dispatch: PropTypes.func.isRequired,
  38. columnId: PropTypes.string,
  39. intl: PropTypes.object.isRequired,
  40. multiColumn: PropTypes.bool,
  41. domain: PropTypes.string.isRequired,
  42. params: PropTypes.shape({
  43. order: PropTypes.string,
  44. local: PropTypes.bool,
  45. }),
  46. };
  47. state = {
  48. order: null,
  49. local: null,
  50. };
  51. handlePin = () => {
  52. const { columnId, dispatch } = this.props;
  53. if (columnId) {
  54. dispatch(removeColumn(columnId));
  55. } else {
  56. dispatch(addColumn('DIRECTORY', this.getParams(this.props, this.state)));
  57. }
  58. }
  59. getParams = (props, state) => ({
  60. order: state.order === null ? (props.params.order || 'active') : state.order,
  61. local: state.local === null ? (props.params.local || false) : state.local,
  62. });
  63. handleMove = dir => {
  64. const { columnId, dispatch } = this.props;
  65. dispatch(moveColumn(columnId, dir));
  66. }
  67. handleHeaderClick = () => {
  68. this.column.scrollTop();
  69. }
  70. componentDidMount () {
  71. const { dispatch } = this.props;
  72. dispatch(fetchDirectory(this.getParams(this.props, this.state)));
  73. }
  74. componentDidUpdate (prevProps, prevState) {
  75. const { dispatch } = this.props;
  76. const paramsOld = this.getParams(prevProps, prevState);
  77. const paramsNew = this.getParams(this.props, this.state);
  78. if (paramsOld.order !== paramsNew.order || paramsOld.local !== paramsNew.local) {
  79. dispatch(fetchDirectory(paramsNew));
  80. }
  81. }
  82. setRef = c => {
  83. this.column = c;
  84. }
  85. handleChangeOrder = e => {
  86. const { dispatch, columnId } = this.props;
  87. if (columnId) {
  88. dispatch(changeColumnParams(columnId, ['order'], e.target.value));
  89. } else {
  90. this.setState({ order: e.target.value });
  91. }
  92. }
  93. handleChangeLocal = e => {
  94. const { dispatch, columnId } = this.props;
  95. if (columnId) {
  96. dispatch(changeColumnParams(columnId, ['local'], e.target.value === '1'));
  97. } else {
  98. this.setState({ local: e.target.value === '1' });
  99. }
  100. }
  101. handleLoadMore = () => {
  102. const { dispatch } = this.props;
  103. dispatch(expandDirectory(this.getParams(this.props, this.state)));
  104. }
  105. render () {
  106. const { isLoading, accountIds, intl, columnId, multiColumn, domain } = this.props;
  107. const { order, local } = this.getParams(this.props, this.state);
  108. const pinned = !!columnId;
  109. const scrollableArea = (
  110. <div className='scrollable' style={{ background: 'transparent' }}>
  111. <div className='filter-form'>
  112. <div className='filter-form__column' role='group'>
  113. <RadioButton name='order' value='active' label={intl.formatMessage(messages.recentlyActive)} checked={order === 'active'} onChange={this.handleChangeOrder} />
  114. <RadioButton name='order' value='new' label={intl.formatMessage(messages.newArrivals)} checked={order === 'new'} onChange={this.handleChangeOrder} />
  115. </div>
  116. <div className='filter-form__column' role='group'>
  117. <RadioButton name='local' value='1' label={intl.formatMessage(messages.local, { domain })} checked={local} onChange={this.handleChangeLocal} />
  118. <RadioButton name='local' value='0' label={intl.formatMessage(messages.federated)} checked={!local} onChange={this.handleChangeLocal} />
  119. </div>
  120. </div>
  121. <div className={classNames('directory__list', { loading: isLoading })}>
  122. {accountIds.map(accountId => <AccountCard id={accountId} key={accountId} />)}
  123. </div>
  124. <LoadMore onClick={this.handleLoadMore} visible={!isLoading} />
  125. </div>
  126. );
  127. return (
  128. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  129. <ColumnHeader
  130. icon='address-book-o'
  131. title={intl.formatMessage(messages.title)}
  132. onPin={this.handlePin}
  133. onMove={this.handleMove}
  134. onClick={this.handleHeaderClick}
  135. pinned={pinned}
  136. multiColumn={multiColumn}
  137. />
  138. {multiColumn && !pinned ? <ScrollContainer scrollKey='directory'>{scrollableArea}</ScrollContainer> : scrollableArea}
  139. </Column>
  140. );
  141. }
  142. }