Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions docs/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
122 changes: 122 additions & 0 deletions nextjs-frontend/__tests__/notes-tree.test.tsx
Original file line number Diff line number Diff line change
@@ -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(
<QueryClientProvider client={new QueryClient({ defaultOptions: { queries: { retry: false } } })}>
<NotesTree />
</QueryClientProvider>,
);

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" } });
});
});
103 changes: 50 additions & 53 deletions nextjs-frontend/components/app-shell/nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,60 +45,57 @@ export function Nav({
<Brand collapsed={collapsed} />
</div>
{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 = (
<Link
href={it.href}
onClick={onNavigate}
aria-current={on ? "page" : undefined}
className={cn(
"flex h-8 items-center gap-[11px] whitespace-nowrap rounded-sm px-2.5 text-[14px] transition-colors duration-fast",
on ? "bg-blue-surface font-bold text-fg-accent" : "font-medium text-fg-strong hover:bg-surface-neutral",
collapsed && "justify-center px-0",
)}
>
<it.icon className="size-[17px] shrink-0" strokeWidth={2} aria-hidden />
{collapsed ? (
<span className="sr-only">{it.label}</span>
) : (
<>
<span className="flex-1">{it.label}</span>
{n ? <span className="tabular text-[11px] font-medium text-fg-muted">{count(n)}</span> : null}
</>
)}
</Link>
);
return (
<div key={it.href}>
{opens &&
(collapsed ? (
<div className="mx-1.5 my-2 h-px bg-border" />
{/* the links and the notes tree scroll together, so a long tree has the whole height; Collapse stays put */}
<div className="-mx-2.5 flex min-h-0 flex-1 flex-col gap-0.5 overflow-y-auto px-2.5">
{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 = (
<Link
href={it.href}
onClick={onNavigate}
aria-current={on ? "page" : undefined}
className={cn(
"flex h-8 items-center gap-[11px] whitespace-nowrap rounded-sm px-2.5 text-[14px] transition-colors duration-fast",
on ? "bg-blue-surface font-bold text-fg-accent" : "font-medium text-fg-strong hover:bg-surface-neutral",
collapsed && "justify-center px-0",
)}
>
<it.icon className="size-[17px] shrink-0" strokeWidth={2} aria-hidden />
{collapsed ? (
<span className="sr-only">{it.label}</span>
) : (
<div className="px-2.5 pb-1 pt-2.5 text-[11px] font-bold uppercase leading-none tracking-wide text-fg-muted">
{it.group}
</div>
))}
{collapsed ? (
<Tooltip content={it.label} side="right">
{link}
</Tooltip>
) : (
link
)}
</div>
);
})}
{collapsed ? (
<div className="flex-1" />
) : (
<div className="flex min-h-0 flex-1 flex-col">
<NotesTree onNavigate={onNavigate} />
</div>
)}
<>
<span className="flex-1">{it.label}</span>
{n ? <span className="tabular text-[11px] font-medium text-fg-muted">{count(n)}</span> : null}
</>
)}
</Link>
);
return (
<div key={it.href}>
{opens &&
(collapsed ? (
<div className="mx-1.5 my-2 h-px bg-border" />
) : (
<div className="px-2.5 pb-1 pt-2.5 text-[11px] font-bold uppercase leading-none tracking-wide text-fg-muted">
{it.group}
</div>
))}
{collapsed ? (
<Tooltip content={it.label} side="right">
{link}
</Tooltip>
) : (
link
)}
</div>
);
})}
{!collapsed && <NotesTree onNavigate={onNavigate} />}
</div>
{onToggle && (
<button
type="button"
Expand Down
Loading
Loading