Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(import): stop the zip import progress total from jumping mid-import
Elian Dorand3718c097fc9
diff --git a/packages/trilium-core/src/services/import/zip.spec.ts b/packages/trilium-core/src/services/import/zip.spec.tsindex ab556d0a0f..f124663a27 100644--- a/packages/trilium-core/src/services/import/zip.spec.ts+++ b/packages/trilium-core/src/services/import/zip.spec.ts@@ -258,6 +258,33 @@ describe("processNoteContent", () => {expect(finalTotal).toBe(increaseSpy.mock.calls.length);});+ it("sets the progress total up front so the denominator doesn't jump mid-import", async () => {+ const taskContext = TaskContext.getInstance("import-progress-no-jump", "importNotes", { textImportedAsText: true });+ const setTotalSpy = vi.spyOn(taskContext, "setTotalCount");++ // Flat files at the root: each becomes a note, no folder notes, no meta file.+ const zipBuffer = await createZipBuffer({ "a.txt": "first", "b.txt": "second", "c.txt": "third" });++ await new Promise<void>((resolve, reject) => {+ getContext().init(async () => {+ const rootNote = becca.getNote("root");+ if (!rootNote) {+ reject(new Error("missing root note"));+ return;+ }+ await zip.importZip(taskContext, zipBuffer, rootNote as BNote);+ resolve();+ });+ });++ const firstTotal = setTotalSpy.mock.calls[0]?.[0] ?? 0;+ const finalTotal = setTotalSpy.mock.calls.at(-1)?.[0] ?? 0;+ // The first total already accounts for the post-processing phase (3 entries + 3 notes = 6), rather+ // than the bare entry count (3) — so the denominator never leaps from 3 to 6 partway through.+ expect(firstTotal).toBe(6);+ expect(finalTotal).toBe(firstTotal);+ });+it("imports a CSV entry as a plain file note when the spreadsheet option is off", async () => {const zipBuffer = await createZipBuffer({ "csv_as_file_sample.csv": "a,b\r\n1,2" });const { rootNote } = await testImportBuffer(zipBuffer, "import-csv-off", { spreadsheetImportedAsSpreadsheet: false });diff --git a/packages/trilium-core/src/services/import/zip.ts b/packages/trilium-core/src/services/import/zip.tsindex 564cfeb3a0..6c17b02f40 100644--- a/packages/trilium-core/src/services/import/zip.ts+++ b/packages/trilium-core/src/services/import/zip.ts@@ -696,8 +696,14 @@ async function importZip(taskContext: TaskContext<"importNotes">, source: ZipSoutopLevelPath = (topLevelItems.size > 1 ? "" : topLevelItems.values().next().value ?? "");- // the processing pass increments progress once per entry; expose the total so the client shows a bar- taskContext.setTotalCount(entriesToProcess);+ // The import is counted in two phases: extracting every entry, then post-processing each created note.+ // Set the total to cover *both* up front so the bar climbs monotonically — otherwise the denominator+ // jumps mid-import (e.g. "X of 32k" → "X of 60k") when post-processing starts. The post-process count+ // is taken from the meta file when present (exact for a Trilium export); without one, every entry+ // becomes a note, so the entry count is the right estimate. Any small mismatch is corrected exactly by+ // the setTotalCount below, which then barely moves instead of leaping.+ const estimatedNoteCount = countImportableNotes(metaFile) || entriesToProcess;+ taskContext.setTotalCount(entriesToProcess + estimatedNoteCount);await zipProvider.readZipFile(source, async (entry, readContent) => {const filePath = normalizeFilePath(entry.fileName);@@ -823,6 +829,22 @@ export function removeTriliumTags(content: string) {return content;}+/**+ * Estimates how many notes an import will create — the post-processing phase's unit count — from the meta+ * tree, so the progress total can be set once up front. Clones and explicitly non-imported notes don't+ * create a note, so they're not counted. Returns 0 when there's no meta file (the caller then falls back to+ * the entry count, since without meta every content entry becomes a note). `metaFile` is typed nullable+ * because control-flow analysis can't see it being assigned inside the read callback.+ */+function countImportableNotes(metaFile: MetaFile | null): number {+ if (!metaFile) {+ return 0;+ }+ const count = (notes: NoteMeta[]): number =>+ notes.reduce((sum, note) => sum + (note.isClone || note.noImport ? 0 : 1) + count(note.children ?? []), 0);+ return count(metaFile.files);+}+export default {importZip};