download-project.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. export const DownloadProjectZip = () => {
  11. const { t } = useTranslation()
  12. const { projectId } = useProjectContext()
  13. const sendDownloadEvent = useCallback(() => {
  14. sendMB('download-zip-button-click', {
  15. projectId,
  16. location: 'project-name-dropdown',
  17. isSmallDevice,
  18. })
  19. }, [projectId])
  20. useCommandProvider(
  21. () => [
  22. {
  23. id: 'download-as-source-zip',
  24. href: `/project/${projectId}/download/zip`,
  25. label: t('download_as_source_zip'),
  26. },
  27. ],
  28. [t, projectId]
  29. )
  30. return (
  31. <OLDropdownMenuItem
  32. href={`/project/${projectId}/download/zip`}
  33. target="_blank"
  34. rel="noreferrer"
  35. onClick={sendDownloadEvent}
  36. >
  37. {t('download_as_source_zip')}
  38. </OLDropdownMenuItem>
  39. )
  40. }
  41. export const DownloadProjectPDF = () => {
  42. const { t } = useTranslation()
  43. const { pdfDownloadUrl, pdfUrl } = useCompileContext()
  44. const { projectId } = useProjectContext()
  45. const { sendEvent } = useEditorAnalytics()
  46. const sendDownloadEvent = useCallback(() => {
  47. sendEvent('download-pdf-button-click', {
  48. projectId,
  49. location: 'project-name-dropdown',
  50. isSmallDevice,
  51. })
  52. }, [projectId, sendEvent])
  53. useCommandProvider(
  54. () => [
  55. {
  56. id: 'download-pdf',
  57. disabled: !pdfUrl,
  58. href: pdfDownloadUrl || pdfUrl,
  59. handler: ({ location }) => {
  60. sendEvent('download-pdf-button-click', {
  61. projectId,
  62. location,
  63. isSmallDevice,
  64. })
  65. },
  66. label: t('download_as_pdf'),
  67. },
  68. ],
  69. [t, pdfUrl, projectId, pdfDownloadUrl, sendEvent]
  70. )
  71. const button = (
  72. <OLDropdownMenuItem
  73. href={pdfDownloadUrl || pdfUrl}
  74. target="_blank"
  75. rel="noreferrer"
  76. onClick={sendDownloadEvent}
  77. disabled={!pdfUrl}
  78. >
  79. {t('download_as_pdf')}
  80. </OLDropdownMenuItem>
  81. )
  82. if (!pdfUrl) {
  83. return (
  84. <OLTooltip
  85. id="tooltip-download-pdf-unavailable"
  86. description={t('please_compile_pdf_before_download')}
  87. overlayProps={{ placement: 'right', delay: 0 }}
  88. >
  89. <span>{button}</span>
  90. </OLTooltip>
  91. )
  92. } else {
  93. return button
  94. }
  95. }