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:
@@ -4,6 +4,9 @@ import {apiDelete, apiGet, 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 {AlertContext, AlertContextProps} from "@/context/AlertContext";
|
||||
import {ChapterContext, ChapterContextProps} from "@/context/ChapterContext";
|
||||
@@ -26,6 +29,8 @@ export default function ScribeChapterComponent() {
|
||||
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
|
||||
const userToken: string = session?.accessToken ? session?.accessToken : '';
|
||||
const {isCurrentlyOffline}: OfflineContextType = useContext<OfflineContextType>(OfflineContext);
|
||||
const {addToQueue}: LocalSyncQueueContextProps = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const {localSyncedBooks}: BooksSyncContextProps = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const router = useRouter();
|
||||
|
||||
const [chapters, setChapters] = useState<ChapterListProps[]>([])
|
||||
@@ -107,11 +112,12 @@ export default function ScribeChapterComponent() {
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
response = await tauri.updateChapter(chapterId, title, chapterOrder);
|
||||
} else {
|
||||
response = await apiPost<boolean>('chapter/update', {
|
||||
chapterId: chapterId,
|
||||
chapterOrder: chapterOrder,
|
||||
title: title,
|
||||
}, userToken, lang);
|
||||
const updateData = {chapterId, chapterOrder, title};
|
||||
response = await apiPost<boolean>('chapter/update', updateData, userToken, lang);
|
||||
|
||||
if (isDesktop && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === book?.bookId)) {
|
||||
addToQueue('update_chapter', updateData);
|
||||
}
|
||||
}
|
||||
if (!response) {
|
||||
errorMessage(t("scribeChapterComponent.errorChapterUpdate"));
|
||||
@@ -148,9 +154,12 @@ export default function ScribeChapterComponent() {
|
||||
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
|
||||
response = await tauri.removeChapter(removeChapterId, book?.bookId ?? '', Date.now());
|
||||
} else {
|
||||
response = await apiDelete<boolean>('chapter/remove', {
|
||||
chapterId: removeChapterId,
|
||||
}, userToken, lang);
|
||||
const deleteData = {bookId: book?.bookId, chapterId: removeChapterId};
|
||||
response = await apiDelete<boolean>('chapter/remove', deleteData, userToken, lang);
|
||||
|
||||
if (isDesktop && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === book?.bookId)) {
|
||||
addToQueue('remove_chapter', {...deleteData, deletedAt: Date.now()});
|
||||
}
|
||||
}
|
||||
if (!response) {
|
||||
errorMessage(t("scribeChapterComponent.errorChapterDelete"));
|
||||
@@ -184,11 +193,12 @@ export default function ScribeChapterComponent() {
|
||||
chapterOrder: chapterOrder,
|
||||
});
|
||||
} else {
|
||||
chapterId = await apiPost<string>('chapter/add', {
|
||||
bookId: book?.bookId,
|
||||
chapterOrder: chapterOrder,
|
||||
title: chapterTitle
|
||||
}, userToken, lang);
|
||||
const addData = {bookId: book?.bookId, chapterOrder, title: chapterTitle};
|
||||
chapterId = await apiPost<string>('chapter/add', addData, userToken, lang);
|
||||
|
||||
if (isDesktop && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === book?.bookId)) {
|
||||
addToQueue('add_chapter', addData);
|
||||
}
|
||||
}
|
||||
if (!chapterId) {
|
||||
errorMessage(t("scribeChapterComponent.errorChapterSubmit", {chapterName: newChapterName}));
|
||||
|
||||
Reference in New Issue
Block a user