Refactor and extend offline synchronization logic across components and services

- Integrated sync queue mechanisms with `LocalSyncQueueContext` for offline data handling.
- Updated key sync-related services (e.g., book, chapter, series) to support offline-first functionality.
- Removed redundant database fetch methods to optimize repository logic and improve maintainability.
- Enhanced Tauri IPC usage for sync operations and removed legacy methods in Rust services.
This commit is contained in:
natreex
2026-03-30 21:06:58 -04:00
parent b9606e899a
commit dbbe33b19b
22 changed files with 295 additions and 293 deletions

View File

@@ -11,6 +11,9 @@ import {apiDelete, apiPost} from "@/lib/api/client";
import {isDesktop} from '@/lib/configs';
import * as tauri from '@/lib/tauri';
import OfflineContext, {OfflineContextType} from '@/context/OfflineContext';
import {BooksSyncContext, BooksSyncContextProps} from '@/context/BooksSyncContext';
import {SyncedBook} from '@/lib/types/synced-book';
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from '@/context/SyncQueueContext';
import {BookContext, BookContextProps} from '@/context/BookContext';
import InputField from "@/components/form/InputField";
import {useTranslations} from '@/lib/i18n';
@@ -52,6 +55,8 @@ export default function WorldElementComponent({sectionLabel, sectionType}: World
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
const {book}: BookContextProps = useContext<BookContextProps>(BookContext);
const {isCurrentlyOffline}: OfflineContextType = useContext<OfflineContextType>(OfflineContext);
const {addToQueue}: LocalSyncQueueContextProps = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
const {localSyncedBooks}: BooksSyncContextProps = useContext<BooksSyncContextProps>(BooksSyncContext);
const [newElementName, setNewElementName] = useState<string>('');
@@ -69,6 +74,9 @@ export default function WorldElementComponent({sectionLabel, sectionType}: World
response = await apiDelete<boolean>(endpoint, {
elementId: elements[index].id,
}, session.accessToken, lang);
if (!isSeriesMode && isDesktop && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === book?.bookId)) {
addToQueue('remove_world_element', {elementId: elements[index].id, bookId: book?.bookId, deletedAt: Date.now()});
}
}
if (!response) {
errorMessage(t("worldSetting.unknownError"))
@@ -119,6 +127,9 @@ export default function WorldElementComponent({sectionLabel, sectionType}: World
worldId: worlds[selectedWorldIndex].id,
elementName: newElementName,
}, session.accessToken, lang);
if (isDesktop && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === book?.bookId)) {
addToQueue('add_world_element', {worldId: worlds[selectedWorldIndex].id, elementId, elementType: section, elementName: newElementName});
}
if (!elementId) {
errorMessage(t("worldSetting.unknownError"))
return;