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.

70 lines
2.6 KiB

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