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.

75 lines
2.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. import React from 'react';
  2. import ImmutablePropTypes from 'react-immutable-proptypes';
  3. import PropTypes from 'prop-types';
  4. import { autoPlayGif } from 'mastodon/initial_state';
  5. export default class DisplayName extends React.PureComponent {
  6. static propTypes = {
  7. account: ImmutablePropTypes.map.isRequired,
  8. others: ImmutablePropTypes.list,
  9. localDomain: PropTypes.string,
  10. };
  11. handleMouseEnter = ({ currentTarget }) => {
  12. if (autoPlayGif) {
  13. return;
  14. }
  15. const emojis = currentTarget.querySelectorAll('.custom-emoji');
  16. for (var i = 0; i < emojis.length; i++) {
  17. let emoji = emojis[i];
  18. emoji.src = emoji.getAttribute('data-original');
  19. }
  20. }
  21. handleMouseLeave = ({ currentTarget }) => {
  22. if (autoPlayGif) {
  23. return;
  24. }
  25. const emojis = currentTarget.querySelectorAll('.custom-emoji');
  26. for (var i = 0; i < emojis.length; i++) {
  27. let emoji = emojis[i];
  28. emoji.src = emoji.getAttribute('data-static');
  29. }
  30. }
  31. render () {
  32. const { others, localDomain } = this.props;
  33. let displayName, suffix, account;
  34. if (others && others.size > 1) {
  35. displayName = others.take(2).map(a => <bdi key={a.get('id')}><strong className='display-name__html' dangerouslySetInnerHTML={{ __html: a.get('display_name_html') }} /></bdi>).reduce((prev, cur) => [prev, ', ', cur]);
  36. if (others.size - 2 > 0) {
  37. suffix = `+${others.size - 2}`;
  38. }
  39. } else {
  40. if (others && others.size > 0) {
  41. account = others.first();
  42. } else {
  43. account = this.props.account;
  44. }
  45. let acct = account.get('acct');
  46. if (acct.indexOf('@') === -1 && localDomain) {
  47. acct = `${acct}@${localDomain}`;
  48. }
  49. displayName = <bdi><strong className='display-name__html' dangerouslySetInnerHTML={{ __html: account.get('display_name_html') }} /></bdi>;
  50. suffix = <span className='display-name__account'>@{acct}</span>;
  51. }
  52. return (
  53. <span className='display-name' onMouseEnter={this.handleMouseEnter} onMouseLeave={this.handleMouseLeave}>
  54. {displayName} {suffix}
  55. </span>
  56. );
  57. }
  58. }