index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const { h, Fragment } = require('preact')
  2. const prettierBytes = require('@transloadit/prettier-bytes')
  3. const truncateString = require('@uppy/utils/lib/truncateString')
  4. const MetaErrorMessage = require('../MetaErrorMessage')
  5. const renderFileName = (props) => {
  6. const { author, name } = props.file.meta
  7. function getMaxNameLength () {
  8. if (props.containerWidth <= 352) {
  9. return 35
  10. }
  11. if (props.containerWidth <= 576) {
  12. return 60
  13. }
  14. // When `author` is present, we want to make sure
  15. // the file name fits on one line so we can place
  16. // the author on the second line.
  17. return author ? 20 : 30
  18. }
  19. return (
  20. <div className="uppy-Dashboard-Item-name" title={name}>
  21. {truncateString(name, getMaxNameLength())}
  22. </div>
  23. )
  24. }
  25. const renderAuthor = (props) => {
  26. const { author } = props.file.meta
  27. const { providerName } = props.file.remote
  28. const dot = `\u00B7`
  29. if (!author) {
  30. return null
  31. }
  32. return (
  33. <div className="uppy-Dashboard-Item-author">
  34. <a
  35. href={`${author.url}?utm_source=Companion&utm_medium=referral`}
  36. target="_blank"
  37. rel="noopener noreferrer"
  38. >
  39. {truncateString(author.name, 13)}
  40. </a>
  41. {providerName ? (
  42. <Fragment>
  43. {` ${dot} `}
  44. {providerName}
  45. </Fragment>
  46. ) : null}
  47. </div>
  48. )
  49. }
  50. const renderFileSize = (props) => props.file.size && (
  51. <div className="uppy-Dashboard-Item-statusSize">
  52. {prettierBytes(props.file.size)}
  53. </div>
  54. )
  55. const ReSelectButton = (props) => props.file.isGhost && (
  56. <span>
  57. {' \u2022 '}
  58. <button
  59. className="uppy-u-reset uppy-c-btn uppy-Dashboard-Item-reSelect"
  60. type="button"
  61. onClick={props.toggleAddFilesPanel}
  62. >
  63. {props.i18n('reSelect')}
  64. </button>
  65. </span>
  66. )
  67. const ErrorButton = ({ file, onClick }) => {
  68. if (file.error) {
  69. return (
  70. <button
  71. className="uppy-u-reset uppy-Dashboard-Item-errorDetails"
  72. aria-label={file.error}
  73. data-microtip-position="bottom"
  74. data-microtip-size="medium"
  75. onClick={onClick}
  76. type="button"
  77. >
  78. ?
  79. </button>
  80. )
  81. }
  82. return null
  83. }
  84. module.exports = function FileInfo (props) {
  85. const { file } = props
  86. return (
  87. <div
  88. className="uppy-Dashboard-Item-fileInfo"
  89. data-uppy-file-source={file.source}
  90. >
  91. <div className="uppy-Dashboard-Item-fileName">
  92. {renderFileName(props)}
  93. <ErrorButton
  94. file={props.file}
  95. // eslint-disable-next-line no-alert
  96. onClick={() => alert(props.file.error)} // TODO: move to a custom alert implementation
  97. />
  98. </div>
  99. <div className="uppy-Dashboard-Item-status">
  100. {renderAuthor(props)}
  101. {renderFileSize(props)}
  102. {ReSelectButton(props)}
  103. </div>
  104. <MetaErrorMessage
  105. file={props.file}
  106. i18n={props.i18n}
  107. toggleFileCard={props.toggleFileCard}
  108. metaFields={props.metaFields}
  109. />
  110. </div>
  111. )
  112. }