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

@@ -7,6 +7,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 {SessionContext, SessionContextProps} from '@/context/SessionContext';
import {AlertContext, AlertContextProps} from '@/context/AlertContext';
@@ -30,6 +33,8 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
const {errorMessage, successMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
const {isCurrentlyOffline}: OfflineContextType = useContext<OfflineContextType>(OfflineContext);
const {addToQueue}: LocalSyncQueueContextProps = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
const {localSyncedBooks}: BooksSyncContextProps = useContext<BooksSyncContextProps>(BooksSyncContext);
const bookId: string | undefined = book?.bookId;
const token: string = session.accessToken;
@@ -88,15 +93,12 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
response = await tauri.removeChapter(chapterIdToRemove, bookId ?? '', Date.now());
} else {
response = await apiDelete<boolean>(
'chapter/remove',
{
bookId,
chapterId: chapterIdToRemove,
},
token,
lang,
);
const deleteData = {bookId, chapterId: chapterIdToRemove};
response = await apiDelete<boolean>('chapter/remove', deleteData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('remove_chapter', {...deleteData, deletedAt: Date.now()});
}
}
if (!response) {
errorMessage(t("mainChapter.errorDelete"));
@@ -126,16 +128,12 @@ export default function MainChapter({chapters, setChapters}: MainChapterProps) {
chapterOrder: newChapterOrder ? newChapterOrder : 0,
});
} else {
responseId = await apiPost<string>(
'chapter/add',
{
bookId: bookId,
wordsCount: 0,
chapterOrder: newChapterOrder ? newChapterOrder : 0,
title: newChapterTitle,
},
token,
);
const addData = {bookId, wordsCount: 0, chapterOrder: newChapterOrder ? newChapterOrder : 0, title: newChapterTitle};
responseId = await apiPost<string>('chapter/add', addData, token);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('add_chapter', addData);
}
}
if (!responseId) {
errorMessage(t("mainChapter.errorAdd"));