Files
overleaf-cep/services/web/frontend/js/features/ide-react/hooks/use-convert-project.ts
Mathias Jakobsen 6b28a4ee5a Merge pull request #33560 from overleaf/mj-conversion-cleanup
[clsi+web] Small cleanups and improvements to conversions / exports

GitOrigin-RevId: 300adfbb91e89f754ee7f835db792ccb50b27613
2026-05-12 08:06:17 +00:00

49 lines
1.5 KiB
TypeScript

import { getJSON } from '@/infrastructure/fetch-json'
import { useLocation } from '@/shared/hooks/use-location'
import { debugConsole } from '@/utils/debugging'
import { useProjectContext } from '@/shared/context/project-context'
import { useCallback } from 'react'
import {
hidePreparingExportToast,
showExportDocumentError,
showExportDocumentSuccess,
showPreparingExportToast,
} from '../components/toolbar/export-document-toasts'
const SLOW_CONVERSION_THRESHOLD = 2000
export default function useConvertProject(type: 'docx' | 'markdown') {
const { projectId } = useProjectContext()
const location = useLocation()
const triggerConversion = useCallback(async () => {
let handle: string | undefined
const toastTimer = setTimeout(() => {
handle = showPreparingExportToast()
}, SLOW_CONVERSION_THRESHOLD)
const hidePreparingToast = () => {
clearTimeout(toastTimer)
if (handle) hidePreparingExportToast(handle)
}
try {
const response = await getJSON(
`/project/${projectId}/download/conversion/${type}?responseFormat=json`
)
hidePreparingToast()
const { downloadUrl } = response
if (downloadUrl) {
const url = new URL(downloadUrl, window.location.origin)
location.assign(url.toString())
showExportDocumentSuccess(type)
} else {
showExportDocumentError()
}
} catch (error) {
hidePreparingToast()
showExportDocumentError()
debugConsole.error(error)
}
}, [projectId, type, location])
return triggerConversion
}