index.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import PropTypes from 'prop-types';
  2. import { PureComponent } from 'react';
  3. import { FormattedMessage, injectIntl } from 'react-intl';
  4. import { connect } from 'react-redux';
  5. import ArrowDropDownIcon from '@/material-icons/400-24px/arrow_drop_down.svg?react';
  6. import { openModal } from 'mastodon/actions/modal';
  7. import { Icon } from 'mastodon/components/icon';
  8. import InlineAccount from 'mastodon/components/inline_account';
  9. import { RelativeTimestamp } from 'mastodon/components/relative_timestamp';
  10. import DropdownMenu from './containers/dropdown_menu_container';
  11. const mapDispatchToProps = (dispatch, { statusId }) => ({
  12. onItemClick (index) {
  13. dispatch(openModal({
  14. modalType: 'COMPARE_HISTORY',
  15. modalProps: { index, statusId },
  16. }));
  17. },
  18. });
  19. class EditedTimestamp extends PureComponent {
  20. static propTypes = {
  21. statusId: PropTypes.string.isRequired,
  22. timestamp: PropTypes.string.isRequired,
  23. intl: PropTypes.object.isRequired,
  24. onItemClick: PropTypes.func.isRequired,
  25. };
  26. handleItemClick = (item, i) => {
  27. const { onItemClick } = this.props;
  28. onItemClick(i);
  29. };
  30. renderHeader = items => {
  31. return (
  32. <FormattedMessage id='status.edited_x_times' defaultMessage='Edited {count, plural, one {# time} other {# times}}' values={{ count: items.size - 1 }} />
  33. );
  34. };
  35. renderItem = (item, index, { onClick, onKeyPress }) => {
  36. const formattedDate = <RelativeTimestamp timestamp={item.get('created_at')} short={false} />;
  37. const formattedName = <InlineAccount accountId={item.get('account')} />;
  38. const label = item.get('original') ? (
  39. <FormattedMessage id='status.history.created' defaultMessage='{name} created {date}' values={{ name: formattedName, date: formattedDate }} />
  40. ) : (
  41. <FormattedMessage id='status.history.edited' defaultMessage='{name} edited {date}' values={{ name: formattedName, date: formattedDate }} />
  42. );
  43. return (
  44. <li className='dropdown-menu__item edited-timestamp__history__item' key={item.get('created_at')}>
  45. <button data-index={index} onClick={onClick} onKeyPress={onKeyPress}>{label}</button>
  46. </li>
  47. );
  48. };
  49. render () {
  50. const { timestamp, intl, statusId } = this.props;
  51. return (
  52. <DropdownMenu statusId={statusId} renderItem={this.renderItem} scrollable renderHeader={this.renderHeader} onItemClick={this.handleItemClick}>
  53. <button className='dropdown-menu__text-button'>
  54. <FormattedMessage id='status.edited' defaultMessage='Edited {date}' values={{ date: intl.formatDate(timestamp, { hour12: false, month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' }) }} /> <Icon id='caret-down' icon={ArrowDropDownIcon} />
  55. </button>
  56. </DropdownMenu>
  57. );
  58. }
  59. }
  60. export default connect(null, mapDispatchToProps)(injectIntl(EditedTimestamp));