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.

100 lines
2.3 KiB

  1. // @ts-check
  2. import React from 'react';
  3. import { Sparklines, SparklinesCurve } from 'react-sparklines';
  4. import { FormattedMessage } from 'react-intl';
  5. import PropTypes from 'prop-types';
  6. import ImmutablePropTypes from 'react-immutable-proptypes';
  7. import Permalink from './permalink';
  8. import ShortNumber from 'mastodon/components/short_number';
  9. class SilentErrorBoundary extends React.Component {
  10. static propTypes = {
  11. children: PropTypes.node,
  12. };
  13. state = {
  14. error: false,
  15. };
  16. componentDidCatch () {
  17. this.setState({ error: true });
  18. }
  19. render () {
  20. if (this.state.error) {
  21. return null;
  22. }
  23. return this.props.children;
  24. }
  25. }
  26. /**
  27. * Used to render counter of how much people are talking about hashtag
  28. *
  29. * @type {(displayNumber: JSX.Element, pluralReady: number) => JSX.Element}
  30. */
  31. const accountsCountRenderer = (displayNumber, pluralReady) => (
  32. <FormattedMessage
  33. id='trends.counter_by_accounts'
  34. defaultMessage='{count, plural, one {{counter} person} other {{counter} people}} talking'
  35. values={{
  36. count: pluralReady,
  37. counter: <strong>{displayNumber}</strong>,
  38. }}
  39. />
  40. );
  41. const Hashtag = ({ hashtag }) => (
  42. <div className='trends__item'>
  43. <div className='trends__item__name'>
  44. <Permalink
  45. href={hashtag.get('url')}
  46. to={`/timelines/tag/${hashtag.get('name')}`}
  47. >
  48. #<span>{hashtag.get('name')}</span>
  49. </Permalink>
  50. <ShortNumber
  51. value={
  52. hashtag.getIn(['history', 0, 'accounts']) * 1 +
  53. hashtag.getIn(['history', 1, 'accounts']) * 1
  54. }
  55. renderer={accountsCountRenderer}
  56. />
  57. </div>
  58. <div className='trends__item__current'>
  59. <ShortNumber
  60. value={
  61. hashtag.getIn(['history', 0, 'uses']) * 1 +
  62. hashtag.getIn(['history', 1, 'uses']) * 1
  63. }
  64. />
  65. </div>
  66. <div className='trends__item__sparkline'>
  67. <SilentErrorBoundary>
  68. <Sparklines
  69. width={50}
  70. height={28}
  71. data={hashtag
  72. .get('history')
  73. .reverse()
  74. .map((day) => day.get('uses'))
  75. .toArray()}
  76. >
  77. <SparklinesCurve style={{ fill: 'none' }} />
  78. </Sparklines>
  79. </SilentErrorBoundary>
  80. </div>
  81. </div>
  82. );
  83. Hashtag.propTypes = {
  84. hashtag: ImmutablePropTypes.map.isRequired,
  85. };
  86. export default Hashtag;