~cytrogen/masto-fe

ref: f8dc013b0d81d5d463c086e291ce99ab1d796a9d masto-fe/app/javascript/mastodon/components/admin/Trends.jsx -rw-r--r-- 1.9 KiB
f8dc013b — Cytrogen [feature] Add language selector to local settings 9 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import PropTypes from "prop-types";
import { PureComponent } from "react";

import { FormattedMessage } from "react-intl";

import classNames from "classnames";

import api from "mastodon/api";
import Hashtag from "mastodon/components/hashtag";

export default class Trends extends PureComponent {

  static propTypes = {
    limit: PropTypes.number.isRequired,
  };

  state = {
    loading: true,
    data: null,
  };

  componentDidMount () {
    const { limit } = this.props;

    api().get("/api/v1/admin/trends/tags", { params: { limit } }).then(res => {
      this.setState({
        loading: false,
        data: res.data,
      });
    }).catch(err => {
      console.error(err);
    });
  }

  render () {
    const { limit } = this.props;
    const { loading, data } = this.state;

    let content;

    if (loading) {
      content = (
        <div>
          {Array.from(Array(limit)).map((_, i) => (
            <Hashtag key={i} />
          ))}
        </div>
      );
    } else {
      content = (
        <div>
          {data.map(hashtag => (
            <Hashtag
              key={hashtag.name}
              name={hashtag.name}
              to={hashtag.id === undefined ? undefined : `/admin/tags/${hashtag.id}`}
              people={hashtag.history[0].accounts * 1 + hashtag.history[1].accounts * 1}
              uses={hashtag.history[0].uses * 1 + hashtag.history[1].uses * 1}
              history={hashtag.history.reverse().map(day => day.uses)}
              className={classNames(hashtag.requires_review && "trends__item--requires-review", !hashtag.trendable && !hashtag.requires_review && "trends__item--disabled")}
            />
          ))}
        </div>
      );
    }

    return (
      <div className='trends trends--compact'>
        <h4><FormattedMessage id='trends.trending_now' defaultMessage='Trending now' /></h4>

        {content}
      </div>
    );
  }

}