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 {useTranslations} from "next-intl";
import {LangContext, LangContextProps} from "@/context/LangContext";
import OfflineContext, {OfflineContextType} from "@/context/OfflineContext";
import {BookContext} from "@/context/BookContext";
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from "@/context/SyncQueueContext";
import {BooksSyncContext, BooksSyncContextProps} from "@/context/BooksSyncContext";
import {SyncedBook} from "@/lib/models/SyncedBook";
interface WorldElementInputProps {
sectionLabel: string;
@@ -23,6 +26,8 @@ export default function WorldElementComponent({sectionLabel, sectionType}: World
const t = useTranslations();
const {lang} = useContext<LangContextProps>(LangContext);
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {addToQueue} = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
const {book} = useContext(BookContext);
const {worlds, setWorlds, selectedWorldIndex} = useContext(WorldContext);
const {errorMessage, successMessage} = useContext(AlertContext);
@@ -37,19 +42,19 @@ export default function WorldElementComponent({sectionLabel, sectionType}: World
try {
let response: boolean;
const elementId = (worlds[selectedWorldIndex][section] as WorldElement[])[index].id;
if (isCurrentlyOffline()) {
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:world:element:remove', {
elementId: elementId,
});
} else {
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:world:element:remove', {
response = await System.authDeleteToServer<boolean>('book/world/element/delete', {
elementId: elementId,
}, session.accessToken, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
addToQueue('db:book:world:element:remove', {
elementId: elementId,
});
} else {
response = await System.authDeleteToServer<boolean>('book/world/element/delete', {
elementId: elementId,
}, session.accessToken, lang);
}
}
if (!response) {
@@ -77,25 +82,26 @@ export default function WorldElementComponent({sectionLabel, sectionType}: World
}
try {
let elementId: string;
if (isCurrentlyOffline()) {
if (isCurrentlyOffline() || book?.localBook) {
elementId = await window.electron.invoke<string>('db:book:world:element:add', {
elementType: section,
worldId: worlds[selectedWorldIndex].id,
elementName: newElementName,
});
} else {
if (book?.localBook) {
elementId = await window.electron.invoke<string>('db:book:world:element:add', {
elementId = await System.authPostToServer('book/world/element/add', {
elementType: section,
worldId: worlds[selectedWorldIndex].id,
elementName: newElementName,
}, session.accessToken, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === book?.bookId)) {
addToQueue('db:book:world:element:add', {
elementType: section,
worldId: worlds[selectedWorldIndex].id,
elementId,
elementName: newElementName,
});
} else {
elementId = await System.authPostToServer('book/world/element/add', {
elementType: section,
worldId: worlds[selectedWorldIndex].id,
elementName: newElementName,
}, session.accessToken, lang);
}
}
if (!elementId) {

View File

@@ -17,6 +17,9 @@ import SelectBox from "@/components/form/SelectBox";
import {useTranslations} from "next-intl";
import {LangContext, LangContextProps} 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 interface ElementSection {
title: string;
@@ -28,6 +31,8 @@ export function WorldSetting(props: any, ref: any) {
const t = useTranslations();
const {lang} = useContext<LangContextProps>(LangContext);
const {isCurrentlyOffline} = useContext<OfflineContextType>(OfflineContext);
const {addToQueue} = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
const {localSyncedBooks} = useContext<BooksSyncContextProps>(BooksSyncContext);
const {errorMessage, successMessage} = useContext(AlertContext);
const {session} = useContext(SessionContext);
const {book} = useContext(BookContext);
@@ -89,22 +94,23 @@ export function WorldSetting(props: any, ref: any) {
}
try {
let worldId: string;
if (isCurrentlyOffline()) {
if (isCurrentlyOffline() || book?.localBook) {
worldId = await window.electron.invoke<string>('db:book:world:add', {
worldName: newWorldName,
bookId: bookId,
});
} else {
if (book?.localBook) {
worldId = await window.electron.invoke<string>('db:book:world:add', {
worldId = await System.authPostToServer<string>('book/world/add', {
worldName: newWorldName,
bookId: bookId,
}, session.accessToken, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
addToQueue('db:book:world:add', {
worldName: newWorldName,
worldId,
bookId: bookId,
});
} else {
worldId = await System.authPostToServer<string>('book/world/add', {
worldName: newWorldName,
bookId: bookId,
}, session.accessToken, lang);
}
}
if (!worldId) {
@@ -152,22 +158,17 @@ export function WorldSetting(props: any, ref: any) {
async function handleUpdateWorld(): Promise<void> {
try {
let response: boolean;
if (isCurrentlyOffline()) {
response = await window.electron.invoke<boolean>('db:book:world:update', {
world: worlds[selectedWorldIndex],
bookId: bookId,
});
const worldData = {
world: worlds[selectedWorldIndex],
bookId: bookId,
};
if (isCurrentlyOffline() || book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:world:update', worldData);
} else {
if (book?.localBook) {
response = await window.electron.invoke<boolean>('db:book:world:update', {
world: worlds[selectedWorldIndex],
bookId: bookId,
});
} else {
response = await System.authPutToServer<boolean>('book/world/update', {
world: worlds[selectedWorldIndex],
bookId: bookId,
}, session.accessToken, lang);
response = await System.authPutToServer<boolean>('book/world/update', worldData, session.accessToken, lang);
if (localSyncedBooks.find((syncedBook: SyncedBook): boolean => syncedBook.id === bookId)) {
addToQueue('db:book:world:update', worldData);
}
}
if (!response) {