diff --git a/apps/readest-app/src/app/reader/hooks/useNotesSync.ts b/apps/readest-app/src/app/reader/hooks/useNotesSync.ts index 13fcc768..925681f9 100644 --- a/apps/readest-app/src/app/reader/hooks/useNotesSync.ts +++ b/apps/readest-app/src/app/reader/hooks/useNotesSync.ts @@ -1,6 +1,7 @@ import { useEffect, useRef } from 'react'; import { useAuth } from '@/context/AuthContext'; import { useSync } from '@/hooks/useSync'; +import { BookNote } from '@/types/book'; import { useBookDataStore } from '@/store/bookDataStore'; import { SYNC_NOTES_INTERVAL_SEC } from '@/services/constants'; @@ -12,12 +13,6 @@ export const useNotesSync = (bookKey: string) => { const config = getConfig(bookKey); const bookHash = bookKey.split('-')[0]!; - useEffect(() => { - if (!user) return; - syncNotes([], bookHash, 'pull'); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); - const lastSyncTime = useRef(0); const syncTimeoutRef = useRef | null>(null); @@ -57,13 +52,25 @@ export const useNotesSync = (bookKey: string) => { }, [config]); useEffect(() => { - if (syncedNotes?.length && config?.location) { + const processNewNote = (note: BookNote) => { + const oldNotes = config?.booknotes ?? []; + const existingNote = oldNotes.find((oldNote) => oldNote.id === note.id); + if (existingNote) { + if (existingNote.updatedAt < note.updatedAt) { + return { ...existingNote, ...note }; + } else { + return { ...note, ...existingNote }; + } + } + return note; + }; + if (syncedNotes?.length && config) { const newNotes = syncedNotes.filter((note) => note.bookHash === bookHash); if (!newNotes.length) return; const oldNotes = config.booknotes ?? []; const mergedNotes = [ ...oldNotes.filter((oldNote) => !newNotes.some((newNote) => newNote.id === oldNote.id)), - ...newNotes, + ...newNotes.map(processNewNote), ]; setConfig(bookKey, { ...config, booknotes: mergedNotes }); } diff --git a/apps/readest-app/src/pages/api/sync.ts b/apps/readest-app/src/pages/api/sync.ts index f6aee57a..47f3326e 100644 --- a/apps/readest-app/src/pages/api/sync.ts +++ b/apps/readest-app/src/pages/api/sync.ts @@ -180,8 +180,6 @@ export async function POST(req: NextRequest) { clientDeletedAt > serverDeletedAt || clientUpdatedAt > serverUpdatedAt; if (clientIsNewer) { - // use server updated_at for updated records - dbRec.updated_at = new Date().toISOString(); const { data: updated, error: updateError } = await supabase .from(table) .update(dbRec)