diff --git a/apps/app/src/app/routes/EditorRoute.tsx b/apps/app/src/app/routes/EditorRoute.tsx index 62dd0dc0..525f99a2 100644 --- a/apps/app/src/app/routes/EditorRoute.tsx +++ b/apps/app/src/app/routes/EditorRoute.tsx @@ -33,6 +33,7 @@ import type { SheetIndex } from '@/services/sheetIndex' import { pickLabel } from '@/i18n/pickLocaleLabel' import type { ViewStates } from '@/anatomy/drawing' import { exportReportToPdf } from '@/pdf/export' +import { downloadBlob } from '@/lib/download' import type { ExportWarning } from '@faunosteo/pdf' import type { RebaseOutcome } from '@faunosteo/core-wasm/types' import { useT } from '@/i18n/useT' @@ -236,13 +237,10 @@ export default function EditorRoute() { const { pdf, warnings } = await exportReportToPdf(report, index, { locale, }) - const blob = new Blob([pdf as BlobPart], { type: 'application/pdf' }) - const url = URL.createObjectURL(blob) - const a = document.createElement('a') - a.href = url - a.download = `bilan-${report.animal.name}-${report.date}.pdf` - a.click() - URL.revokeObjectURL(url) + downloadBlob( + new Blob([pdf as BlobPart], { type: 'application/pdf' }), + `bilan-${report.animal.name}-${report.date}.pdf`, + ) const missing = missingItemsOf(warnings) if (missing.length) { window.alert( diff --git a/apps/app/src/lib/download.ts b/apps/app/src/lib/download.ts index e19ec8e1..009fa9f4 100644 --- a/apps/app/src/lib/download.ts +++ b/apps/app/src/lib/download.ts @@ -3,18 +3,28 @@ import { todayIso } from '@/lib/dates' const REVOKE_DELAY_MS = 30_000 /** - * Saves `raw` as `-.json`. A lone surrogate in a JSON.stringify output is a - * `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a - * deletion: the anchor is attached while clicked and the URL revoked later, since some - * engines (WebKit) cancel a download whose URL is revoked at once. + * Saves `blob` under `fileName`. The anchor is attached while clicked and the URL revoked + * later, since some engines (WebKit) cancel a download whose URL is revoked at once. */ -export function downloadJson(raw: string, stem: string): void { - const url = URL.createObjectURL(new Blob([raw], { type: 'application/json' })) +export function downloadBlob(blob: Blob, fileName: string): void { + const url = URL.createObjectURL(blob) const a = document.createElement('a') a.href = url - a.download = `${stem}-${todayIso()}.json` + a.download = fileName document.body.appendChild(a) a.click() a.remove() setTimeout(() => URL.revokeObjectURL(url), REVOKE_DELAY_MS) } + +/** + * Saves `raw` as `-.json`. A lone surrogate in a JSON.stringify output is a + * `\uXXXX` escape, so the UTF-8 Blob is lossless. This copy may be the last one before a + * deletion. + */ +export function downloadJson(raw: string, stem: string): void { + downloadBlob( + new Blob([raw], { type: 'application/json' }), + `${stem}-${todayIso()}.json`, + ) +} diff --git a/apps/app/tests/unit/routes.EditorRoute.export.test.tsx b/apps/app/tests/unit/routes.EditorRoute.export.test.tsx index c1bd4e8c..7d20d875 100644 --- a/apps/app/tests/unit/routes.EditorRoute.export.test.tsx +++ b/apps/app/tests/unit/routes.EditorRoute.export.test.tsx @@ -31,6 +31,7 @@ const PDF_BYTES = new Uint8Array([37, 80, 68, 70]) let sheet: SheetIndex const downloads: string[] = [] +const clicked: { connectedAtClick: boolean }[] = [] const realCreateObjectURL = URL.createObjectURL const realRevokeObjectURL = URL.revokeObjectURL @@ -48,17 +49,20 @@ beforeEach(() => { .hydrateImport({ report: minimalReport(sheet), archive: null }, sheet) exportMock.mockReset() downloads.length = 0 + clicked.length = 0 URL.createObjectURL = vi.fn(() => 'blob:faunosteo-test') URL.revokeObjectURL = vi.fn() vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function ( this: HTMLAnchorElement, ) { downloads.push(this.download) + clicked.push({ connectedAtClick: this.isConnected }) }) vi.stubGlobal('alert', vi.fn()) }) afterEach(() => { cleanup() + vi.useRealTimers() vi.restoreAllMocks() vi.unstubAllGlobals() URL.createObjectURL = realCreateObjectURL @@ -94,7 +98,8 @@ describe('EditorRoute export', () => { expect(exportMock.mock.calls[0]![2]).toEqual({ locale: 'en' }) }) - it('downloads bilan--.pdf through a blob URL it then revokes', async () => { + it('downloads bilan--.pdf through a blob URL it revokes after a delay', async () => { + vi.useFakeTimers({ shouldAdvanceTime: true }) exportMock.mockResolvedValue({ pdf: PDF_BYTES, warnings: [] }) await mountAndExport(fr['editor.export']!) await waitFor(() => expect(downloads).toHaveLength(1)) @@ -102,6 +107,10 @@ describe('EditorRoute export', () => { expect(URL.createObjectURL).toHaveBeenCalledTimes(1) const blob = vi.mocked(URL.createObjectURL).mock.calls[0]![0] as Blob expect(blob.type).toBe('application/pdf') + expect(clicked[0]!.connectedAtClick).toBe(true) + expect(document.body.querySelector('a[download]')).toBeNull() + expect(URL.revokeObjectURL).not.toHaveBeenCalled() + vi.advanceTimersByTime(30_000) expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:faunosteo-test') expect(window.alert).not.toHaveBeenCalled() })