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.

171 lines
5.9 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 'react-router-scroll-4';
  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. shouldUpdateScroll: PropTypes.func,
  39. columnId: PropTypes.string,
  40. intl: PropTypes.object.isRequired,
  41. multiColumn: PropTypes.bool,
  42. domain: PropTypes.string.isRequired,
  43. params: PropTypes.shape({
  44. order: PropTypes.string,
  45. local: PropTypes.bool,
  46. }),
  47. };
  48. state = {
  49. order: null,
  50. local: null,
  51. };
  52. handlePin = () => {
  53. const { columnId, dispatch } = this.props;
  54. if (columnId) {
  55. dispatch(removeColumn(columnId));
  56. } else {
  57. dispatch(addColumn('DIRECTORY', this.getParams(this.props, this.state)));
  58. }
  59. }
  60. getParams = (props, state) => ({
  61. order: state.order === null ? (props.params.order || 'active') : state.order,
  62. local: state.local === null ? (props.params.local || false) : state.local,
  63. });
  64. handleMove = dir => {
  65. const { columnId, dispatch } = this.props;
  66. dispatch(moveColumn(columnId, dir));
  67. }
  68. handleHeaderClick = () => {
  69. this.column.scrollTop();
  70. }
  71. componentDidMount () {
  72. const { dispatch } = this.props;
  73. dispatch(fetchDirectory(this.getParams(this.props, this.state)));
  74. }
  75. componentDidUpdate (prevProps, prevState) {
  76. const { dispatch } = this.props;
  77. const paramsOld = this.getParams(prevProps, prevState);
  78. const paramsNew = this.getParams(this.props, this.state);
  79. if (paramsOld.order !== paramsNew.order || paramsOld.local !== paramsNew.local) {
  80. dispatch(fetchDirectory(paramsNew));
  81. }
  82. }
  83. setRef = c => {
  84. this.column = c;
  85. }
  86. handleChangeOrder = e => {
  87. const { dispatch, columnId } = this.props;
  88. if (columnId) {
  89. dispatch(changeColumnParams(columnId, ['order'], e.target.value));
  90. } else {
  91. this.setState({ order: e.target.value });
  92. }
  93. }
  94. handleChangeLocal = e => {
  95. const { dispatch, columnId } = this.props;
  96. if (columnId) {
  97. dispatch(changeColumnParams(columnId, ['local'], e.target.value === '1'));
  98. } else {
  99. this.setState({ local: e.target.value === '1' });
  100. }
  101. }
  102. handleLoadMore = () => {
  103. const { dispatch } = this.props;
  104. dispatch(expandDirectory(this.getParams(this.props, this.state)));
  105. }
  106. render () {
  107. const { isLoading, accountIds, intl, columnId, multiColumn, domain, shouldUpdateScroll } = this.props;
  108. const { order, local } = this.getParams(this.props, this.state);
  109. const pinned = !!columnId;
  110. const scrollableArea = (
  111. <div className='scrollable' style={{ background: 'transparent' }}>
  112. <div className='filter-form'>
  113. <div className='filter-form__column' role='group'>
  114. <RadioButton name='order' value='active' label={intl.formatMessage(messages.recentlyActive)} checked={order === 'active'} onChange={this.handleChangeOrder} />
  115. <RadioButton name='order' value='new' label={intl.formatMessage(messages.newArrivals)} checked={order === 'new'} onChange={this.handleChangeOrder} />
  116. </div>
  117. <div className='filter-form__column' role='group'>
  118. <RadioButton name='local' value='1' label={intl.formatMessage(messages.local, { domain })} checked={local} onChange={this.handleChangeLocal} />
  119. <RadioButton name='local' value='0' label={intl.formatMessage(messages.federated)} checked={!local} onChange={this.handleChangeLocal} />
  120. </div>
  121. </div>
  122. <div className={classNames('directory__list', { loading: isLoading })}>
  123. {accountIds.map(accountId => <AccountCard id={accountId} key={accountId} />)}
  124. </div>
  125. <LoadMore onClick={this.handleLoadMore} visible={!isLoading} />
  126. </div>
  127. );
  128. return (
  129. <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
  130. <ColumnHeader
  131. icon='address-book-o'
  132. title={intl.formatMessage(messages.title)}
  133. onPin={this.handlePin}
  134. onMove={this.handleMove}
  135. onClick={this.handleHeaderClick}
  136. pinned={pinned}
  137. multiColumn={multiColumn}
  138. />
  139. {multiColumn && !pinned ? <ScrollContainer scrollKey='directory' shouldUpdateScroll={shouldUpdateScroll}>{scrollableArea}</ScrollContainer> : scrollableArea}
  140. </Column>
  141. );
  142. }
  143. }