mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-03 19:47:03 +00:00
fix(ui): handle library-open errors
Opening a book card swallowed failures: a corrupt or unreadable file failed silently and normalizeBook could throw on a missing chapters array. Surface load errors as a toast, throw a clear message on unparseable JSON, and make normalizeBook tolerant of missing chapters, metadata and settings. Claude-Session: https://claude.ai/code/session_01RzTWrLiy4zGuw2hZSQGamk
This commit is contained in:
1 parent
9eabef9041
commit
4e5ea6d855
3 files changed
+21
-5
No files matched your search
+11
-3
@@ -140,11 +140,19 @@ export function createCover(): Cover {
|
||||
return { kind: "default", image: "", bg: COVER_PALETTES[0].bg, ink: COVER_PALETTES[0].ink };
|
||||
}
|
||||
|
||||
const DEFAULT_METADATA: BookMetadata = { title: "Untitled", subtitle: "", author: "", isbn: "", language: "en" };
|
||||
const DEFAULT_SETTINGS: BookSettings = { trim: "6x9", bleed: true };
|
||||
|
||||
export function normalizeBook(book: Book): Book {
|
||||
const chapters = (Array.isArray(book.chapters) ? book.chapters : []).filter(Boolean);
|
||||
return {
|
||||
...book,
|
||||
metadata: { ...DEFAULT_METADATA, ...book.metadata },
|
||||
settings: { ...DEFAULT_SETTINGS, ...book.settings },
|
||||
cover: book.cover ? { ...createCover(), ...book.cover } : createCover(),
|
||||
chapters: book.chapters.map((c) => (c.updatedAt ? c : { ...c, updatedAt: Date.now() })),
|
||||
chapters: (chapters.length ? chapters : [createChapter()]).map((c) =>
|
||||
c.updatedAt ? c : { ...c, updatedAt: Date.now() },
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -152,9 +160,9 @@ export function createBook(): Book {
|
||||
return {
|
||||
schema: "margin/1",
|
||||
id: crypto.randomUUID(),
|
||||
metadata: { title: "Untitled", subtitle: "", author: "", isbn: "", language: "en" },
|
||||
metadata: { ...DEFAULT_METADATA },
|
||||
theme: "quiet-press",
|
||||
settings: { trim: "6x9", bleed: true },
|
||||
settings: { ...DEFAULT_SETTINGS },
|
||||
cover: createCover(),
|
||||
chapters: [createChapter("Chapter One")],
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user