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

@@ -6,6 +6,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 Act({acts, setActs, mainChapters}: ActProps) {
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;
@@ -72,10 +77,12 @@ export default function Act({acts, setActs, mainChapters}: ActProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
incidentId = await tauri.addIncident(bookId ?? '', newIncidentTitle);
} else {
incidentId = await apiPost<string>('book/incident/new', {
bookId,
name: newIncidentTitle,
}, token, lang);
const addData = {bookId, name: newIncidentTitle};
incidentId = await apiPost<string>('book/incident/new', addData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('add_incident', addData);
}
}
if (!incidentId) {
errorMessage(t('errorAddIncident'));
@@ -114,10 +121,12 @@ export default function Act({acts, setActs, mainChapters}: ActProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
response = await tauri.removeIncident(bookId ?? '', incidentId, Date.now());
} else {
response = await apiDelete<boolean>('book/incident/remove', {
bookId,
incidentId,
}, token, lang);
const deleteData = {bookId, incidentId};
response = await apiDelete<boolean>('book/incident/remove', deleteData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('remove_incident', {...deleteData, deletedAt: Date.now()});
}
}
if (!response) {
errorMessage(t('errorDeleteIncident'));
@@ -151,11 +160,12 @@ export default function Act({acts, setActs, mainChapters}: ActProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
plotId = await tauri.addPlotPoint(bookId ?? '', newPlotPointTitle, selectedIncidentId);
} else {
plotId = await apiPost<string>('book/plot/new', {
bookId,
name: newPlotPointTitle,
incidentId: selectedIncidentId,
}, token, lang);
const plotData = {bookId, name: newPlotPointTitle, incidentId: selectedIncidentId};
plotId = await apiPost<string>('book/plot/new', plotData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('add_plot_point', plotData);
}
}
if (!plotId) {
errorMessage(t('errorAddPlotPoint'));
@@ -195,9 +205,12 @@ export default function Act({acts, setActs, mainChapters}: ActProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
response = await tauri.removePlotPoint(plotPointId, bookId ?? '', Date.now());
} else {
response = await apiDelete<boolean>('book/plot/remove', {
plotId: plotPointId,
}, token, lang);
const deleteData = {plotId: plotPointId};
response = await apiDelete<boolean>('book/plot/remove', deleteData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('remove_plot_point', {...deleteData, bookId, deletedAt: Date.now()});
}
}
if (!response) {
errorMessage(t('errorDeletePlotPoint'));
@@ -246,13 +259,12 @@ export default function Act({acts, setActs, mainChapters}: ActProps) {
incidentId: destination === 'incident' ? itemId : undefined,
});
} else {
linkId = await apiPost<string>('chapter/resume/add', {
bookId,
chapterId: chapterId,
actId: actId,
plotId: destination === 'plotPoint' ? itemId : null,
incidentId: destination === 'incident' ? itemId : null,
}, token, lang);
const linkData = {bookId, chapterId, actId, plotId: destination === 'plotPoint' ? itemId : null, incidentId: destination === 'incident' ? itemId : null};
linkId = await apiPost<string>('chapter/resume/add', linkData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('add_chapter_information', linkData);
}
}
if (!linkId) {
errorMessage(t('errorLinkChapter'));
@@ -332,9 +344,12 @@ export default function Act({acts, setActs, mainChapters}: ActProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
response = await tauri.removeChapterInformation(chapterInfoId, bookId ?? '', Date.now());
} else {
response = await apiDelete<boolean>('chapter/resume/remove', {
chapterInfoId,
}, token, lang);
const removeData = {chapterInfoId};
response = await apiDelete<boolean>('chapter/resume/remove', removeData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('remove_chapter_information', {...removeData, bookId, deletedAt: Date.now()});
}
}
if (!response) {
errorMessage(t('errorUnlinkChapter'));

View File

@@ -6,6 +6,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';
@@ -27,6 +30,8 @@ export default function Issues({issues, setIssues}: IssuesProps) {
const {session}: SessionContextProps = useContext<SessionContextProps>(SessionContext);
const {errorMessage}: 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;
@@ -43,10 +48,12 @@ export default function Issues({issues, setIssues}: IssuesProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
issueId = await tauri.addIssue(bookId ?? '', newIssueName);
} else {
issueId = await apiPost<string>('book/issue/add', {
bookId,
name: newIssueName,
}, token, lang);
const addData = {bookId, name: newIssueName};
issueId = await apiPost<string>('book/issue/add', addData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('add_issue', addData);
}
}
if (!issueId) {
errorMessage(t("issues.errorAdd"));
@@ -79,15 +86,12 @@ export default function Issues({issues, setIssues}: IssuesProps) {
if (isDesktop && (isCurrentlyOffline() || book?.localBook)) {
response = await tauri.removeIssue(bookId ?? '', issueId, Date.now());
} else {
response = await apiDelete<boolean>(
'book/issue/remove',
{
bookId,
issueId,
},
token,
lang
);
const deleteData = {bookId, issueId};
response = await apiDelete<boolean>('book/issue/remove', deleteData, token, lang);
if (isDesktop && bookId && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('remove_issue', {...deleteData, deletedAt: Date.now()});
}
}
if (response) {
const updatedIssues: Issue[] = issues.filter((issue: Issue): boolean => issue.id !== issueId,);

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"));

View File

@@ -8,6 +8,9 @@ import {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 {Act as ActType, Incident, Issue, PlotPoint} from '@/lib/types/book';
import {ActChapter, ChapterListProps} from '@/lib/types/chapter';
import MainChapter from "@/components/book/settings/story/MainChapter";
@@ -51,6 +54,8 @@ export function Story(_props: object, ref: React.ForwardedRef<SettingRef>): Reac
const userToken: string = session.accessToken;
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 [acts, setActs] = useState<ActType[]>([]);
const [issues, setIssues] = useState<Issue[]>([]);
@@ -138,12 +143,12 @@ export function Story(_props: object, ref: React.ForwardedRef<SettingRef>): Reac
issues,
});
} else {
response = await apiPost<boolean>('book/story', {
bookId,
acts,
mainChapters,
issues,
}, userToken, lang);
const storyData = {bookId, acts, mainChapters, issues};
response = await apiPost<boolean>('book/story', storyData, userToken, lang);
if (isDesktop && localSyncedBooks.find((sb: SyncedBook): boolean => sb.id === bookId)) {
addToQueue('update_book_story', storyData);
}
}
if (!response) {
errorMessage(t("story.errorSave"))