Enhance synchronization logic and offline handling

- Refactor components to support conditional offline and online CRUD operations.
- Introduce `addToQueue` mechanism for syncing offline changes to the server.
- Add `isChapterContentExist` method and related existence checks in repositories.
- Consolidate data structures and streamline book, chapter, character, and guideline synchronization workflows.
- Encrypt additional character fields and adjust repository inserts for offline data.
This commit is contained in:
natreex
2026-01-07 20:43:34 -05:00
parent fa05d6dbae
commit 8eab6fd771
21 changed files with 557 additions and 578 deletions

View File

@@ -13,6 +13,9 @@ import CollapsableArea from "@/components/CollapsableArea";
import {useTranslations} from "next-intl";
import {LangContext} from "@/context/LangContext";
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from "@/context/SyncQueueContext";
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
import {SyncedBook} from "@/lib/models/SyncedBook";
interface MainChapterProps {
chapters: ChapterListProps[];
@@ -23,6 +26,8 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
const t = useTranslations();
const {lang} = useContext(LangContext)
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {addToQueue} = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
const {book} = useContext(BookContext);
const {session} = useContext(SessionContext);
const {errorMessage, successMessage} = useContext(AlertContext);
@@ -85,13 +90,13 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
bookId,
chapterId: chapterIdToRemove,
};
if (isCurrentlyOffline()) {
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:chapter:remove', deleteData);
} else {
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:chapter:remove', deleteData);
} else {
response = await System.authDeleteToServer<boolean>('chapter/remove', deleteData, token, lang);
response = await System.authDeleteToServer<boolean>('chapter/remove', deleteData, token, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
addToQueue('db:chapter:remove', deleteData);
}
}
if (!response) {
@@ -121,13 +126,16 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
chapterOrder: newChapterOrder ? newChapterOrder : 0,
title: newChapterTitle,
};
if (isCurrentlyOffline()) {
if (isCurrentlyOffline() || book?.localBook) {
responseId = await window.electron.invoke<string>('db:chapter:add', chapterData);
} else {
if (book?.localBook) {
responseId = await window.electron.invoke<string>('db:chapter:add', chapterData);
} else {
responseId = await System.authPostToServer<string>('chapter/add', chapterData, token);
responseId = await System.authPostToServer<string>('chapter/add', chapterData, token);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
addToQueue('db:chapter:add', {
...chapterData,
chapterId: responseId,
});
}
}
if (!responseId) {