download-project.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { useCommandProvider } from '@/features/ide-react/hooks/use-command-provider'
  2. import OLDropdownMenuItem from '@/shared/components/ol/ol-dropdown-menu-item'
  3. import OLTooltip from '@/shared/components/ol/ol-tooltip'
  4. import { isSmallDevice, sendMB } from '@/infrastructure/event-tracking'
  5. import { useDetachCompileContext as useCompileContext } from '@/shared/context/detach-compile-context'
  6. import { useProjectContext } from '@/shared/context/project-context'
  7. import { useCallback } from 'react'
  8. import { useTranslation } from 'react-i18next'
  9. import { useEditorAnalytics } from '@/shared/hooks/use-editor-analytics'
  10. import getMeta from '@/utils/meta'
  11. import { useFeatureFlag } from '@/shared/context/split-test-context'
  12. export const DownloadProjectZip = () => {
  13. const { t } = useTranslation()
  14. const { projectId } = useProjectContext()
  15. const sendDownloadEvent = useCallback(() => {
  16. sendMB('download-zip-button-click', {
  17. projectId,
  18. location: 'project-name-dropdown',
  19. isSmallDevice,
  20. })
  21. }, [projectId])
  22. useCommandProvider(
  23. () => [
  24. {
  25. id: 'download-as-source-zip',
  26. href: `/project/${projectId}/download/zip`,
  27. label: t('download_as_source_zip'),
  28. },
  29. ],
  30. [t, projectId]
  31. )
  32. return (
  33. <OLDropdownMenuItem
  34. href={`/project/${projectId}/download/zip`}
  35. target="_blank"
  36. rel="noreferrer"
  37. onClick={sendDownloadEvent}
  38. >
  39. {t('download_as_source_zip')}
  40. </OLDropdownMenuItem>
  41. )
  42. }
  43. export const DownloadProjectPDF = () => {
  44. const { t } = useTranslation()
  45. const { pdfDownloadUrl, pdfUrl } = useCompileContext()
  46. const { projectId } = useProjectContext()
  47. const { sendEvent } = useEditorAnalytics()
  48. const sendDownloadEvent = useCallback(() => {
  49. sendEvent('download-pdf-button-click', {
  50. projectId,
  51. location: 'project-name-dropdown',
  52. isSmallDevice,
  53. })
  54. }, [projectId, sendEvent])
  55. useCommandProvider(
  56. () => [
  57. {
  58. id: 'download-pdf',
  59. disabled: !pdfUrl,
  60. href: pdfDownloadUrl || pdfUrl,
  61. handler: ({ location }) => {
  62. sendEvent('download-pdf-button-click', {
  63. projectId,
  64. location,
  65. isSmallDevice,
  66. })
  67. },
  68. label: t('download_as_pdf'),
  69. },
  70. ],
  71. [t, pdfUrl, projectId, pdfDownloadUrl, sendEvent]
  72. )
  73. const button = (
  74. <OLDropdownMenuItem
  75. href={pdfDownloadUrl || pdfUrl}
  76. target="_blank"
  77. rel="noreferrer"
  78. onClick={sendDownloadEvent}
  79. disabled={!pdfUrl}
  80. >
  81. {t('download_as_pdf')}
  82. </OLDropdownMenuItem>
  83. )
  84. if (!pdfUrl) {
  85. return (
  86. <OLTooltip
  87. id="tooltip-download-pdf-unavailable"
  88. description={t('please_compile_pdf_before_download')}
  89. overlayProps={{ placement: 'right', delay: 0 }}
  90. >
  91. <span>{button}</span>
  92. </OLTooltip>
  93. )
  94. } else {
  95. return button
  96. }
  97. }
  98. export const ExportProjectDocx = () => {
  99. const { t } = useTranslation()
  100. const { projectId } = useProjectContext()
  101. const exportDocxEnabled = useFeatureFlag('export-docx')
  102. const enablePandocConversions =
  103. getMeta('ol-ExposedSettings')?.enablePandocConversions
  104. const anonymous = getMeta('ol-anonymous')
  105. const showExportDocx =
  106. exportDocxEnabled && enablePandocConversions && !anonymous
  107. useCommandProvider(
  108. () =>
  109. showExportDocx
  110. ? [
  111. {
  112. id: 'export-as-docx',
  113. href: `/project/${projectId}/download/conversion/docx`,
  114. label: t('export_as_docx'),
  115. },
  116. ]
  117. : [],
  118. [t, showExportDocx, projectId]
  119. )
  120. if (!showExportDocx) {
  121. return null
  122. }
  123. return (
  124. <OLDropdownMenuItem
  125. href={`/project/${projectId}/download/conversion/docx`}
  126. target="_blank"
  127. rel="noreferrer"
  128. >
  129. {t('export_as_docx')}
  130. </OLDropdownMenuItem>
  131. )
  132. }