From df6ce147c6ca5d2a9e17db201efb3c461ae230c3 Mon Sep 17 00:00:00 2001 From: Codex Date: Thu, 26 Nov 2026 17:13:58 +0000 Subject: [PATCH] Recover when a notebook results page disappears --- frontend/src/components/NotebookView.test.tsx | 30 +++++++++++++++++++ frontend/src/components/NotebookView.tsx | 20 +++++++++---- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/NotebookView.test.tsx b/frontend/src/components/NotebookView.test.tsx index e85e028..2db7e7f 100644 --- a/frontend/src/components/NotebookView.test.tsx +++ b/frontend/src/components/NotebookView.test.tsx @@ -76,4 +76,34 @@ describe("cross-course notebook", () => { ).toHaveAttribute("href", "#/account"); expect(request).not.toHaveBeenCalled(); }); + + it("returns to the last available page when notes disappear during pagination", async () => { + const user = userEvent.setup(); + const firstPage = { + ...page, + items: Array.from({ length: page.limit }, (_, index) => ({ + ...page.items[0], + lesson_id: `note-${index + 1}`, + lesson_title: `Lesson ${index + 1}`, + })), + }; + const request = vi + .spyOn(api, "notebook") + .mockResolvedValueOnce({ ...firstPage, total: 51 }) + .mockResolvedValueOnce({ ...page, items: [], total: 50, offset: 50 }) + .mockResolvedValueOnce({ ...firstPage, total: 50 }); + render( + , + ); + + await user.click(await screen.findByRole("button", { name: "More notes" })); + + expect(await screen.findByText("50 saved notes")).toBeInTheDocument(); + expect(screen.getByRole("link", { name: "Lesson 50" })).toBeInTheDocument(); + expect(request.mock.calls).toEqual([ + ["", undefined, 0], + ["", undefined, 50], + ["", undefined, 0], + ]); + }); }); diff --git a/frontend/src/components/NotebookView.tsx b/frontend/src/components/NotebookView.tsx index 7e3cef9..93d2ec4 100644 --- a/frontend/src/components/NotebookView.tsx +++ b/frontend/src/components/NotebookView.tsx @@ -37,13 +37,23 @@ export function NotebookView({ api .notebook(query, courseId || undefined, offset) .then((result) => { - if (active) setPage(result); + if (!active) return; + if (offset > 0 && result.items.length === 0) { + // Notes may have changed since the learner opened the previous page. + const lastPage = Math.max( + 0, + Math.ceil(result.total / pageSize) - 1, + ); + setOffset(Math.min(lastPage * pageSize, offset - pageSize)); + return; + } + setPage(result); + setLoading(false); }) .catch((requestError) => { - if (active) setError(errorMessage(requestError)); - }) - .finally(() => { - if (active) setLoading(false); + if (!active) return; + setError(errorMessage(requestError)); + setLoading(false); }); }, 200);