Counter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 { Sparklines, SparklinesCurve } from 'react-sparklines';
  6. import classNames from 'classnames';
  7. import Skeleton from 'mastodon/components/skeleton';
  8. const percIncrease = (a, b) => {
  9. let percent;
  10. if (b !== 0) {
  11. if (a !== 0) {
  12. percent = (b - a) / a;
  13. } else {
  14. percent = 1;
  15. }
  16. } else if (b === 0 && a === 0) {
  17. percent = 0;
  18. } else {
  19. percent = - 1;
  20. }
  21. return percent;
  22. };
  23. export default class Counter extends React.PureComponent {
  24. static propTypes = {
  25. measure: PropTypes.string.isRequired,
  26. start_at: PropTypes.string.isRequired,
  27. end_at: PropTypes.string.isRequired,
  28. label: PropTypes.string.isRequired,
  29. href: PropTypes.string,
  30. params: PropTypes.object,
  31. target: PropTypes.string,
  32. };
  33. state = {
  34. loading: true,
  35. data: null,
  36. };
  37. componentDidMount () {
  38. const { measure, start_at, end_at, params } = this.props;
  39. api().post('/api/v1/admin/measures', { keys: [measure], start_at, end_at, [measure]: params }).then(res => {
  40. this.setState({
  41. loading: false,
  42. data: res.data,
  43. });
  44. }).catch(err => {
  45. console.error(err);
  46. });
  47. }
  48. render () {
  49. const { label, href, target } = this.props;
  50. const { loading, data } = this.state;
  51. let content;
  52. if (loading) {
  53. content = (
  54. <React.Fragment>
  55. <span className='sparkline__value__total'><Skeleton width={43} /></span>
  56. <span className='sparkline__value__change'><Skeleton width={43} /></span>
  57. </React.Fragment>
  58. );
  59. } else {
  60. const measure = data[0];
  61. const percentChange = measure.previous_total && percIncrease(measure.previous_total * 1, measure.total * 1);
  62. content = (
  63. <React.Fragment>
  64. <span className='sparkline__value__total'>{measure.human_value || <FormattedNumber value={measure.total} />}</span>
  65. {measure.previous_total && (<span className={classNames('sparkline__value__change', { positive: percentChange > 0, negative: percentChange < 0 })}>{percentChange > 0 && '+'}<FormattedNumber value={percentChange} style='percent' /></span>)}
  66. </React.Fragment>
  67. );
  68. }
  69. const inner = (
  70. <React.Fragment>
  71. <div className='sparkline__value'>
  72. {content}
  73. </div>
  74. <div className='sparkline__label'>
  75. {label}
  76. </div>
  77. <div className='sparkline__graph'>
  78. {!loading && (
  79. <Sparklines width={259} height={55} data={data[0].data.map(x => x.value * 1)}>
  80. <SparklinesCurve />
  81. </Sparklines>
  82. )}
  83. </div>
  84. </React.Fragment>
  85. );
  86. if (href) {
  87. return (
  88. <a href={href} className='sparkline' target={target}>
  89. {inner}
  90. </a>
  91. );
  92. } else {
  93. return (
  94. <div className='sparkline'>
  95. {inner}
  96. </div>
  97. );
  98. }
  99. }
  100. }