preview-download-button.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import { Dropdown, MenuItem } from 'react-bootstrap'
  4. import { useTranslation, Trans } from 'react-i18next'
  5. import Icon from '../../../shared/components/icon'
  6. export const topFileTypes = ['bbl', 'gls', 'ind']
  7. function PreviewDownloadButton({ isCompiling, outputFiles, pdfDownloadUrl }) {
  8. let topFiles = []
  9. let otherFiles = []
  10. const { t } = useTranslation()
  11. if (outputFiles) {
  12. topFiles = outputFiles.filter(file => {
  13. if (topFileTypes.includes(file.type)) {
  14. return file
  15. }
  16. })
  17. otherFiles = outputFiles.filter(file => {
  18. if (!topFileTypes.includes(file.type)) {
  19. if (file.type === 'pdf' && file.main === true) return
  20. return file
  21. }
  22. })
  23. }
  24. return (
  25. <Dropdown id="download-dropdown" disabled={isCompiling}>
  26. <a
  27. className="btn btn-xs btn-info"
  28. disabled={isCompiling || !pdfDownloadUrl}
  29. download
  30. href={pdfDownloadUrl || '#'}
  31. >
  32. <Icon type="download" modifier="fw" /> {t('download_pdf')}
  33. </a>
  34. <Dropdown.Toggle
  35. className="btn btn-xs btn-info dropdown-toggle"
  36. aria-label={t('toggle_output_files_list')}
  37. />
  38. <Dropdown.Menu id="download-dropdown-list">
  39. <FileList list={topFiles} listType="main" />
  40. {otherFiles.length > 0 && (
  41. <>
  42. <MenuItem divider />
  43. <MenuItem header>{t('other_output_files')}</MenuItem>
  44. <FileList list={otherFiles} listType="other" />
  45. </>
  46. )}
  47. </Dropdown.Menu>
  48. </Dropdown>
  49. )
  50. }
  51. function FileList({ listType, list }) {
  52. return list.map((file, index) => {
  53. return (
  54. <MenuItem download href={file.url} key={`${listType}-${index}`}>
  55. <Trans
  56. i18nKey="download_file"
  57. components={[<strong />]}
  58. values={{ type: file.fileName }}
  59. />
  60. </MenuItem>
  61. )
  62. })
  63. }
  64. PreviewDownloadButton.propTypes = {
  65. isCompiling: PropTypes.bool.isRequired,
  66. outputFiles: PropTypes.array,
  67. pdfDownloadUrl: PropTypes.string
  68. }
  69. FileList.propTypes = {
  70. list: PropTypes.array.isRequired,
  71. listType: PropTypes.string.isRequired
  72. }
  73. export default PreviewDownloadButton