attachment.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* jshint ignore:start */
  2. import React from 'react';
  3. import misago from 'misago';
  4. import escapeHtml from 'misago/utils/escape-html';
  5. import formatFilesize from 'misago/utils/file-size';
  6. const DATE_ABBR = '<abbr title="%(absolute)s">%(relative)s</abbr>';
  7. const USER_SPAN = '<span class="item-title">%(user)s</span>';
  8. const USER_URL = '<a href="%(url)s" class="item-title">%(user)s</a>';
  9. export default function(props) {
  10. if (props.attachment === null) {
  11. return (
  12. <td className="col-md-6">
  13. &nbsp;
  14. </td>
  15. );
  16. }
  17. return (
  18. <td className="col-md-6">
  19. <AttachmentPreview {...props} />
  20. <div className="post-attachment">
  21. <a href={props.attachment.url.index} className="item-title">
  22. {props.attachment.filename}
  23. </a>
  24. <AttachmentDetails {...props} />
  25. </div>
  26. </td>
  27. );
  28. }
  29. export function AttachmentPreview(props) {
  30. if (props.attachment.is_image) {
  31. return (
  32. <div className="post-attachment-preview">
  33. <AttachmentThumbnail {...props} />
  34. </div>
  35. );
  36. } else {
  37. return (
  38. <div className="post-attachment-preview">
  39. <AttachmentIcon {...props} />
  40. </div>
  41. );
  42. }
  43. }
  44. export function AttachmentIcon(props) {
  45. return (
  46. <a href={props.attachment.url.index} className="material-icon">
  47. insert_drive_file
  48. </a>
  49. );
  50. }
  51. export function AttachmentThumbnail(props) {
  52. const url = props.attachment.url.thumb || props.attachment.url.index;
  53. return (
  54. <a
  55. className="post-thumbnail"
  56. href={props.attachment.url.index}
  57. style={{backgroundImage: 'url("' + escapeHtml(url) + '")'}}
  58. />
  59. );
  60. }
  61. export function AttachmentDetails(props) {
  62. let user = null;
  63. if (props.attachment.url.uploader) {
  64. user = interpolate(USER_URL, {
  65. url: escapeHtml(props.attachment.url.uploader),
  66. user: escapeHtml(props.attachment.uploader_name)
  67. }, true);
  68. } else {
  69. user = interpolate(USER_SPAN, {
  70. user: escapeHtml(props.attachment.uploader_name)
  71. }, true);
  72. }
  73. const date = interpolate(DATE_ABBR, {
  74. absolute: escapeHtml(props.attachment.uploaded_on.format('LLL')),
  75. relative: escapeHtml(props.attachment.uploaded_on.fromNow())
  76. }, true);
  77. const message = interpolate(escapeHtml(gettext("%(filetype)s, %(size)s, uploaded by %(uploader)s %(uploaded_on)s.")), {
  78. filetype: props.attachment.filetype,
  79. size: formatFilesize(props.attachment.size),
  80. uploader: user,
  81. uploaded_on: date
  82. }, true);
  83. return (
  84. <p
  85. className="post-attachment-description"
  86. dangerouslySetInnerHTML={{__html: message}}
  87. />
  88. );
  89. }