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
feat(client): state whether the browser will keep the database
Elian Doran07aaf5ef8f12
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 07e4099479..380e07a64e 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1806,7 +1806,12 @@"start_over_pending": "The setup screen will appear the next time this server is restarted. Restart the server when you are ready, or cancel below to leave everything as it is.","start_over_cancel": "Cancel start over","related_backup": "Backup",- "related_backup_description": "Create or restore database backups, manage automatic backups."+ "related_backup_description": "Create or restore database backups, manage automatic backups.",+ "info_persistence": "Persistence",+ "info_persistence_persistent": "Persistent",+ "info_persistence_best_effort": "Best-effort",+ "info_persistence_persistent_description": "The browser has undertaken to keep this database until you delete it.",+ "info_persistence_best_effort_description": "The browser can evict this database to reclaim space.\nInstalling Trilium as an app or adding it to your home screen usually earns persistence."},"vacuum_database": {"title": "Vacuum Database",diff --git a/apps/client/src/widgets/react/Card.css b/apps/client/src/widgets/react/Card.cssindex 2c18a06cef..83f4fb8ab6 100644--- a/apps/client/src/widgets/react/Card.css+++ b/apps/client/src/widgets/react/Card.css@@ -84,6 +84,7 @@.tn-card-option-description {font-size: .85em;opacity: .7;+ white-space: pre-line;}/* A fact rather than a control: unlike the buttons a row is built around, a value gives waydiff --git a/apps/client/src/widgets/type_widgets/options/database.css b/apps/client/src/widgets/type_widgets/options/database.cssindex a44b60b20c..966460a41f 100644--- a/apps/client/src/widgets/type_widgets/options/database.css+++ b/apps/client/src/widgets/type_widgets/options/database.css@@ -1,3 +1,14 @@+.database-info .database-persistence {+ /* A card option's value gives way once the description beside it has to wrap, and breaks+ wherever it must: at 55px "Best-effort" loses its second half to a line of its own, where it+ reads as a word in its own right. This value is one short word at either setting, so it keeps+ its width and the sentence wraps instead. */+ .tn-card-option-value {+ flex-shrink: 0;+ white-space: nowrap;+ }+}+/* The notice sits above the cards it precedes, so the gap belongs under it. */.admonition.start-over-pending {margin-bottom: 16px;diff --git a/apps/client/src/widgets/type_widgets/options/database.spec.tsx b/apps/client/src/widgets/type_widgets/options/database.spec.tsxindex 87c8691194..7eb02eb66b 100644--- a/apps/client/src/widgets/type_widgets/options/database.spec.tsx+++ b/apps/client/src/widgets/type_widgets/options/database.spec.tsx@@ -125,6 +125,11 @@ function renderPage() {return container;}+/** What the browser reports about its own storage, which only the browser build reads. */+function setStorageManager(storage: { persisted?: () => Promise<boolean> } | undefined) {+ Object.defineProperty(navigator, "storage", { value: storage, configurable: true });+}+/** By name rather than by label: one label is a prefix of the other. */function button(name: string): HTMLButtonElement | null {return container.querySelector<HTMLButtonElement>(`button[name='${name}']`);@@ -139,6 +144,7 @@ beforeEach(() => {failing.info = false;INFO = DATABASE_INFO;standalone.enabled = false;+ setStorageManager(undefined);BACKUPS = [ { mtime: new Date("2026-01-01T10:00:00Z") }, { mtime: new Date("2026-01-02T10:00:00Z") } ];ANONYMIZED = [ ANONYMIZED_COPY ];dialog.closeActiveDialog.mockClear();@@ -217,6 +223,56 @@ describe("what the database is", () => {expect(server.get).not.toHaveBeenCalledWith("database/anonymized-databases");});+ it("states whether the browser has undertaken to keep the storage", async () => {+ standalone.enabled = true;+ INFO = { ...DATABASE_INFO, filePath: null };+ setStorageManager({ persisted: async () => true });+ renderPage();+ await settle();++ const [ , persistence ] = infoValues();+ expect(persistence).toBe("database.info_persistence_persistent");+ expect(container.querySelector(".database-info")?.textContent)+ .toContain("database.info_persistence_persistent_description");+ });++ it("says what follows from storage the browser can evict", async () => {+ standalone.enabled = true;+ INFO = { ...DATABASE_INFO, filePath: null };+ setStorageManager({ persisted: async () => false });+ renderPage();+ await settle();++ expect(infoValues()[1]).toBe("database.info_persistence_best_effort");+ // The row is named so its CSS can keep "Best-effort" off a second line, where the half of+ // it left above would read as a word of its own.+ expect(container.querySelector(".database-info .database-persistence")).not.toBeNull();+ // The grant is the browser's own decision and no button here can overrule it, so the row+ // says what earns it instead.+ expect(container.querySelector(".database-info")?.textContent)+ .toContain("database.info_persistence_best_effort_description");+ });++ it("leaves out the persistence row where the browser does not answer for the storage", async () => {+ // A database on disk stays there whatever the browser thinks, and a browser old enough to+ // lack `navigator.storage` has nothing to report.+ setStorageManager({ persisted: async () => true });+ renderPage();+ await settle();+ expect(container.querySelector(".database-info")?.textContent)+ .not.toContain("database.info_persistence");++ render(null, container);+ container.remove();+ standalone.enabled = true;+ INFO = { ...DATABASE_INFO, filePath: null };+ setStorageManager(undefined);+ renderPage();+ await settle();+ expect(container.querySelector(".database-info")?.textContent)+ .not.toContain("database.info_persistence");+ });+it("says a database has never been backed up rather than leaving the row blank", async () => {BACKUPS = [];renderPage();diff --git a/apps/client/src/widgets/type_widgets/options/database.tsx b/apps/client/src/widgets/type_widgets/options/database.tsxindex 6887aa8623..1a88392843 100644--- a/apps/client/src/widgets/type_widgets/options/database.tsx+++ b/apps/client/src/widgets/type_widgets/options/database.tsx@@ -121,6 +121,10 @@ function DatabaseInfo({ refreshToken }: { refreshToken: number }) {</span></OptionCardSection>+ {/* Follows the storage it qualifies, and only where that storage is the browser's:+ a database on disk stays there whatever the browser thinks of it. */}+ {isStandalone && <StoragePersistence />}+<OptionCardSection label={t("database.info_content")}><span className="tn-card-option-value">{t("database.info_notes", { count: info.noteCount })}@@ -146,6 +150,49 @@ function DatabaseInfo({ refreshToken }: { refreshToken: number }) {);}+/**+ * Whether the browser has undertaken to keep the storage the database lives in. Standalone asks for+ * that undertaking at every startup, and the browser decides from its own heuristics — chiefly how+ * installed the site looks — so this states the answer and what follows from it.+ *+ * Read from the page rather than from `database/info`, which is answered by core: `persisted()` is+ * the browser's own reading of its own storage, and there is nothing in the database to base it on.+ * Nothing is shown where the browser does not report it, which is every build whose database is a+ * file on disk.+ */+function StoragePersistence() {+ const [ persisted, setPersisted ] = useState<boolean | null>(null);++ useEffect(() => {+ let current = true;+ navigator.storage?.persisted?.()+ .then((result) => { if (current) setPersisted(result); })+ .catch(() => {});++ return () => { current = false; };+ }, []);++ if (persisted === null) {+ return null;+ }++ return (+ <OptionCardSection+ className="database-persistence"+ label={t("database.info_persistence")}+ description={persisted+ ? t("database.info_persistence_persistent_description")+ : t("database.info_persistence_best_effort_description")}+ >+ <span className="tn-card-option-value">+ {persisted+ ? t("database.info_persistence_persistent")+ : t("database.info_persistence_best_effort")}+ </span>+ </OptionCardSection>+ );+}+/*** How the backups stand, and the way to the page that acts on them.*