From 854410006188a7c7e4d530b8e8f61c700ad893b8 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Thu, 15 Oct 2026 11:31:13 +0000 Subject: [PATCH] feat(notes): a menu on each note in the tree to add inside, rename, move and delete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each row of the notes tree has a ⋯ menu (always showing on a touch screen): add a note inside, rename in place (Enter saves, Escape leaves it), Move to… (find the note to put it in by name, never itself or a note inside it), Move to the top, and Delete with Undo. The navigation links and the tree scroll together, so a long tree has the whole height. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01RrY2byt5BHVeBXJUyfwWpb --- CHANGELOG.md | 4 + docs/notes.md | 13 + nextjs-frontend/__tests__/notes-tree.test.tsx | 122 +++++++++ nextjs-frontend/components/app-shell/nav.tsx | 103 ++++---- .../components/notes/notes-tree.tsx | 242 +++++++++++++++--- 5 files changed, 401 insertions(+), 83 deletions(-) create mode 100644 nextjs-frontend/__tests__/notes-tree.test.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index da26ad248..595e19448 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ The backend (`fastapi_backend`) and the frontend (`nextjs-frontend`) are version ## Unreleased +- **Notes tree menu.** Each note in the tree in the left navigation has a ⋯ menu to add a note inside, rename it in + place, move it (find the note to put it in by name, or move it to the top) and delete it with Undo, so nothing needs + dragging and it works on a phone. The navigation links and the tree now scroll together, so a long tree has room + ([The tree](docs/notes.md#the-tree)). - **Delete a note with Undo.** Deleting a note no longer asks first: the notice has Undo, which puts it back where it was. Deleted notes wait in Recently deleted, at the bottom of the Notes page, for 30 days with their files and history, to put back with one click or delete for good; then they go on their own diff --git a/docs/notes.md b/docs/notes.md index 1a70b581c..fce3f2a56 100644 --- a/docs/notes.md +++ b/docs/notes.md @@ -122,6 +122,19 @@ refining it, or someone else always starts a new one. **History** on a page list it; pick one to read it, and editors can restore it. Restoring keeps the page as it was too, so it can be undone. `GET /api/v1/notes/{id}/history`, `GET .../history/{version}` and `POST .../history/{version}/restore`. +## The tree + +Free notes sit in a tree in the left navigation, under the main links. The links and the tree scroll together, so a +long tree has the whole height. A row opens its note, its arrow shows the notes inside it (the tree remembers which are +open) and **+** adds a note inside. Each row's **⋯** menu, which always shows on a touch screen, adds a note inside, +renames the note in place (Enter saves, Escape leaves it as it was), moves it (**Move to…** finds the note to put it in +by name, Enter picks the first; **Move to the top**) and deletes it with Undo. Dragging a note onto another puts it +inside, and onto the Notes heading moves it to the top. The pages of recordings and entities aren't in the tree: they +open from those things. + +`POST /api/v1/notes/{pid}/move` with `{parent}` moves a note (`null` for the top); `PATCH /api/v1/notes/{pid}` with +`{title}` renames it. + ## Deleting and putting back **Delete** on a page deletes it at once, with no question first: the notice has **Undo**, which puts it back and opens diff --git a/nextjs-frontend/__tests__/notes-tree.test.tsx b/nextjs-frontend/__tests__/notes-tree.test.tsx new file mode 100644 index 000000000..33b5bcfd4 --- /dev/null +++ b/nextjs-frontend/__tests__/notes-tree.test.tsx @@ -0,0 +1,122 @@ +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; +import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; +import "@testing-library/jest-dom"; + +import { Notes } from "@/app/openapi-client"; +import { NotesTree } from "@/components/notes/notes-tree"; + +jest.mock("@/app/openapi-client", () => ({ + Notes: { + listPages: jest.fn(), + createPage: jest.fn(), + movePage: jest.fn(), + updatePage: jest.fn(), + deletePage: jest.fn(), + restoreDeleted: jest.fn(), + }, +})); +jest.mock("next-auth/react", () => ({ useSession: () => ({ data: { accessToken: "t" } }) })); +const push = jest.fn(); +let pathname = "/notes"; +jest.mock("next/navigation", () => ({ usePathname: () => pathname, useRouter: () => ({ push }) })); +const toast = jest.fn(); +jest.mock("@/components/ui/toast", () => ({ useToast: () => toast })); +jest.mock("@/lib/hooks/session", () => ({ + useArchive: () => ({ namespaces: [{ name: "home" }], namespace: "home", can: () => true }), +})); + +const ok = (data: unknown) => Promise.resolve({ data, response: { ok: true, status: 200 } }); +const m = (f: unknown) => f as jest.Mock; +const show = () => + render( + + + , + ); + +const PAGES = [ + { id: 1, title: "Home lab", author: "person" }, + { id: 2, title: "Pi 5 shopping list", author: "person", parent: 1 }, + { id: 3, title: "NVMe hat options", author: "person", parent: 2 }, + { id: 4, title: "Trips", author: "person" }, +]; + +/** Open a row's ⋯ menu and choose an item (items run once the menu has closed). */ +async function choose(row: string, item: string) { + fireEvent.keyDown(await screen.findByRole("button", { name: `More for ${row}` }), { key: "Enter" }); + fireEvent.click(await screen.findByRole("menuitem", { name: item })); +} + +beforeAll(() => { + Element.prototype.scrollIntoView = jest.fn(); +}); +beforeEach(() => { + jest.clearAllMocks(); + localStorage.clear(); + pathname = "/notes"; + m(Notes.listPages).mockImplementation(() => ok({ namespace: "home", pages: PAGES })); +}); + +describe("the notes tree", () => { + it("renames a note in place: Enter saves, Escape leaves it as it was", async () => { + m(Notes.updatePage).mockImplementation(() => ok({ id: 4, title: "Holidays" })); + show(); + await choose("Trips", "Rename"); + const box = await screen.findByRole("textbox", { name: "New name for Trips" }); + expect(box).toHaveValue("Trips"); + fireEvent.change(box, { target: { value: "Holidays" } }); + fireEvent.keyDown(box, { key: "Enter" }); + await waitFor(() => expect(Notes.updatePage).toHaveBeenCalled()); + expect(m(Notes.updatePage).mock.calls[0][0]).toMatchObject({ path: { pid: 4 }, body: { title: "Holidays" } }); + + await choose("Home lab", "Rename"); + const again = await screen.findByRole("textbox", { name: "New name for Home lab" }); + fireEvent.change(again, { target: { value: "Lab" } }); + fireEvent.keyDown(again, { key: "Escape" }); + expect(screen.queryByRole("textbox")).not.toBeInTheDocument(); + expect(Notes.updatePage).toHaveBeenCalledTimes(1); + }); + + it("moves a note into another found by name, never into itself or a note inside it", async () => { + m(Notes.movePage).mockImplementation(() => ok({ ok: true })); + show(); + await choose("Home lab", "Move to…"); + const dialog = await screen.findByRole("dialog", { name: "Move “Home lab”" }); + // Home lab is at the top already, and Pi 5 shopping list is inside it + expect( + within(dialog) + .getAllByRole("option") + .map((o) => o.textContent), + ).toEqual(["Trips"]); + fireEvent.keyDown(within(dialog).getByRole("combobox", { name: "Move into" }), { key: "Enter" }); + await waitFor(() => expect(Notes.movePage).toHaveBeenCalled()); + expect(m(Notes.movePage).mock.calls[0][0]).toMatchObject({ path: { pid: 1 }, body: { parent: 4 } }); + }); + + it("moves a nested note to the top with one pick", async () => { + localStorage.setItem("lens.notes.open", "[1]"); + m(Notes.movePage).mockImplementation(() => ok({ ok: true })); + show(); + await choose("Pi 5 shopping list", "Move to the top"); + await waitFor(() => expect(Notes.movePage).toHaveBeenCalled()); + expect(m(Notes.movePage).mock.calls[0][0]).toMatchObject({ path: { pid: 2 }, body: { parent: null } }); + }); + + it("adds a note inside another from the menu and opens it", async () => { + m(Notes.createPage).mockImplementation(() => ok({ id: 9, title: "Untitled" })); + show(); + await choose("Trips", "Add a note inside"); + await waitFor(() => expect(push).toHaveBeenCalledWith("/notes/9?new=1")); + expect(m(Notes.createPage).mock.calls[0][0].body).toMatchObject({ ns: "home", parent: 4 }); + }); + + it("deletes a note with Undo on the notice, and leaves it when it was open", async () => { + pathname = "/notes/4"; + m(Notes.deletePage).mockImplementation(() => ok({ ok: true })); + show(); + await choose("Trips", "Delete"); + await waitFor(() => expect(push).toHaveBeenCalledWith("/notes")); + expect(m(Notes.deletePage).mock.calls[0][0].path).toEqual({ pid: 4 }); + expect(toast.mock.calls[0][0]).toMatchObject({ title: "Deleted “Trips”", action: { label: "Undo" } }); + }); +}); diff --git a/nextjs-frontend/components/app-shell/nav.tsx b/nextjs-frontend/components/app-shell/nav.tsx index a8109ad5d..f77f93bbc 100644 --- a/nextjs-frontend/components/app-shell/nav.tsx +++ b/nextjs-frontend/components/app-shell/nav.tsx @@ -45,60 +45,57 @@ export function Nav({ {top} - {navFor(admin).map((it, i, all) => { - // a group's name goes above its first item; collapsed to icons, a line does - const opens = it.group && it.group !== all[i - 1]?.group; - const on = isActive(pathname, it.href); - const n = it.countKey ? counts[it.countKey] : undefined; - const link = ( - - - {collapsed ? ( - {it.label} - ) : ( - <> - {it.label} - {n ? {count(n)} : null} - - )} - - ); - return ( -
- {opens && - (collapsed ? ( -
+ {/* the links and the notes tree scroll together, so a long tree has the whole height; Collapse stays put */} +
+ {navFor(admin).map((it, i, all) => { + // a group's name goes above its first item; collapsed to icons, a line does + const opens = it.group && it.group !== all[i - 1]?.group; + const on = isActive(pathname, it.href); + const n = it.countKey ? counts[it.countKey] : undefined; + const link = ( + + + {collapsed ? ( + {it.label} ) : ( -
- {it.group} -
- ))} - {collapsed ? ( - - {link} - - ) : ( - link - )} -
- ); - })} - {collapsed ? ( -
- ) : ( -
- -
- )} + <> + {it.label} + {n ? {count(n)} : null} + + )} + + ); + return ( +
+ {opens && + (collapsed ? ( +
+ ) : ( +
+ {it.group} +
+ ))} + {collapsed ? ( + + {link} + + ) : ( + link + )} +
+ ); + })} + {!collapsed && } +
{onToggle && ( - - {page.author === "assistant" ? ( - - ) : ( - - )} - {page.title} - - {canEdit && ( - + {page.author === "assistant" ? ( + + ) : ( + + )} + {page.title} + + )} + {canEdit && renaming !== page.id && ( + <> + + + + + + } onSelect={() => actions.add(page.id)}> + Add a note inside + + } onSelect={() => actions.startRename(page.id)}> + Rename + + } onSelect={() => actions.startMove(page)}> + Move to… + + {page.parent != null && ( + } onSelect={() => actions.moveTo(page.id, null)}> + Move to the top + + )} + + } danger onSelect={() => actions.remove(page)}> + Delete + + + + + )}
{expanded && @@ -271,10 +448,15 @@ function Node({ allow={allow} drop={drop} onLeave={onLeave} - onAdd={onAdd} + actions={actions} + renaming={renaming} onNavigate={onNavigate} /> ))}
); } + +// A row's buttons show on hover or focus with a mouse, and always on a touch screen (no hover there) +const rowButton = + "grid size-5 shrink-0 place-items-center rounded-sm text-fg-muted opacity-0 hover:bg-surface-neutral hover:text-fg-strong focus:opacity-100 group-hover:opacity-100 data-[state=open]:opacity-100 [@media(hover:none)]:opacity-100";