Save reading progress in book config
This commit is contained in:
@@ -1,69 +1,106 @@
|
||||
import { create } from 'zustand';
|
||||
|
||||
import { BookNote, BookContent, Book } from '@/types/book';
|
||||
import { BookNote, BookContent, Book, BookConfig } from '@/types/book';
|
||||
import { AppService } from '@/types/system';
|
||||
|
||||
interface BookState {
|
||||
loading?: boolean;
|
||||
content?: BookContent | null;
|
||||
error?: string | null;
|
||||
notes?: BookNote[];
|
||||
book?: Book | null;
|
||||
file?: File | null;
|
||||
config?: BookConfig | null;
|
||||
fraction?: number;
|
||||
}
|
||||
|
||||
interface ReaderStore {
|
||||
library: Book[];
|
||||
books: Record<string, BookState>;
|
||||
|
||||
setLibrary: (books: Book[]) => void;
|
||||
fetchBook: (appService: AppService, id: string) => Promise<Book | null>;
|
||||
addNote: (id: string, note: BookNote) => void;
|
||||
setProgress: (id: string, progress: number, location: string, pageinfo: {}) => void;
|
||||
addBookmark: (id: string, bookmark: BookNote) => void;
|
||||
}
|
||||
|
||||
export const useReaderStore = create<ReaderStore>((set) => {
|
||||
return {
|
||||
books: {},
|
||||
export const useReaderStore = create<ReaderStore>((set) => ({
|
||||
library: [],
|
||||
books: {},
|
||||
|
||||
setLibrary: (books: Book[]) => set({ library: books }),
|
||||
|
||||
fetchBook: async (appService: AppService, id: string) => {
|
||||
set((state) => ({
|
||||
books: {
|
||||
...state.books,
|
||||
[id]: { loading: true, file: null, book: null, config: null, error: null, notes: [] },
|
||||
},
|
||||
}));
|
||||
|
||||
try {
|
||||
const library = await appService.loadLibraryBooks();
|
||||
const book = library.find((b) => b.hash === id);
|
||||
if (!book) {
|
||||
throw new Error('Book not found');
|
||||
}
|
||||
const content = (await appService.loadBookContent(book)) as BookContent;
|
||||
const { file, config } = content;
|
||||
|
||||
fetchBook: async (appService: AppService, id: string) => {
|
||||
set((state) => ({
|
||||
books: {
|
||||
...state.books,
|
||||
[id]: { loading: true, content: null, error: null, notes: [] },
|
||||
[id]: { ...state.books[id], loading: false, book, file, config },
|
||||
},
|
||||
}));
|
||||
|
||||
try {
|
||||
const library = await appService.loadLibraryBooks();
|
||||
const book = library.find((b) => b.hash === id);
|
||||
if (!book) {
|
||||
throw new Error('Book not found');
|
||||
}
|
||||
const content = (await appService.loadBookContent(book)) as BookContent;
|
||||
|
||||
set((state) => ({
|
||||
books: {
|
||||
...state.books,
|
||||
[id]: { ...state.books[id], loading: false, content },
|
||||
},
|
||||
}));
|
||||
return book;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
set((state) => ({
|
||||
books: {
|
||||
...state.books,
|
||||
[id]: { ...state.books[id], loading: false, error: 'Failed to load book.' },
|
||||
},
|
||||
}));
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
addNote: (id: string, note: BookNote) =>
|
||||
return book;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
set((state) => ({
|
||||
books: {
|
||||
...state.books,
|
||||
[id]: { ...state.books[id], loading: false, error: 'Failed to load book.' },
|
||||
},
|
||||
}));
|
||||
return null;
|
||||
}
|
||||
},
|
||||
|
||||
setProgress: (id: string, progress: number, location: string, pageinfo: {}) =>
|
||||
set((state) => {
|
||||
const book = state.books[id];
|
||||
if (!book) return state;
|
||||
return {
|
||||
books: {
|
||||
...state.books,
|
||||
[id]: {
|
||||
...state.books[id],
|
||||
notes: [...state.books[id]!.notes!, note],
|
||||
...book,
|
||||
config: {
|
||||
...book.config,
|
||||
lastUpdated: Date.now(),
|
||||
progress,
|
||||
location,
|
||||
pageinfo,
|
||||
},
|
||||
},
|
||||
},
|
||||
})),
|
||||
};
|
||||
});
|
||||
};
|
||||
}),
|
||||
|
||||
addBookmark: (id: string, bookmark: BookNote) =>
|
||||
set((state) => {
|
||||
const book = state.books[id];
|
||||
if (!book) return state;
|
||||
return {
|
||||
books: {
|
||||
...state.books,
|
||||
[id]: {
|
||||
...book,
|
||||
config: {
|
||||
...book.config,
|
||||
lastUpdated: Date.now(),
|
||||
bookmarks: [...(book.config?.bookmarks || []), bookmark],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
}),
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user