Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): show a progress bar with page totals during OneNote import

Elian Doran895df5fe6f35
diff --git a/apps/client/src/services/import.spec.ts b/apps/client/src/services/import.spec.ts
index c4384ade24..f6f5793d37 100644
--- a/apps/client/src/services/import.spec.ts
+++ b/apps/client/src/services/import.spec.ts
@@ -146,12 +146,18 @@ describe("importNotes ws handler", () => {
expect(toastService.showError).toHaveBeenCalledWith("nope");
});
- it("shows a persistent progress toast on taskProgressCount", async () => {
+ it("shows a persistent count-only progress toast when no total is reported", async () => {
await handler()({ type: "taskProgressCount", taskType: "importNotes", taskId: "t2", progressCount: 3 } as any);
expect(toastService.showPersistent).toHaveBeenCalledTimes(1);
const toast = (toastService.showPersistent as any).mock.calls[0][0];
expect(toast.id).toBe("t2");
- expect(toast.icon).toBe("plus");
+ expect(toast.progress).toBeUndefined();
+ });
+
+ it("shows a progress-bar toast when a total is reported", async () => {
+ await handler()({ type: "taskProgressCount", taskType: "importNotes", taskId: "t2", progressCount: 3, totalCount: 12 } as any);
+ const toast = (toastService.showPersistent as any).mock.calls[0][0];
+ expect(toast.progress).toBe(3 / 12);
});
it("shows a success toast and navigates to the imported note when one is returned", async () => {
diff --git a/apps/client/src/services/import.ts b/apps/client/src/services/import.ts
index bb3892ac91..7d14451764 100644
--- a/apps/client/src/services/import.ts
+++ b/apps/client/src/services/import.ts
@@ -67,6 +67,23 @@ function makeToast(id: string, message: string): ToastOptionsWithRequiredId {
};
}
+/**
+ * Builds the persistent "import in progress" toast. When the task reported a total up front, it shows
+ * an "X of N" message with a progress bar; otherwise it falls back to a bare running count.
+ */
+function makeProgressToast(taskId: string, progressCount: number, totalCount?: number): ToastOptionsWithRequiredId {
+ const hasTotal = typeof totalCount === "number" && totalCount > 0;
+ return {
+ id: taskId,
+ title: t("import.import-status"),
+ icon: "bx bx-loader-circle bx-spin",
+ message: hasTotal
+ ? t("import.in-progress-with-total", { progress: progressCount, total: totalCount })
+ : t("import.in-progress", { progress: progressCount }),
+ ...(hasTotal ? { progress: progressCount / totalCount } : {})
+ };
+}
+
ws.subscribeToMessages(async (message) => {
if (!("taskType" in message) || message.taskType !== "importNotes") {
return;
@@ -76,7 +93,7 @@ ws.subscribeToMessages(async (message) => {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
- toastService.showPersistent(makeToast(message.taskId, t("import.in-progress", { progress: message.progressCount })));
+ toastService.showPersistent(makeProgressToast(message.taskId, message.progressCount, message.totalCount));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("import.successful"));
toast.timeout = 5000;
@@ -98,7 +115,7 @@ ws.subscribeToMessages(async (message: WebSocketMessage) => {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === "taskProgressCount") {
- toastService.showPersistent(makeToast(message.taskId, t("import.in-progress", { progress: message.progressCount })));
+ toastService.showPersistent(makeProgressToast(message.taskId, message.progressCount, message.totalCount));
} else if (message.type === "taskSucceeded") {
const toast = makeToast(message.taskId, t("import.successful"));
toast.timeout = 5000;
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 0454ecac63..4004b0df3f 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -226,6 +226,7 @@
},
"import-status": "Import status",
"in-progress": "Import in progress: {{progress}}",
+ "in-progress-with-total": "Importing {{progress}} of {{total}}",
"successful": "Import finished successfully."
},
"import_provider": {
diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.ts
index b8f2a3a41c..31f2ba85d3 100644
--- a/apps/server/src/services/import/onenote/importer.ts
+++ b/apps/server/src/services/import/onenote/importer.ts
@@ -10,7 +10,7 @@ import { parse } from "node-html-parser";
import sql from "../../sql.js";
import converter, { ONENOTE_ATTACHMENT_CLASS } from "./converter.js";
-import graph from "./graph.js";
+import graph, { type OneNotePage } from "./graph.js";
import { inkmlToSvg } from "./inkml.js";
export interface SectionSelection {
@@ -53,9 +53,17 @@ export async function importSelection({ accessToken, parentNoteId, sections, tas
// Phase 1: pull everything over the network first, so note creation can run in a single
// synchronous transaction afterwards.
- const fetched: FetchedSection[] = [];
+
+ // Enumerate every selected section's pages up front so the total page count is known before
+ // any content is fetched — this lets the client show a real progress bar rather than a bare count.
+ const sectionPages: { section: SectionSelection; pages: OneNotePage[] }[] = [];
for (const section of sections) {
- const pages = await graph.listPages(accessToken, section.id);
+ sectionPages.push({ section, pages: await graph.listPages(accessToken, section.id) });
+ }
+ taskContext.setTotalCount(sectionPages.reduce((total, entry) => total + entry.pages.length, 0));
+
+ const fetched: FetchedSection[] = [];
+ for (const { section, pages } of sectionPages) {
const fetchedPages: FetchedPage[] = [];
for (const page of pages) {
const { html: rawHtml, inkml } = await graph.getPageContent(accessToken, page.id);
diff --git a/packages/commons/src/lib/ws_api.ts b/packages/commons/src/lib/ws_api.ts
index 74cc643a80..b847b0e05b 100644
--- a/packages/commons/src/lib/ws_api.ts
+++ b/packages/commons/src/lib/ws_api.ts
@@ -70,7 +70,9 @@ type TaskDefinition<T extends TaskType> = {
taskId: string;
taskType: T;
data: TaskData<T>,
- progressCount: number
+ progressCount: number;
+ /** Total expected units of work, when known up front; lets the client show a progress bar. */
+ totalCount?: number;
} | {
type: "taskError",
taskId: string;
diff --git a/packages/trilium-core/src/services/task_context.spec.ts b/packages/trilium-core/src/services/task_context.spec.ts
index e8ea5bf7ab..b17caba200 100644
--- a/packages/trilium-core/src/services/task_context.spec.ts
+++ b/packages/trilium-core/src/services/task_context.spec.ts
@@ -96,6 +96,21 @@ describe("TaskContext", () => {
expect(sendMessageToAllClients).not.toHaveBeenCalled();
});
+
+ it("includes the total once set, and omits it until then", () => {
+ vi.useFakeTimers();
+ vi.setSystemTime(1_000);
+
+ const ctx = new TaskContext("total-1", "importNotes", importData);
+ // The constructor's first message has no total yet.
+ expect(sendMessageToAllClients).toHaveBeenLastCalledWith(expect.not.objectContaining({ totalCount: expect.anything() }));
+
+ ctx.setTotalCount(10);
+ vi.setSystemTime(1_300);
+ ctx.increaseProgressCount();
+
+ expect(sendMessageToAllClients).toHaveBeenLastCalledWith(expect.objectContaining({ progressCount: 1, totalCount: 10 }));
+ });
});
describe("reportError", () => {
diff --git a/packages/trilium-core/src/services/task_context.ts b/packages/trilium-core/src/services/task_context.ts
index 79122895bd..09528b29ae 100644
--- a/packages/trilium-core/src/services/task_context.ts
+++ b/packages/trilium-core/src/services/task_context.ts
@@ -10,6 +10,7 @@ class TaskContext<T extends TaskType> {
private taskId: string;
private taskType: TaskType;
private progressCount: number;
+ private totalCount: number | null;
private lastSentCountTs: number;
data: TaskData<T>;
noteDeletionHandlerTriggered: boolean;
@@ -19,6 +20,7 @@ class TaskContext<T extends TaskType> {
this.taskType = taskType;
this.data = data;
this.noteDeletionHandlerTriggered = false;
+ this.totalCount = null;
// progressCount is meant to represent just some progress - to indicate the task is not stuck
this.progressCount = -1; // we're incrementing immediately
@@ -39,6 +41,15 @@ class TaskContext<T extends TaskType> {
return taskContexts[taskId];
}
+ /**
+ * Sets the total expected units of work, so progress messages can carry a denominator and the
+ * client can show a progress bar instead of a bare count. Optional — tasks that don't know their
+ * total up front simply never call this.
+ */
+ setTotalCount(totalCount: number) {
+ this.totalCount = totalCount;
+ }
+
increaseProgressCount() {
this.progressCount++;
@@ -50,7 +61,8 @@ class TaskContext<T extends TaskType> {
taskId: this.taskId,
taskType: this.taskType,
data: this.data,
- progressCount: this.progressCount
+ progressCount: this.progressCount,
+ ...(this.totalCount !== null ? { totalCount: this.totalCount } : {})
} as WebSocketMessage);
}
}