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

@@ -12,11 +12,16 @@ import {useTranslations} from "next-intl";
import InlineAddInput from "@/components/form/InlineAddInput";
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";
export default function ScribeChapterComponent() {
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 {chapter, setChapter} = useContext(ChapterContext);
@@ -137,25 +142,18 @@ export default function ScribeChapterComponent() {
async function handleChapterUpdate(chapterId: string, title: string, chapterOrder: number): Promise<void> {
try {
let response: boolean;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:chapter:update',{
chapterId: chapterId,
chapterOrder: chapterOrder,
title: title,
})
const updateData = {
chapterId: chapterId,
chapterOrder: chapterOrder,
title: title,
};
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:chapter:update', updateData);
} else {
if (book?.localBook){
response = await window.electron.invoke<boolean>('db:chapter:update',{
chapterId: chapterId,
chapterOrder: chapterOrder,
title: title,
})
} else {
response = await System.authPostToServer<boolean>('chapter/update', {
chapterId: chapterId,
chapterOrder: chapterOrder,
title: title,
}, userToken, lang);
response = await System.authPostToServer<boolean>('chapter/update', updateData, userToken, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
addToQueue('db:chapter:update', updateData);
}
}
if (!response) {
@@ -190,15 +188,17 @@ export default function ScribeChapterComponent() {
try {
setDeleteConfirmationMessage(false);
let response:boolean = false;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:chapter:remove', removeChapterId)
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:chapter:remove', removeChapterId);
} else {
if (book?.localBook){
response = await window.electron.invoke<boolean>('db:chapter:remove', removeChapterId)
} else {
response = await System.authDeleteToServer<boolean>('chapter/remove', {
response = await System.authDeleteToServer<boolean>('chapter/remove', {
chapterId: removeChapterId,
}, userToken, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
addToQueue('db:chapter:remove', {
chapterId: removeChapterId,
}, userToken, lang);
});
}
}
if (!response) {
@@ -226,25 +226,21 @@ export default function ScribeChapterComponent() {
const chapterTitle: string = chapterOrder >= 0 ? newChapterName : book?.title as string;
try {
let chapterId:string|null = null;
if (isCurrentlyOffline()){
chapterId = await window.electron.invoke<string>('db:chapter:add', {
bookId: book?.bookId,
chapterOrder: chapterOrder,
title: chapterTitle
})
const addData = {
bookId: book?.bookId,
chapterOrder: chapterOrder,
title: chapterTitle
};
if (isCurrentlyOffline() || book?.localBook){
chapterId = await window.electron.invoke<string>('db:chapter:add', addData);
} else {
if (book?.localBook){
chapterId = await window.electron.invoke<string>('db:chapter:add', {
bookId: book?.bookId,
chapterOrder: chapterOrder,
title: chapterTitle
})
} else {
chapterId = await System.authPostToServer<string>('chapter/add', {
bookId: book?.bookId,
chapterOrder: chapterOrder,
title: chapterTitle
}, userToken, lang);
chapterId = await System.authPostToServer<string>('chapter/add', addData, userToken, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
addToQueue('db:chapter:add', {
...addData,
chapterId,
});
}
}
if (!chapterId) {