Skip to content

Build your personal knowledge base with Trilium Notes

feat(onenote): add a refresh button to the section picker

Elian Dorana087a54a4a63
diff --git a/apps/client/src/services/onenote_import.spec.ts b/apps/client/src/services/onenote_import.spec.ts
index 97a7c73ce3..56e7ef5638 100644
--- a/apps/client/src/services/onenote_import.spec.ts
+++ b/apps/client/src/services/onenote_import.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { buildSectionSelections, extractServerMessage, type OneNoteNotebook, orderedChildren } from "./onenote_import.js";
+import { buildSectionSelections, collectSectionIds, extractServerMessage, type OneNoteNotebook, orderedChildren } from "./onenote_import.js";
const NOTEBOOKS: OneNoteNotebook[] = [
{
@@ -111,3 +111,12 @@ describe("extractServerMessage", () => {
expect(extractServerMessage(undefined)).toBeNull();
});
});
+
+describe("collectSectionIds", () => {
+ it("collects every section id across all notebooks, including those nested in section groups", () => {
+ // s1 is a direct section, s2 lives in a group, s3 in a nested subgroup — all must be found;
+ // group ids themselves must not be (groups are structure, never selectable).
+ expect(collectSectionIds(NOTEBOOKS)).toEqual(new Set(["s1", "s2", "s3"]));
+ expect(collectSectionIds([])).toEqual(new Set());
+ });
+});
diff --git a/apps/client/src/services/onenote_import.ts b/apps/client/src/services/onenote_import.ts
index 6a107d4057..a99656b1d4 100644
--- a/apps/client/src/services/onenote_import.ts
+++ b/apps/client/src/services/onenote_import.ts
@@ -127,6 +127,21 @@ export function buildSectionSelections(notebooks: OneNoteNotebook[], selectedIds
return selections;
}
+/**
+ * Collects the ids of every section anywhere in the given notebooks, including those nested inside
+ * section groups. Used to prune a selection after the notebook list is refreshed, so ids of sections
+ * that no longer exist can't linger in the selection (and inflate the import button's count).
+ */
+export function collectSectionIds(containers: OneNoteContainer[], into = new Set<string>()): Set<string> {
+ for (const container of containers) {
+ for (const section of container.sections) {
+ into.add(section.id);
+ }
+ collectSectionIds(container.sectionGroups, into);
+ }
+ return into;
+}
+
/**
* Returns a container's direct children — its sections and section groups — interleaved into a single
* list ordered by creation date. OneNote shows sections and groups intermixed in one ordered rail but
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 1abe0bb243..72259da2a4 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -270,6 +270,7 @@
"device_cancel": "Cancel sign-in",
"connected_as": "Connected as {{name}}",
"disconnect": "Disconnect",
+ "refresh": "Refresh",
"load_failed": "Could not load your OneNote notebooks.",
"load_retry": "Try again",
"no_notebooks": "No notebooks were found in this account.",
diff --git a/apps/client/src/widgets/dialogs/import/import_dialog.css b/apps/client/src/widgets/dialogs/import/import_dialog.css
index ef892e4341..937adf1469 100644
--- a/apps/client/src/widgets/dialogs/import/import_dialog.css
+++ b/apps/client/src/widgets/dialogs/import/import_dialog.css
@@ -56,6 +56,13 @@
border-bottom: 1px solid var(--main-border-color);
}
+ /* Refresh + Disconnect stay grouped on the row's right edge. */
+ .onenote-account-actions {
+ display: flex;
+ align-items: center;
+ gap: 0.5em;
+ }
+
.onenote-status {
display: flex;
align-items: center;
diff --git a/apps/client/src/widgets/dialogs/import/onenote.tsx b/apps/client/src/widgets/dialogs/import/onenote.tsx
index 9093e99bf1..773a3ce5cf 100644
--- a/apps/client/src/widgets/dialogs/import/onenote.tsx
+++ b/apps/client/src/widgets/dialogs/import/onenote.tsx
@@ -1,7 +1,7 @@
import { useCallback, useEffect, useMemo, useRef, useState } from "preact/hooks";
import { t } from "../../../services/i18n.js";
-import onenoteImport, { buildSectionSelections, type OneNoteAccount, type OneNoteContainer, type OneNoteDeviceLogin, type OneNoteNotebook, orderedChildren } from "../../../services/onenote_import.js";
+import onenoteImport, { buildSectionSelections, collectSectionIds, type OneNoteAccount, type OneNoteContainer, type OneNoteDeviceLogin, type OneNoteNotebook, orderedChildren } from "../../../services/onenote_import.js";
import toast from "../../../services/toast.js";
import { isElectron, randomString } from "../../../services/utils.js";
import Button from "../../react/Button.js";
@@ -21,6 +21,8 @@ function OneNotePanel({ parentNoteId, closeDialog, setFooter }: ImportProviderPa
const [account, setAccount] = useState<OneNoteAccount | null>(null);
const [notebooks, setNotebooks] = useState<OneNoteNotebook[]>([]);
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
+ // Set while a user-triggered notebook reload is in flight, to spin and disable the refresh button.
+ const [refreshing, setRefreshing] = useState(false);
const [debug, setDebug] = useState(false);
const [compressImages] = useTriliumOptionBool("compressImages");
const [shrinkImages, setShrinkImages] = useState(compressImages);
@@ -43,11 +45,26 @@ function OneNotePanel({ parentNoteId, closeDialog, setFooter }: ImportProviderPa
try {
const { notebooks } = await onenoteImport.getNotebooks();
setNotebooks(notebooks);
+ // Drop selected ids whose section no longer exists (deleted/moved in OneNote since the
+ // last load), so a refresh can't leave the import button counting phantom sections.
+ const validIds = collectSectionIds(notebooks);
+ setSelectedIds((prev) => new Set([...prev].filter((id) => validIds.has(id))));
} catch {
toast.showError(t("onenote_import.load_failed"));
}
}, []);
+ // User-triggered reload of the notebook list, e.g. after creating a section in OneNote while the
+ // dialog is open. Wraps loadNotebooks purely to drive the refresh button's spinner/disabled state.
+ const refresh = useCallback(async () => {
+ setRefreshing(true);
+ try {
+ await loadNotebooks();
+ } finally {
+ setRefreshing(false);
+ }
+ }, [loadNotebooks]);
+
// On mount, see whether this session is already connected.
useEffect(() => {
void onenoteImport.getStatus().then(async (status) => {
@@ -246,7 +263,17 @@ function OneNotePanel({ parentNoteId, closeDialog, setFooter }: ImportProviderPa
<CardSection className="onenote-panel">
<div className="onenote-account">
<span>{t("onenote_import.connected_as", { name: account?.name ?? "" })}</span>
- <Button text={t("onenote_import.disconnect")} kind="lowProfile" size="small" onClick={disconnect} />
+ <div className="onenote-account-actions">
+ <Button
+ text={t("onenote_import.refresh")}
+ kind="lowProfile"
+ size="small"
+ icon={refreshing ? "bx-refresh bx-spin" : "bx-refresh"}
+ disabled={refreshing}
+ onClick={() => void refresh()}
+ />
+ <Button text={t("onenote_import.disconnect")} kind="lowProfile" size="small" onClick={disconnect} />
+ </div>
</div>
{notebooks.length === 0