From 6e2ca9c4bc8773371ad2419a3a9e153ca1a8560e Mon Sep 17 00:00:00 2001 From: PJ Date: Mon, 22 Jun 2026 13:17:50 -0400 Subject: [PATCH] fix(persist): persist new books on create A freshly created book lived only in memory until the first edit, so quitting beforehand lost it and it never appeared in the library. Save it to disk on creation via a shared createAndOpenBook used by both the Library card and the New Book menu action. Claude-Session: https://claude.ai/code/session_01RzTWrLiy4zGuw2hZSQGamk --- src/App.tsx | 4 ++-- src/components/Library.tsx | 4 ++-- src/library.ts | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index acb50f7..6d17dca 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -5,7 +5,7 @@ import { Library } from "./components/Library"; import { EditorView } from "./components/EditorView"; import { useBook } from "./store/useBook"; import { isDesktop } from "./ipc"; -import { newBook, saveBook } from "./library"; +import { createAndOpenBook, saveBook } from "./library"; import { runExport } from "./export/run"; import { checkForUpdates } from "./updater"; @@ -18,7 +18,7 @@ function App() { checkForUpdates(true); const unlisten = listen("menu-action", (event) => { const state = useBook.getState(); - if (event.payload === "new-book") state.openBook(newBook()); + if (event.payload === "new-book") createAndOpenBook(state.openBook, state.setNotice); else if (event.payload === "export-pdf") runExport("pdf"); else if (event.payload === "export-epub") runExport("epub"); else if (event.payload === "check-updates") checkForUpdates(false); diff --git a/src/components/Library.tsx b/src/components/Library.tsx index ba1d403..ccdee2a 100644 --- a/src/components/Library.tsx +++ b/src/components/Library.tsx @@ -1,5 +1,5 @@ import { useEffect, useState } from "react"; -import { type BookSummary, deleteBook, exampleBook, listBooks, loadBook, newBook, saveBook } from "../library"; +import { type BookSummary, createAndOpenBook, deleteBook, exampleBook, listBooks, loadBook, saveBook } from "../library"; import type { Book } from "../model/book"; import { importEpub } from "../import/epub"; import { isDesktop } from "../ipc"; @@ -64,7 +64,7 @@ export function Library({ onOpen }: { onOpen: (book: Book) => void }) {
- diff --git a/src/library.ts b/src/library.ts index cc7a367..e329473 100644 --- a/src/library.ts +++ b/src/library.ts @@ -35,3 +35,18 @@ export function newBook(): Book { export function exampleBook(): Book { return starterBook(); } + +export async function createAndOpenBook( + open: (book: Book) => void, + onError?: (message: string) => void, +): Promise { + const book = newBook(); + if (isDesktop) { + try { + await saveBook(book); + } catch (e) { + onError?.(`Could not create book: ${e}`); + } + } + open(book); +}