feat(model): add document model and book store

This commit is contained in:
pj committed 2026-06-20 12:28:43 -04:00
1 parent 03a05b7e8f
commit c76e5b0a5f
2 files changed
+129

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import { create } from "zustand";
import type { JSONContent } from "@tiptap/core";
import { type Book, createChapter, starterBook } from "../model/book";
interface BookState {
book: Book;
activeChapterId: string;
dirty: boolean;
setActiveChapter: (id: string) => void;
setChapterContent: (id: string, content: JSONContent) => void;
setChapterTitle: (id: string, title: string) => void;
addChapter: () => void;
}
export const useBook = create<BookState>((set) => {
const book = starterBook();
return {
book,
activeChapterId: book.chapters[0].id,
dirty: false,
setActiveChapter: (id) => set({ activeChapterId: id }),
setChapterContent: (id, content) =>
set((s) => ({
dirty: true,
book: {
...s.book,
chapters: s.book.chapters.map((c) => (c.id === id ? { ...c, content } : c)),
},
})),
setChapterTitle: (id, title) =>
set((s) => ({
dirty: true,
book: {
...s.book,
chapters: s.book.chapters.map((c) => (c.id === id ? { ...c, title } : c)),
},
})),
addChapter: () =>
set((s) => {
const chapter = createChapter();
return {
activeChapterId: chapter.id,
dirty: true,
book: { ...s.book, chapters: [...s.book.chapters, chapter] },
};
}),
};
});