Dimension.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import api from 'mastodon/api';
  4. import { FormattedNumber } from 'react-intl';
  5. import { roundTo10 } from 'mastodon/utils/numbers';
  6. import Skeleton from 'mastodon/components/skeleton';
  7. export default class Dimension extends React.PureComponent {
  8. static propTypes = {
  9. dimension: PropTypes.string.isRequired,
  10. start_at: PropTypes.string.isRequired,
  11. end_at: PropTypes.string.isRequired,
  12. limit: PropTypes.number.isRequired,
  13. label: PropTypes.string.isRequired,
  14. params: PropTypes.object,
  15. };
  16. state = {
  17. loading: true,
  18. data: null,
  19. };
  20. componentDidMount () {
  21. const { start_at, end_at, dimension, limit, params } = this.props;
  22. api().post('/api/v1/admin/dimensions', { keys: [dimension], start_at, end_at, limit, [dimension]: params }).then(res => {
  23. this.setState({
  24. loading: false,
  25. data: res.data,
  26. });
  27. }).catch(err => {
  28. console.error(err);
  29. });
  30. }
  31. render () {
  32. const { label, limit } = this.props;
  33. const { loading, data } = this.state;
  34. let content;
  35. if (loading) {
  36. content = (
  37. <table>
  38. <tbody>
  39. {Array.from(Array(limit)).map((_, i) => (
  40. <tr className='dimension__item' key={i}>
  41. <td className='dimension__item__key'>
  42. <Skeleton width={100} />
  43. </td>
  44. <td className='dimension__item__value'>
  45. <Skeleton width={60} />
  46. </td>
  47. </tr>
  48. ))}
  49. </tbody>
  50. </table>
  51. );
  52. } else {
  53. const sum = data[0].data.reduce((sum, cur) => sum + (cur.value * 1), 0);
  54. content = (
  55. <table>
  56. <tbody>
  57. {data[0].data.map(item => (
  58. <tr className='dimension__item' key={item.key}>
  59. <td className='dimension__item__key'>
  60. <span className={`dimension__item__indicator dimension__item__indicator--${roundTo10(((item.value * 1) / sum) * 100)}`} />
  61. <span title={item.key}>{item.human_key}</span>
  62. </td>
  63. <td className='dimension__item__value'>
  64. {typeof item.human_value !== 'undefined' ? item.human_value : <FormattedNumber value={item.value} />}
  65. </td>
  66. </tr>
  67. ))}
  68. </tbody>
  69. </table>
  70. );
  71. }
  72. return (
  73. <div className='dimension'>
  74. <h4>{label}</h4>
  75. {content}
  76. </div>
  77. );
  78. }
  79. }