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:
@@ -30,8 +30,10 @@ export default function AddNewSeriesForm({setCloseForm, onSeriesCreated}: AddNew
|
||||
const {isCurrentlyOffline} = useContext(OfflineContext);
|
||||
const {
|
||||
serverSyncedBooks,
|
||||
setServerSyncedBooks
|
||||
setServerSyncedBooks,
|
||||
localSyncedBooks
|
||||
}: BooksSyncContextProps = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const useLocal: boolean = isDesktop && isCurrentlyOffline();
|
||||
const [name, setName] = useState<string>('');
|
||||
const [description, setDescription] = useState<string>('');
|
||||
const [selectedBookIds, setSelectedBookIds] = useState<string[]>([]);
|
||||
@@ -83,6 +85,8 @@ export default function AddNewSeriesForm({setCloseForm, onSeriesCreated}: AddNew
|
||||
: book
|
||||
)
|
||||
);
|
||||
} else {
|
||||
setServerSyncedBooks((prev: SyncedBook[]): SyncedBook[] => [...prev]);
|
||||
}
|
||||
|
||||
if (onSeriesCreated) {
|
||||
@@ -150,7 +154,7 @@ export default function AddNewSeriesForm({setCloseForm, onSeriesCreated}: AddNew
|
||||
)}
|
||||
</div>
|
||||
|
||||
{serverSyncedBooks.length === 0 ? (
|
||||
{(useLocal ? localSyncedBooks : serverSyncedBooks).length === 0 ? (
|
||||
<div className="text-center py-6 text-muted">
|
||||
<Book className="w-8 h-8 mb-2 opacity-50" strokeWidth={1.75}/>
|
||||
<p>{t("addNewSeriesForm.noBooks")}</p>
|
||||
@@ -158,7 +162,7 @@ export default function AddNewSeriesForm({setCloseForm, onSeriesCreated}: AddNew
|
||||
) : (
|
||||
<div
|
||||
className="max-h-48 overflow-y-auto rounded-xl border border-secondary bg-tertiary">
|
||||
{serverSyncedBooks.map((book: SyncedBook) => {
|
||||
{(useLocal ? localSyncedBooks : serverSyncedBooks).map((book: SyncedBook) => {
|
||||
const isSelected: boolean = selectedBookIds.includes(book.id);
|
||||
return (
|
||||
<button
|
||||
|
||||
@@ -10,6 +10,10 @@ import OfflineContext, {OfflineContextType} from '@/context/OfflineContext';
|
||||
import {isDesktop} from '@/lib/configs';
|
||||
import {apiDelete} from '@/lib/api/client';
|
||||
import {deleteSeries} from '@/lib/tauri';
|
||||
import {SeriesContext, SeriesContextProps} from '@/context/SeriesContext';
|
||||
import {SeriesSyncContext, SeriesSyncContextProps} from '@/context/SeriesSyncContext';
|
||||
import {SyncedSeries} from '@/lib/types/synced-series';
|
||||
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from '@/context/SyncQueueContext';
|
||||
|
||||
interface SeriesSettingOption {
|
||||
id: string;
|
||||
@@ -36,6 +40,8 @@ export default function SeriesSettingSidebar(
|
||||
const {lang}: LangContextProps = useContext<LangContextProps>(LangContext);
|
||||
const {errorMessage, successMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
|
||||
const {isCurrentlyOffline} = useContext(OfflineContext);
|
||||
const {localSyncedSeries}: SeriesSyncContextProps = useContext<SeriesSyncContextProps>(SeriesSyncContext);
|
||||
const {addToQueue}: LocalSyncQueueContextProps = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const userToken: string = session?.accessToken ? session?.accessToken : '';
|
||||
|
||||
const [showDeleteConfirm, setShowDeleteConfirm] = useState<boolean>(false);
|
||||
@@ -44,9 +50,15 @@ export default function SeriesSettingSidebar(
|
||||
try {
|
||||
const useLocal: boolean = isDesktop && isCurrentlyOffline();
|
||||
const deletedAt: number = Math.floor(Date.now() / 1000);
|
||||
const success: boolean = useLocal
|
||||
? await deleteSeries(seriesId, deletedAt)
|
||||
: await apiDelete<boolean>('series/delete', {seriesId: seriesId}, userToken, lang);
|
||||
let success: boolean;
|
||||
if (useLocal) {
|
||||
success = await deleteSeries(seriesId, deletedAt);
|
||||
} else {
|
||||
success = await apiDelete<boolean>('series/delete', {seriesId: seriesId}, userToken, lang);
|
||||
if (isDesktop && localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('delete_series', {seriesId, deletedAt});
|
||||
}
|
||||
}
|
||||
if (success) {
|
||||
successMessage(t('seriesSetting.deleteSuccess'));
|
||||
onClose();
|
||||
|
||||
@@ -14,6 +14,9 @@ import {LangContext, LangContextProps} from "@/context/LangContext";
|
||||
import {SeriesContext, SeriesContextProps} from "@/context/SeriesContext";
|
||||
import {SeriesDetailResponse, SeriesUpdateResponse} from "@/lib/types/series";
|
||||
import PulseLoader from '@/components/ui/PulseLoader';
|
||||
import {SeriesSyncContext, SeriesSyncContextProps} from '@/context/SeriesSyncContext';
|
||||
import {SyncedSeries} from '@/lib/types/synced-series';
|
||||
import {LocalSyncQueueContext, LocalSyncQueueContextProps} from '@/context/SyncQueueContext';
|
||||
|
||||
function BasicSeriesInformation(props: object, ref: React.Ref<{ handleSave: () => Promise<void> }>) {
|
||||
const t = useTranslations();
|
||||
@@ -24,6 +27,8 @@ function BasicSeriesInformation(props: object, ref: React.Ref<{ handleSave: () =
|
||||
const userToken: string = session?.accessToken ? session?.accessToken : '';
|
||||
const {errorMessage, successMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
|
||||
const {isCurrentlyOffline} = useContext(OfflineContext);
|
||||
const {localSyncedSeries}: SeriesSyncContextProps = useContext<SeriesSyncContextProps>(SeriesSyncContext);
|
||||
const {addToQueue}: LocalSyncQueueContextProps = useContext<LocalSyncQueueContextProps>(LocalSyncQueueContext);
|
||||
const useLocal: boolean = isDesktop && isCurrentlyOffline();
|
||||
|
||||
const [isLoading, setIsLoading] = useState<boolean>(true);
|
||||
@@ -69,9 +74,16 @@ function BasicSeriesInformation(props: object, ref: React.Ref<{ handleSave: () =
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const response: SeriesUpdateResponse = useLocal
|
||||
? {success: await updateSeries({seriesId, name, description})} as SeriesUpdateResponse
|
||||
: await apiPut<SeriesUpdateResponse>('series/update', {seriesId, name, description}, userToken, lang);
|
||||
let response: SeriesUpdateResponse;
|
||||
const updateData = {seriesId, name, description};
|
||||
if (useLocal) {
|
||||
response = {success: await updateSeries(updateData)} as SeriesUpdateResponse;
|
||||
} else {
|
||||
response = await apiPut<SeriesUpdateResponse>('series/update', updateData, userToken, lang);
|
||||
if (isDesktop && localSyncedSeries.find((s: SyncedSeries): boolean => s.id === seriesId)) {
|
||||
addToQueue('update_series', updateData);
|
||||
}
|
||||
}
|
||||
if (!response.success) {
|
||||
errorMessage(t('seriesBasicInformation.error.update'));
|
||||
return;
|
||||
|
||||
@@ -29,7 +29,8 @@ function SeriesBooksManager(props: object, ref: React.Ref<{ handleSave: () => Pr
|
||||
const {seriesId}: SeriesContextProps = useContext<SeriesContextProps>(SeriesContext);
|
||||
const {
|
||||
serverSyncedBooks,
|
||||
setServerSyncedBooks
|
||||
setServerSyncedBooks,
|
||||
localSyncedBooks
|
||||
}: BooksSyncContextProps = useContext<BooksSyncContextProps>(BooksSyncContext);
|
||||
const userToken: string = session?.accessToken ? session?.accessToken : '';
|
||||
const {errorMessage, successMessage}: AlertContextProps = useContext<AlertContextProps>(AlertContext);
|
||||
@@ -49,11 +50,12 @@ function SeriesBooksManager(props: object, ref: React.Ref<{ handleSave: () => Pr
|
||||
|
||||
useEffect(function () {
|
||||
const booksInSeries: string[] = seriesBooks.map((book: SeriesBookProps) => book.bookId);
|
||||
const filteredBooks: SyncedBook[] = serverSyncedBooks.filter(
|
||||
const allBooks: SyncedBook[] = useLocal ? localSyncedBooks : serverSyncedBooks;
|
||||
const filteredBooks: SyncedBook[] = allBooks.filter(
|
||||
(book: SyncedBook) => !booksInSeries.includes(book.id)
|
||||
);
|
||||
setAvailableBooks(filteredBooks);
|
||||
}, [seriesBooks, serverSyncedBooks]);
|
||||
}, [seriesBooks, serverSyncedBooks, localSyncedBooks, useLocal]);
|
||||
|
||||
async function loadSeriesBooks(): Promise<void> {
|
||||
setIsLoading(true);
|
||||
@@ -97,7 +99,8 @@ function SeriesBooksManager(props: object, ref: React.Ref<{ handleSave: () => Pr
|
||||
: await apiPost<boolean>('series/book/add', {seriesId: seriesId, bookId: selectedBookToAdd}, userToken, lang);
|
||||
|
||||
if (response) {
|
||||
const addedBook: SyncedBook | undefined = serverSyncedBooks.find(
|
||||
const allBooks: SyncedBook[] = useLocal ? localSyncedBooks : serverSyncedBooks;
|
||||
const addedBook: SyncedBook | undefined = allBooks.find(
|
||||
(book: SyncedBook) => book.id === selectedBookToAdd
|
||||
);
|
||||
if (addedBook) {
|
||||
|
||||
Reference in New Issue
Block a user