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

@@ -22,11 +22,16 @@ import {
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";
function GuideLineSetting(props: any, ref: any) {
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 userToken: string = session?.accessToken ? session?.accessToken : '';
@@ -159,18 +164,18 @@ function GuideLineSetting(props: any, ref: any) {
intendedAudience: intendedAudience,
keyMessages: keyMessages,
};
if (isCurrentlyOffline()) {
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:guideline:update', guidelineData);
} else {
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:guideline:update', guidelineData);
} else {
response = await System.authPostToServer<boolean>(
'book/guide-line',
guidelineData,
userToken,
lang,
);
response = await System.authPostToServer<boolean>(
'book/guide-line',
guidelineData,
userToken,
lang,
);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
addToQueue('db:book:guideline:update', guidelineData);
}
}
if (!response) {
@@ -190,45 +195,28 @@ function GuideLineSetting(props: any, ref: any) {
async function saveQuillSense(): Promise<void> {
try {
let response: boolean;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:book:guideline:ai:update', {
bookId: bookId,
plotSummary: plotSummary,
verbTense: verbTense,
narrativeType: narrativeType,
dialogueType: dialogueType,
toneAtmosphere: toneAtmosphere,
language: language,
themes: themes,
});
const aiGuidelineData = {
bookId: bookId,
plotSummary: plotSummary,
verbTense: verbTense,
narrativeType: narrativeType,
dialogueType: dialogueType,
toneAtmosphere: toneAtmosphere,
language: language,
themes: themes,
};
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:guideline:ai:update', aiGuidelineData);
} else {
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:guideline:ai:update', {
bookId: bookId,
plotSummary: plotSummary,
verbTense: verbTense,
narrativeType: narrativeType,
dialogueType: dialogueType,
toneAtmosphere: toneAtmosphere,
language: language,
themes: themes,
});
} else {
response = await System.authPostToServer<boolean>(
'quillsense/book/guide-line',
{
bookId: bookId,
plotSummary: plotSummary,
verbTense: verbTense,
narrativeType: narrativeType,
dialogueType: dialogueType,
toneAtmosphere: toneAtmosphere,
language: language,
themes: themes,
},
userToken,
lang,
);
response = await System.authPostToServer<boolean>(
'quillsense/book/guide-line',
aiGuidelineData,
userToken,
lang,
);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
addToQueue('db:book:guideline:ai:update', aiGuidelineData);
}
}
if (response) {