Trends.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import api from 'mastodon/api';
  4. import { FormattedMessage } from 'react-intl';
  5. import classNames from 'classnames';
  6. import Hashtag from 'mastodon/components/hashtag';
  7. export default class Trends extends React.PureComponent {
  8. static propTypes = {
  9. limit: PropTypes.number.isRequired,
  10. };
  11. state = {
  12. loading: true,
  13. data: null,
  14. };
  15. componentDidMount () {
  16. const { limit } = this.props;
  17. api().get('/api/v1/admin/trends/tags', { params: { limit } }).then(res => {
  18. this.setState({
  19. loading: false,
  20. data: res.data,
  21. });
  22. }).catch(err => {
  23. console.error(err);
  24. });
  25. }
  26. render () {
  27. const { limit } = this.props;
  28. const { loading, data } = this.state;
  29. let content;
  30. if (loading) {
  31. content = (
  32. <div>
  33. {Array.from(Array(limit)).map((_, i) => (
  34. <Hashtag key={i} />
  35. ))}
  36. </div>
  37. );
  38. } else {
  39. content = (
  40. <div>
  41. {data.map(hashtag => (
  42. <Hashtag
  43. key={hashtag.name}
  44. name={hashtag.name}
  45. href={`/admin/tags/${hashtag.id}`}
  46. people={hashtag.history[0].accounts * 1 + hashtag.history[1].accounts * 1}
  47. uses={hashtag.history[0].uses * 1 + hashtag.history[1].uses * 1}
  48. history={hashtag.history.reverse().map(day => day.uses)}
  49. className={classNames(hashtag.requires_review && 'trends__item--requires-review', !hashtag.trendable && !hashtag.requires_review && 'trends__item--disabled')}
  50. />
  51. ))}
  52. </div>
  53. );
  54. }
  55. return (
  56. <div className='trends trends--compact'>
  57. <h4><FormattedMessage id='trends.trending_now' defaultMessage='Trending now' /></h4>
  58. {content}
  59. </div>
  60. );
  61. }
  62. }