Integrate offline logic for book creation and enhance synchronization
- Add offline handling to `AddNewBookForm` by updating `BooksSyncContext` with server-only and local-only book management. - Refactor `guideTourDone` to check offline completion states via `localStorage`. - Update and lock dependencies, including `@esbuild` and `@next`, to latest versions. - Clean up unused session state updates in book creation logic.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
'use client'
|
||||
import {ChangeEvent, Dispatch, SetStateAction, useContext, useEffect, useRef, useState} from "react";
|
||||
import {ChangeEvent, Dispatch, RefObject, SetStateAction, useContext, useEffect, useRef, useState} from "react";
|
||||
import {AlertContext} from "@/context/AlertContext";
|
||||
import System from "@/lib/models/System";
|
||||
import {SessionContext} from "@/context/SessionContext";
|
||||
@@ -28,6 +28,8 @@ import {UserProps} from "@/lib/models/User";
|
||||
import {useTranslations} from "next-intl";
|
||||
import {LangContext, LangContextProps} from "@/context/LangContext";
|
||||
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
|
||||
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
|
||||
import {SyncedBook} from "@/lib/models/SyncedBook";
|
||||
|
||||
interface MinMax {
|
||||
min: number;
|
||||
@@ -37,10 +39,11 @@ interface MinMax {
|
||||
export default function AddNewBookForm({setCloseForm}: { setCloseForm: Dispatch<SetStateAction<boolean>> }) {
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext<LangContextProps>(LangContext);
|
||||
const {session, setSession} = useContext(SessionContext);
|
||||
const {session} = useContext(SessionContext);
|
||||
const {errorMessage} = useContext(AlertContext);
|
||||
const {setServerOnlyBooks, setLocalOnlyBooks} = useContext<BooksSyncContextProps>(BooksSyncContext)
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
|
||||
const modalRef: React.RefObject<HTMLDivElement | null> = useRef<HTMLDivElement>(null);
|
||||
const modalRef: RefObject<HTMLDivElement | null> = useRef<HTMLDivElement>(null);
|
||||
|
||||
const [title, setTitle] = useState<string>('');
|
||||
const [subtitle, setSubtitle] = useState<string>('');
|
||||
@@ -159,14 +162,45 @@ export default function AddNewBookForm({setCloseForm}: { setCloseForm: Dispatch<
|
||||
bookId: bookId,
|
||||
...bookData
|
||||
};
|
||||
|
||||
setSession({
|
||||
...session,
|
||||
user: {
|
||||
...session.user as UserProps,
|
||||
books: [...((session.user as UserProps)?.books ?? []), book]
|
||||
}
|
||||
});
|
||||
if (isCurrentlyOffline()){
|
||||
setLocalOnlyBooks((prevBooks: SyncedBook[]): SyncedBook[] => [...prevBooks, {
|
||||
id: book.bookId,
|
||||
type: selectedBookType,
|
||||
title: title,
|
||||
subTitle: subtitle,
|
||||
lastUpdate: new Date().getTime()/1000,
|
||||
chapters: [],
|
||||
characters: [],
|
||||
locations: [],
|
||||
worlds: [],
|
||||
incidents: [],
|
||||
plotPoints: [],
|
||||
issues: [],
|
||||
actSummaries: [],
|
||||
guideLine: null,
|
||||
aiGuideLine: null
|
||||
}]);
|
||||
}
|
||||
else {
|
||||
setServerOnlyBooks((prevBooks: SyncedBook[]): SyncedBook[] => [...prevBooks, {
|
||||
id: book.bookId,
|
||||
type: selectedBookType,
|
||||
title: title,
|
||||
subTitle: subtitle,
|
||||
lastUpdate: new Date().getTime()/1000,
|
||||
chapters: [],
|
||||
characters: [],
|
||||
locations: [],
|
||||
worlds: [],
|
||||
incidents: [],
|
||||
plotPoints: [],
|
||||
issues: [],
|
||||
actSummaries: [],
|
||||
guideLine: null,
|
||||
aiGuideLine: null
|
||||
}]);
|
||||
}
|
||||
|
||||
setIsAddingBook(false);
|
||||
setCloseForm(false)
|
||||
} catch (e: unknown) {
|
||||
|
||||
@@ -24,7 +24,7 @@ export default function BookList() {
|
||||
const {setBook} = useContext(BookContext);
|
||||
const t = useTranslations();
|
||||
const {lang} = useContext<LangContextProps>(LangContext)
|
||||
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext)
|
||||
const {isCurrentlyOffline, offlineMode} = useContext<OfflineContextType>(OfflineContext)
|
||||
const {booksToSyncFromServer, booksToSyncToServer, serverOnlyBooks, localOnlyBooks} = useContext<BooksSyncContextProps>(BooksSyncContext)
|
||||
|
||||
const [searchQuery, setSearchQuery] = useState<string>('');
|
||||
@@ -32,7 +32,7 @@ export default function BookList() {
|
||||
const [isLoadingBooks, setIsLoadingBooks] = useState<boolean>(true);
|
||||
|
||||
const [bookGuide, setBookGuide] = useState<boolean>(false);
|
||||
|
||||
|
||||
const bookGuideSteps: GuideStep[] = [
|
||||
{
|
||||
id: 0,
|
||||
@@ -86,23 +86,45 @@ export default function BookList() {
|
||||
setBookGuide(true);
|
||||
}
|
||||
}, [groupedBooks]);
|
||||
|
||||
|
||||
// Charger les livres quand les conditions sont remplies
|
||||
useEffect((): void => {
|
||||
getBooks().then()
|
||||
}, [booksToSyncFromServer, booksToSyncToServer, serverOnlyBooks, localOnlyBooks]);
|
||||
|
||||
useEffect((): void => {
|
||||
if (accessToken) getBooks().then();
|
||||
}, [accessToken]);
|
||||
const shouldFetchBooks =
|
||||
(session.isConnected || accessToken) &&
|
||||
(!isCurrentlyOffline() || offlineMode.isDatabaseInitialized);
|
||||
|
||||
if (shouldFetchBooks) {
|
||||
getBooks().then();
|
||||
}
|
||||
}, [
|
||||
session.isConnected,
|
||||
accessToken,
|
||||
offlineMode.isDatabaseInitialized,
|
||||
booksToSyncFromServer,
|
||||
booksToSyncToServer,
|
||||
serverOnlyBooks,
|
||||
localOnlyBooks
|
||||
]);
|
||||
|
||||
async function handleFirstBookGuide(): Promise<void> {
|
||||
try {
|
||||
const response: boolean = await System.authPostToServer<boolean>(
|
||||
'logs/tour',
|
||||
{plateforme: 'web', tour: 'new-first-book'},
|
||||
session.accessToken, lang
|
||||
);
|
||||
if (response) {
|
||||
if (!isCurrentlyOffline()) {
|
||||
const response: boolean = await System.authPostToServer<boolean>(
|
||||
'logs/tour',
|
||||
{plateforme: 'web', tour: 'new-first-book'},
|
||||
session.accessToken, lang
|
||||
);
|
||||
if (response) {
|
||||
setSession(User.setNewGuideTour(session, 'new-first-book'));
|
||||
setBookGuide(false);
|
||||
}
|
||||
} else {
|
||||
// Mode offline: stocker dans localStorage
|
||||
const completedGuides = JSON.parse(localStorage.getItem('completedGuides') || '[]');
|
||||
if (!completedGuides.includes('new-first-book')) {
|
||||
completedGuides.push('new-first-book');
|
||||
localStorage.setItem('completedGuides', JSON.stringify(completedGuides));
|
||||
}
|
||||
setSession(User.setNewGuideTour(session, 'new-first-book'));
|
||||
setBookGuide(false);
|
||||
}
|
||||
@@ -122,7 +144,9 @@ export default function BookList() {
|
||||
if (!isCurrentlyOffline()) {
|
||||
const [onlineBooks, localBooks]: [BookListProps[], BookListProps[]] = await Promise.all([
|
||||
System.authGetQueryToServer<BookListProps[]>('books', accessToken, lang),
|
||||
window.electron.invoke<BookListProps[]>('db:book:books')
|
||||
offlineMode.isDatabaseInitialized
|
||||
? window.electron.invoke<BookListProps[]>('db:book:books')
|
||||
: Promise.resolve([])
|
||||
]);
|
||||
const onlineBookIds: Set<string> = new Set(onlineBooks.map((book: BookListProps): string => book.id));
|
||||
const uniqueLocalBooks: BookListProps[] = localBooks.filter((book: BookListProps): boolean => !onlineBookIds.has(book.id));
|
||||
@@ -131,6 +155,10 @@ export default function BookList() {
|
||||
...uniqueLocalBooks.map((book: BookListProps): BookListProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: true }))
|
||||
];
|
||||
} else {
|
||||
if (!offlineMode.isDatabaseInitialized) {
|
||||
setIsLoadingBooks(false);
|
||||
return;
|
||||
}
|
||||
const localBooks: BookListProps[] = await window.electron.invoke<BookListProps[]>('db:book:books');
|
||||
bookResponse = localBooks.map((book: BookListProps): BookListProps & { itIsLocal: boolean } => ({ ...book, itIsLocal: true }));
|
||||
}
|
||||
@@ -203,6 +231,10 @@ export default function BookList() {
|
||||
try {
|
||||
let bookResponse: BookListProps|null = null;
|
||||
if (isCurrentlyOffline()){
|
||||
if (!offlineMode.isDatabaseInitialized) {
|
||||
errorMessage(t("bookList.errorBookDetails"));
|
||||
return;
|
||||
}
|
||||
bookResponse = await window.electron.invoke('db:book:bookBasicInformation', bookId)
|
||||
} else {
|
||||
bookResponse = await System.authGetQueryToServer<BookListProps>(`book/basic-information`, accessToken, lang, {
|
||||
|
||||
Reference in New Issue
Block a user