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
client/deleted notes dialog: fix some issues
Adorian Doranb1b444359049
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 5c356f9ab1..f85b6b101b 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -429,6 +429,7 @@"undelete_link": "undelete","confirm_undelete": "Do you want to undelete this note and its sub-notes?","confirm_undelete_to_default_location": "The parent of this note no longer exists, so it can't be restored to its original location. Restore it to the default location for new notes instead?",+ "undeleted_to_default_location": "Its original location no longer exists, so the note was restored to \"{{- title}}\".","undelete_failed": "The note could not be restored — it may have been permanently erased."},"revisions": {diff --git a/apps/client/src/utils/formatters.spec.ts b/apps/client/src/utils/formatters.spec.tsindex 78aea3db84..72417f4b6e 100644--- a/apps/client/src/utils/formatters.spec.ts+++ b/apps/client/src/utils/formatters.spec.ts@@ -118,6 +118,18 @@ describe("formatters", () => {it("falls back to days when the scale is missing or unusable", () => {expect(formatDuration(604800, 0)).toBe("time_interval.days|7");expect(formatDuration(604800, -1)).toBe("time_interval.days|7");+ // useTriliumOptionInt yields NaN for an option that hasn't loaded; the scale still degrades+ // to days rather than producing a NaN count.+ expect(formatDuration(604800, NaN)).toBe("time_interval.days|7");+ });++ it("returns null when the duration itself is unknown, so callers omit the phrase", () => {+ // Options load asynchronously, so useTriliumOptionInt yields NaN until the fetch resolves.+ // Returning null keeps "NaN days" (or a gap mid-sentence) out of the UI.+ expect(formatDuration(NaN, 86400)).toBeNull();+ expect(formatDuration(undefined as unknown as number, 86400)).toBeNull();+ expect(formatDuration(Infinity, 86400)).toBeNull();+ expect(formatDuration(-1, 86400)).toBeNull();});it("reports an unrecognized scale in days derived from the raw seconds", () => {diff --git a/apps/client/src/utils/formatters.ts b/apps/client/src/utils/formatters.tsindex ebe79b163f..bb8677516c 100644--- a/apps/client/src/utils/formatters.ts+++ b/apps/client/src/utils/formatters.ts@@ -17,8 +17,17 @@ const SECONDS_PER_DAY = 86400;* raw seconds, so it always matches what the settings page shows. A fuzzy humanizer would instead* collapse distinct windows (both 30 and 45 days become "a month") — misleading for a setting that* governs when data is destroyed.+ *+ * @returns `null` when the duration is unknown. Options load asynchronously, so `useTriliumOptionInt`+ * yields `NaN` until the fetch resolves; returning `null` (rather than a string) makes callers+ * drop the whole phrase instead of rendering "NaN days" — or an empty gap — inside a sentence.*/-export function formatDuration(seconds: number, timeScale: number) {+export function formatDuration(seconds: number, timeScale: number): string | null {+ if (!Number.isFinite(seconds) || seconds < 0) {+ return null;+ }++ // A NaN/absent scale fails this comparison and falls back to days.const scale = timeScale > 0 ? timeScale : SECONDS_PER_DAY;const count = Math.round(seconds / scale);diff --git a/apps/client/src/widgets/dialogs/recent_changes.tsx b/apps/client/src/widgets/dialogs/recent_changes.tsxindex 894194e227..79bdbd5aa8 100644--- a/apps/client/src/widgets/dialogs/recent_changes.tsx+++ b/apps/client/src/widgets/dialogs/recent_changes.tsx@@ -1,5 +1,6 @@import { Dispatch, StateUpdater, useEffect, useState } from "preact/hooks";import appContext from "../../components/app_context";+import type FNote from "../../entities/fnote";import dateNoteService from "../../services/date_notes";import dialog from "../../services/dialog";import { t } from "../../services/i18n";@@ -48,9 +49,24 @@ export default function RecentChangesDialog() {return;}- froca.getNote(ancestorNoteId, true).then((note) => setAncestorTitle(note?.title));+ // Reopening the dialog for another subtree while this lookup is in flight would otherwise let+ // the stale title win, so ignore a resolution that arrives after the ancestor changed.+ let active = true;++ froca.getNote(ancestorNoteId, true).then((note) => {+ if (active) {+ setAncestorTitle(note?.title);+ }+ });++ return () => {+ active = false;+ };}, [ ancestorNoteId ]);+ // `ancestorNoteId` is a dependency: the dialog can already be open when it is re-triggered for a+ // different subtree (e.g. from the tree context menu), and without it the list would keep showing+ // the previous ancestor's results under the newly updated heading.useEffect(() => {if (!ancestorNoteId) return;server.get<RecentChangeRow[]>(`recent-changes/${ancestorNoteId}?deletedOnly=${deletedOnly}`)@@ -64,9 +80,12 @@ export default function RecentChangesDialog() {const groupedByDate = groupByDate(recentChanges);setGroupedByDate(groupedByDate);});- }, [ shown, refreshCounter, deletedOnly ])+ }, [ shown, refreshCounter, deletedOnly, ancestorNoteId ])const baseTitle = deletedOnly ? t("recent_changes.deleted_notes_title") : t("recent_changes.title");+ // Null until the options have loaded, in which case the retention hint is omitted rather than+ // stating an unknown window.+ const erasePeriod = formatDuration(eraseAfterSeconds, eraseTimeScale);return (<Modal@@ -110,7 +129,7 @@ export default function RecentChangesDialog() {? <RecentChangesTimeline groupedByDate={groupedByDate} setShown={setShown} />: deletedOnly? <NoItems icon="bx bx-trash-alt" text={t("recent_changes.no_deleted_notes_message")}>- <small>{t("recent_changes.no_deleted_notes_erasure_hint", { duration: formatDuration(eraseAfterSeconds, eraseTimeScale) })}</small>+ {erasePeriod && <small>{t("recent_changes.no_deleted_notes_erasure_hint", { duration: erasePeriod })}</small>}</NoItems>: <NoItems icon="bx bx-history" text={t("recent_changes.no_changes_message")} />}</div>@@ -194,27 +213,34 @@ async function undeleteNote(change: RecentChangeRow, setShown: Dispatch<StateUpdreturn;}- let fallbackParentNoteId: string | undefined;+ let fallbackParent: FNote | null = null;if (!hasOriginalLocation) {- const inboxNote = await dateNoteService.getInboxNote();+ fallbackParent = await dateNoteService.getInboxNote();- if (!inboxNote) {+ if (!fallbackParent) {toast.showError(t("recent_changes.undelete_failed"));return;}-- fallbackParentNoteId = inboxNote.noteId;}// The note may still fail to restore if it was erased since the dialog opened, so act on the reported result.- const { undeleted } = await server.put<UndeleteResponse>(`notes/${change.noteId}/undelete`, { fallbackParentNoteId });+ const { undeleted, restoredToFallbackParent } = await server.put<UndeleteResponse>(+ `notes/${change.noteId}/undelete`,+ { fallbackParentNoteId: fallbackParent?.noteId }+ );if (!undeleted) {toast.showError(t("recent_changes.undelete_failed"));return;}+ if (restoredToFallbackParent && fallbackParent) {+ // The note could not go back where it was, so name where it actually landed — the user only+ // saw a prompt about the fallback, never a confirmation of it.+ toast.showMessage(t("recent_changes.undeleted_to_default_location", { title: fallbackParent.title }));+ }+setShown(false);await ws.waitForMaxKnownEntityChangeId();appContext.tabManager.getActiveContext()?.setNote(change.noteId);