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(autocomplete): group the recent notes by when they were visited
Elian Doran932a8fe00ada
diff --git a/apps/client/src/services/note_autocomplete.spec.ts b/apps/client/src/services/note_autocomplete.spec.tsindex 5ffc04f5c2..6cd5af5b75 100644--- a/apps/client/src/services/note_autocomplete.spec.ts+++ b/apps/client/src/services/note_autocomplete.spec.ts@@ -54,7 +54,7 @@ vi.mock("./ws.js", () => ({}));import type { CommandDefinition } from "./command_registry.js";-import noteAutocomplete, { createNoteFromSuggestion, createSearchScheduler, getCommandSuggestions, getNoteSuggestions, type Suggestion } from "./note_autocomplete.js";+import noteAutocomplete, { createNoteFromSuggestion, createSearchScheduler, getCommandSuggestions, getNoteSuggestions, recentNoteGroup, type Suggestion } from "./note_autocomplete.js";import server from "./server.js";beforeEach(() => {@@ -358,3 +358,22 @@ describe("createNoteFromSuggestion", () => {expect(await createNoteFromSuggestion({ action: "create-note", noteTitle: "X" })).toBeUndefined();});});++describe("recentNoteGroup", () => {+ // In the local time zone, as the groups are local days.+ const now = new Date(2026, 9, 15, 10, 30);+ const visited = (year: number, month: number, day: number, hours = 12) =>+ new Date(year, month, day, hours).toISOString().replace("T", " ");++ it("groups a visit by the local day it falls on, as far back as a month", () => {+ expect(recentNoteGroup(visited(2026, 9, 15, 0), now)).toBe("today");+ expect(recentNoteGroup(visited(2026, 9, 14, 23), now)).toBe("yesterday");+ expect(recentNoteGroup(visited(2026, 9, 14, 0), now)).toBe("yesterday");+ expect(recentNoteGroup(visited(2026, 9, 13), now)).toBe("past-week");+ expect(recentNoteGroup(visited(2026, 9, 9), now)).toBe("past-week");+ expect(recentNoteGroup(visited(2026, 9, 8), now)).toBe("past-month");+ expect(recentNoteGroup(visited(2026, 8, 16), now)).toBe("past-month");+ expect(recentNoteGroup(visited(2026, 8, 15), now)).toBe("older");+ expect(recentNoteGroup(visited(2025, 0, 1), now)).toBe("older");+ });+});diff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.tsindex f8f9bb9d4c..c9b582e8dc 100644--- a/apps/client/src/services/note_autocomplete.ts+++ b/apps/client/src/services/note_autocomplete.ts@@ -155,6 +155,24 @@ export async function createNoteFromSuggestion(suggestion: Suggestion) {return note?.getBestNotePathString(hoistedNoteId);}+/** When a recent note was last visited, as the lists group the recent notes under headings. */+export type RecentNoteGroup = "today" | "yesterday" | "past-week" | "past-month" | "older";++/**+ * The group of a note visited at `utcDateVisited` (`YYYY-MM-DD HH:mm:ss.SSSZ`), counted in local+ * days back from `now`: today, yesterday, the 7 days that end today, the 30 days, and before them.+ */+export function recentNoteGroup(utcDateVisited: string, now: Date): RecentNoteGroup {+ const visited = new Date(utcDateVisited.replace(" ", "T"));+ const daysBack = (days: number) => new Date(now.getFullYear(), now.getMonth(), now.getDate() - days);++ if (visited >= daysBack(0)) return "today";+ if (visited >= daysBack(1)) return "yesterday";+ if (visited >= daysBack(6)) return "past-week";+ if (visited >= daysBack(29)) return "past-month";+ return "older";+}+/*** Returns the commands matching what follows the `>` that opens `term`, or every command when* nothing follows it.diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex e554ec35b6..5f3c396629 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3093,6 +3093,13 @@"title": "Note search results","move": "Move through the results","open": "Open the highlighted result"+ },+ "recent": {+ "today": "Today",+ "yesterday": "Yesterday",+ "past_week": "Past 7 days",+ "past_month": "Past 30 days",+ "older": "Older"}},"note_tooltip": {diff --git a/apps/client/src/widgets/react/NoteAutocomplete.css b/apps/client/src/widgets/react/NoteAutocomplete.cssindex d3649d9c06..69ad60c037 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.css+++ b/apps/client/src/widgets/react/NoteAutocomplete.css@@ -83,6 +83,21 @@}}+/*+ * The headings over the recent notes, by when they were visited, at the start of the rows. The+ * `!important` outranks the menu text color that style.css forces on `.dropdown-header`.+ */+.note-autocomplete-menu li > .dropdown-header {+ margin: 0;+ padding: 10px 8px 2px;+ font-size: 0.8em;+ color: var(--muted-text-color) !important;+}++.note-autocomplete-menu li:first-child > .dropdown-header {+ padding-top: 2px;+}+/** The lists in a host's container, such as the Empty tab's and Jump to Note's: the popup's rows,* outside a `.dropdown-menu`, which defines Bootstrap's item and divider variables.@@ -108,6 +123,10 @@margin-block: 8px;border-top: 1px solid var(--main-border-color);}++ > li > .dropdown-header {+ padding-inline: 16px;+ }}/* Under a host's list, outside its scroller, so it stays in view as the list scrolls. */diff --git a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsxindex 5aefa5360f..9260219aea 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.spec.tsx@@ -193,6 +193,33 @@ describe("NoteAutocomplete's suggestion list", () => {return allRows().find((row) => row.querySelector(".bx-search"));}+ it("groups the recent notes under headings by when they were visited, which the keys step over", async () => {+ const today = new Date();+ const daysAgo = (days: number) =>+ new Date(today.getFullYear(), today.getMonth(), today.getDate() - days, 0, 1).toISOString().replace("T", " ");+ getNoteSuggestions.mockResolvedValue([+ { ...notes[0], utcDateVisited: daysAgo(0) },+ { ...notes[1], utcDateVisited: daysAgo(0) },+ { notePath: "root/c", noteTitle: "Gamma", highlightedNotePathTitle: "Gamma", utcDateVisited: daysAgo(40) }+ ]);+ const input = await mount();+ await act(async () => { input.focus(); });+ await settle();++ const menu = document.querySelector(".note-autocomplete-menu .tn-menu-scroll");+ const layout = [ ...(menu?.children ?? []) ].map((child) =>+ child.querySelector(".dropdown-header") ? "heading" : child.className.split(" ")[0]);+ expect(layout).toEqual([ "heading", "dropdown-item", "dropdown-item", "heading", "dropdown-item" ]);+ expect(rows()[0].classList.contains("tn-menu-active")).toBe(true);+ await press(input, "ArrowUp");+ expect(rows()[2].classList.contains("tn-menu-active")).toBe(true);++ // A search's results carry no visit times, and no headings.+ getNoteSuggestions.mockResolvedValue(notes);+ await type(input, "al");+ expect(document.querySelector(".note-autocomplete-menu .dropdown-header")).toBeNull();+ });+it("lists the notes as the rows of a menu, the first one highlighted", async () => {const input = await mount();await type(input, "al");diff --git a/apps/client/src/widgets/react/NoteAutocomplete.tsx b/apps/client/src/widgets/react/NoteAutocomplete.tsxindex 162f7e3512..b592e417fd 100644--- a/apps/client/src/widgets/react/NoteAutocomplete.tsx+++ b/apps/client/src/widgets/react/NoteAutocomplete.tsx@@ -10,13 +10,13 @@ import { type MutableRef, useCallback, useEffect, useLayoutEffect, useMemo, useRimport appContext from "../../components/app_context";import froca from "../../services/froca";import { t } from "../../services/i18n";-import { createSearchScheduler, createNoteFromSuggestion, getCommandSuggestions, getNoteSuggestions, type Options, type Suggestion } from "../../services/note_autocomplete";+import { createSearchScheduler, createNoteFromSuggestion, getCommandSuggestions, getNoteSuggestions, type Options, recentNoteGroup, type RecentNoteGroup, type Suggestion } from "../../services/note_autocomplete";import { escapeHtml, isMobile } from "../../services/utils";import { ShortcutHintOverlayButton } from "../shortcut_hints/shortcut_hint_button";import { AttributeSnippetBadges } from "./Badge";import Button from "./Button";import { useAutocomplete } from "./FormAutocomplete";-import { FormDropdownDivider } from "./FormList";+import { FormDropdownDivider, FormListHeader } from "./FormList";import FormToggle from "./FormToggle";import { useContextualShortcutHints, useSyncedRef } from "./hooks";import Icon from "./Icon";@@ -445,6 +445,11 @@ function NoteSuggestionMenu({ autocomplete, searchingFor, className }: {searchingFor?: string;className: string;}) {+ // Only the recent notes listed on an empty query carry a visit time, and take a heading per group.+ const now = new Date();+ const recentGroups = autocomplete.items.map((suggestion) =>+ suggestion.utcDateVisited ? recentNoteGroup(suggestion.utcDateVisited, now) : undefined);+return (<menuclassName={className}@@ -466,6 +471,9 @@ function NoteSuggestionMenu({ autocomplete, searchingFor, className }: {</>}{searchingFor === undefined && autocomplete.items.map((suggestion, index) => [startsGroup(autocomplete.items, index) && <FormDropdownDivider key={`divider-${index}`} />,+ recentGroups[index] && recentGroups[index] !== recentGroups[index - 1] && (+ <FormListHeader key={`heading-${index}`} text={recentGroupTitle(recentGroups[index])} />+ ),<likey={suggestionKey(suggestion, index)}id={autocomplete.itemId(index)}@@ -573,6 +581,16 @@ function suggestionKey(suggestion: Suggestion, index: number) {return `${suggestion.action ?? ""}:${suggestion.notePath ?? suggestion.commandId ?? index}`;}+function recentGroupTitle(group: RecentNoteGroup) {+ switch (group) {+ case "today": return t("note_autocomplete.recent.today");+ case "yesterday": return t("note_autocomplete.recent.yesterday");+ case "past-week": return t("note_autocomplete.recent.past_week");+ case "past-month": return t("note_autocomplete.recent.past_month");+ case "older": return t("note_autocomplete.recent.older");+ }+}+function suggestionIcon(suggestion: Suggestion) {switch (suggestion.action) {// The icon of the saved search note it opens.diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.htmlindex 8908c1b167..347d98be74 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.html@@ -65,6 +65,9 @@<p>To access this functionality, click on <code>Jump to</code> button on thetop. By default, (when nothing is entered into autocomplete), this dialogwill show the list of recent notes.</p>+<p>The recent notes are grouped by when they were last visited: <em>Today</em>, <em>Yesterday</em>, <em>Past 7 days</em>, <em>Past 30 days</em> and <em>Older</em>.+ The same list, with the same groups, opens in any note field that is focused+ while empty.</p><h2>Command Palette</h2><figure class="image image-style-align-center"><img style="aspect-ratio:982/524;" src="Jump to & command palette_image.png"diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.mdindex 1fe9c7f19e..399b9342f4 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Jump to & command palette.md@@ -31,6 +31,8 @@ Jump to note also has the ability to show the list of recently viewed / edited nTo access this functionality, click on `Jump to` button on the top. By default, (when nothing is entered into autocomplete), this dialog will show the list of recent notes.+The recent notes are grouped by when they were last visited: _Today_, _Yesterday_, _Past 7 days_, _Past 30 days_ and _Older_. The same list, with the same groups, opens in any note field that is focused while empty.+## Command Palette<figure class="image image-style-align-center"><img style="aspect-ratio:982/524;" src="Jump to & command palette_image.png" width="982" height="524"></figure>diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.tsindex 3376c942ae..7f13069882 100644--- a/packages/commons/src/lib/server_api.ts+++ b/packages/commons/src/lib/server_api.ts@@ -391,10 +391,14 @@ export interface SearchResultDetails {/*** One row of `GET /api/autocomplete`: a recently visited note when the query is empty, a search- * result otherwise. Only search results carry the attribute snippet.+ * result otherwise. Only search results carry the attribute snippet, and only recent notes the+ * time of the visit.*/export type AutocompleteResult = Pick<SearchResultDetails, "notePath" | "noteTitle" | "notePathTitle"- | "highlightedNotePathTitle" | "attributeSnippet" | "highlightedAttributeSnippet" | "icon">;+ | "highlightedNotePathTitle" | "attributeSnippet" | "highlightedAttributeSnippet" | "icon"> & {+ /** When the note was last visited, as `recent_notes.utcDateCreated` holds it. */+ utcDateVisited?: string;+};/** Response for `GET /api/quick-search/:searchString`. */export interface QuickSearchResponse {diff --git a/packages/trilium-core/src/routes/api/autocomplete.spec.ts b/packages/trilium-core/src/routes/api/autocomplete.spec.tsindex df9378c28c..90a2b7b361 100644--- a/packages/trilium-core/src/routes/api/autocomplete.spec.ts+++ b/packages/trilium-core/src/routes/api/autocomplete.spec.ts@@ -16,6 +16,7 @@ interface AutocompleteResult {notePathTitle: string;highlightedNotePathTitle: string;icon: string;+ utcDateVisited?: string;}describe("Autocomplete API (core)", () => {@@ -45,12 +46,22 @@ describe("Autocomplete API (core)", () => {expect(match?.icon).toBeTruthy();});- it("returns recent notes for an empty query", async () => {+ it("returns recent notes for an empty query, with when each was visited", async () => {+ const { noteId } = await createTextNote(api, { title: "Recently visited" });+ await api.post("/api/recent-notes", { body: { noteId, notePath: `root/${noteId}` } });+const res = await api.get<AutocompleteResult[]>("/api/autocomplete", {query: { query: "" }});expect(res.status).toBe(200);- expect(Array.isArray(res.body)).toBe(true);+ const visited = res.body.find((r) => r.notePath === `root/${noteId}`);+ expect(visited?.utcDateVisited).toMatch(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/);++ // A search answers with no visit times.+ const searched = await api.get<AutocompleteResult[]>("/api/autocomplete", {+ query: { query: "Recently visited" }+ });+ expect(searched.body.find((r) => r.notePath.endsWith(noteId))?.utcDateVisited).toBeUndefined();});it("filters recent notes by the hoisted note path when not hoisted to root", async () => {diff --git a/packages/trilium-core/src/routes/api/autocomplete.ts b/packages/trilium-core/src/routes/api/autocomplete.tsindex 2d533e501b..6bf1614f99 100644--- a/packages/trilium-core/src/routes/api/autocomplete.ts+++ b/packages/trilium-core/src/routes/api/autocomplete.ts@@ -76,7 +76,8 @@ function getRecentNotes(activeNoteId: string): AutocompleteResult[] {noteTitle: title,notePathTitle,highlightedNotePathTitle: escapeHtml(notePathTitle || title),- icon: icon ?? "bx bx-note"+ icon: icon ?? "bx bx-note",+ utcDateVisited: rn.utcDateCreated};});}