Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): open a deep book URL on the book, not on the library

Bingxi Zhao (Frank)afade979b10f
diff --git a/web/app/(workspace)/books/BooksRoute.tsx b/web/app/(workspace)/books/BooksRoute.tsx
index 816c6d028..489d2d4f3 100644
--- a/web/app/(workspace)/books/BooksRoute.tsx
+++ b/web/app/(workspace)/books/BooksRoute.tsx
@@ -35,7 +35,15 @@ import LearningCapturePanel from './components/LearningCapturePanel'
import SpineEditor from './components/SpineEditor'
import type { QuizAttemptArgs } from './components/blocks/QuizBlock'
-type View = 'list' | 'creator' | 'spine' | 'reader'
+/**
+ * Which surface is on screen.
+ *
+ * `opening` is the one the route can assert before any data exists: a deep
+ * book URL is not the library, and defaulting to `list` while the fetch was
+ * in flight drew the (still empty) library first and jumped to the book when
+ * it landed — a direct link or a refresh looked like a bad route (#1440).
+ */
+type View = 'list' | 'opening' | 'creator' | 'spine' | 'reader'
// Blocks land one at a time during compilation. Coalescing a burst into a
// single fetch keeps a page that is actively generating from issuing one
@@ -98,7 +106,7 @@ function BookPageInner() {
const [books, setBooks] = useState<Book[]>([])
const [canCreateBook, setCanCreateBook] = useState(true)
const [loadingBooks, setLoadingBooks] = useState(false)
- const [view, setView] = useState<View>('list')
+ const [view, setView] = useState<View>(requestedBookId ? 'opening' : 'list')
const [selectedBookId, setSelectedBookId] = useState<string | null>(null)
const [detail, setDetail] = useState<BookDetail | null>(null)
@@ -436,8 +444,21 @@ function BookPageInner() {
if (requestedBookId === selectedBookId) return
if (requestedBookId === lastDeepLinkedBookId.current) return
lastDeepLinkedBookId.current = requestedBookId
- void handleSelectBook(requestedBookId, requestedPageId)
- }, [requestedBookId, requestedPageId, selectedBookId, handleSelectBook])
+ // The shell is now drawn before the book exists, so a load that fails has
+ // to say so and hand the reader back; otherwise the shell is where they
+ // stay. Selecting nothing returns to the library and to its URL.
+ void handleSelectBook(requestedBookId, requestedPageId).catch(err => {
+ notify(
+ t('{{action}} failed: {{reason}}', {
+ action: t('Open book'),
+ reason: bookErrorMessage(err, t),
+ }),
+ { tone: 'error', durationMs: 8000 }
+ )
+ console.error('Open book failed:', err)
+ void handleSelectBook(null)
+ })
+ }, [requestedBookId, requestedPageId, selectedBookId, handleSelectBook, t])
useEffect(() => {
if (view !== 'reader' || !selectedBookId) {
@@ -996,6 +1017,13 @@ function BookPageInner() {
/>
)}
<div className="min-h-0 flex-1 overflow-hidden">
+ {view === 'opening' && (
+ <div className="flex h-full w-full items-center justify-center gap-2 text-[12px] text-[var(--muted-foreground)]">
+ <Loader2 className="h-4 w-4 animate-spin" />
+ <BookLoadingText />
+ </div>
+ )}
+
{view === 'list' && (
<BookLibrary
books={books}
diff --git a/web/locales/en/app.json b/web/locales/en/app.json
index 150133181..bd60b8943 100644
--- a/web/locales/en/app.json
+++ b/web/locales/en/app.json
@@ -814,6 +814,7 @@
"Untitled": "Untitled",
"Knowledge Point": "Knowledge Point",
"Open": "Open",
+ "Open book": "Open book",
"Generation failed": "Generation failed",
"pages ready": "pages ready",
"Completed": "Completed",
diff --git a/web/locales/zh/app.json b/web/locales/zh/app.json
index 53fb07a60..db9f25893 100644
--- a/web/locales/zh/app.json
+++ b/web/locales/zh/app.json
@@ -843,6 +843,7 @@
"Untitled": "未命名",
"Knowledge Point": "知识点",
"Open": "打开",
+ "Open book": "打开书籍",
"Generation failed": "生成失败",
"pages ready": "页已就绪",
"Completed": "已完成",
diff --git a/web/tests/book-deep-link-shell.spec.tsx b/web/tests/book-deep-link-shell.spec.tsx
new file mode 100644
index 000000000..7c6c14c05
--- /dev/null
+++ b/web/tests/book-deep-link-shell.spec.tsx
@@ -0,0 +1,40 @@
+import { render, screen } from "@testing-library/react";
+import { describe, expect, it, vi } from "vitest";
+
+import BookPage from "@/app/(workspace)/books/BooksRoute";
+import { initI18n } from "@/i18n/init";
+
+initI18n("en");
+
+// A deep book URL, with the book's data still in flight.
+vi.mock("next/navigation", () => ({
+ useParams: () => ({ bookId: "bk-1" }),
+ useRouter: () => ({ push: vi.fn(), replace: vi.fn() }),
+}));
+
+vi.mock("@/lib/book-api", () => ({
+ BookApiError: class BookApiError extends Error {},
+ bookApi: {
+ list: vi.fn(() => Promise.resolve({ books: [], can_create: true })),
+ get: vi.fn(() => new Promise(() => {})),
+ },
+}));
+
+vi.mock("@/lib/use-book-stream", () => ({
+ useBookStream: () => undefined,
+ bookEventKind: () => "",
+ bookEventPageId: () => "",
+}));
+
+describe("a deep book URL", () => {
+ it("never renders the library it is not pointing at", async () => {
+ render(<BookPage />);
+
+ expect(await screen.findByText("Loading…")).toBeInTheDocument();
+ // The library's own empty state is what the reader used to see first —
+ // an empty list one level above the book they had asked for (#1440).
+ expect(
+ screen.queryByRole("button", { name: /new book/i })
+ ).not.toBeInTheDocument();
+ });
+});