Update the lastUpdated timestamp in library when book is opened

This commit is contained in:
chrox
2024-10-10 18:38:52 +02:00
parent b88e86be5f
commit c07385f13f
3 changed files with 16 additions and 7 deletions
+11 -5
View File
@@ -21,11 +21,17 @@ const ReaderPage = () => {
useEffect(() => {
envConfig.appService().then((appService) => {
appService.loadSettings().then(() => {
if (id && !bookState.content) {
fetchBook(appService, id);
}
});
appService
.loadSettings()
.then(() => {
return id && !bookState.content ? fetchBook(appService, id) : null;
})
.then((book) => {
if (book) {
book.lastUpdated = Date.now();
appService.updateLibraryBook(book);
}
});
});
}, [id, fetchBook, bookState.content, envConfig]);
+4 -2
View File
@@ -1,6 +1,6 @@
import { create } from 'zustand';
import { BookNote, BookContent } from '@/types/book';
import { BookNote, BookContent, Book } from '@/types/book';
import { AppService } from '@/types/system';
interface BookState {
@@ -12,7 +12,7 @@ interface BookState {
interface ReaderStore {
books: Record<string, BookState>;
fetchBook: (appService: AppService, id: string) => Promise<void>;
fetchBook: (appService: AppService, id: string) => Promise<Book | null>;
addNote: (id: string, note: BookNote) => void;
}
@@ -42,6 +42,7 @@ export const useReaderStore = create<ReaderStore>((set) => {
[id]: { ...state.books[id], loading: false, content },
},
}));
return book;
} catch (error) {
console.error(error);
set((state) => ({
@@ -50,6 +51,7 @@ export const useReaderStore = create<ReaderStore>((set) => {
[id]: { ...state.books[id], loading: false, error: 'Failed to load book.' },
},
}));
return null;
}
},
+1
View File
@@ -31,5 +31,6 @@ export interface AppService {
loadBookContent(book: Book): Promise<BookContent>;
loadLibraryBooks(): Promise<Book[]>;
saveLibraryBooks(books: Book[]): Promise<void>;
updateLibraryBook(book: Book): Promise<void>;
getCoverImageUrl(book: Book): string;
}