lastCommit.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {Component} from 'react';
  2. import UserAvatar from 'app/components/avatar/userAvatar';
  3. import TimeSince from 'app/components/timeSince';
  4. import {t} from 'app/locale';
  5. import {AvatarUser, Commit} from 'app/types';
  6. type Props = {
  7. commit: Commit;
  8. headerClass: string;
  9. };
  10. const unknownUser: AvatarUser = {
  11. id: '',
  12. name: '',
  13. username: '??',
  14. email: '',
  15. avatarUrl: '',
  16. avatar: {
  17. avatarUuid: '',
  18. avatarType: 'letter_avatar',
  19. },
  20. ip_address: '',
  21. };
  22. class LastCommit extends Component<Props> {
  23. renderMessage(message: Commit['message']): string {
  24. if (!message) {
  25. return t('No message provided');
  26. }
  27. const firstLine = message.split(/\n/)[0];
  28. if (firstLine.length > 100) {
  29. let truncated = firstLine.substr(0, 90);
  30. const words = truncated.split(/ /);
  31. // try to not have elipsis mid-word
  32. if (words.length > 1) {
  33. words.pop();
  34. truncated = words.join(' ');
  35. }
  36. return truncated + '...';
  37. }
  38. return firstLine;
  39. }
  40. render() {
  41. const {commit, headerClass} = this.props;
  42. const commitAuthor = commit && commit.author;
  43. return (
  44. <div>
  45. <h6 className={headerClass}>Last commit</h6>
  46. <div className="commit">
  47. <div className="commit-avatar">
  48. <UserAvatar user={commitAuthor || unknownUser} />
  49. </div>
  50. <div className="commit-message truncate">
  51. {this.renderMessage(commit.message)}
  52. </div>
  53. <div className="commit-meta">
  54. <strong>{(commitAuthor && commitAuthor.name) || t('Unknown Author')}</strong>
  55. &nbsp;
  56. <TimeSince date={commit.dateCreated} />
  57. </div>
  58. </div>
  59. </div>
  60. );
  61. }
  62. }
  63. export default LastCommit;