fix: 登录同步时以远端数据为准,避免种子笔记污染云端
远端有数据时直接覆盖本地,不再合并本地 IndexedDB 种子数据; 仅远端为空(首次激活云同步)时才上传本地数据。 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b404a81003
commit
e7e3e2bb47
@ -2,7 +2,7 @@ import { create } from 'zustand'
|
|||||||
import { db, type Note, type Folder } from '../db'
|
import { db, type Note, type Folder } from '../db'
|
||||||
import { generateId, extractTextFromJSON } from '../lib/utils'
|
import { generateId, extractTextFromJSON } from '../lib/utils'
|
||||||
import { getToken, clearToken, parseToken, apiGetMe, type CurrentUser } from '../lib/auth'
|
import { getToken, clearToken, parseToken, apiGetMe, type CurrentUser } from '../lib/auth'
|
||||||
import { pullAll, pushAll, pushNote, pushFolder, pushDeleteNote, pushDeleteFolder, mergeNotes, mergeFolders } from '../lib/sync'
|
import { pullAll, pushAll, pushNote, pushFolder, pushDeleteNote, pushDeleteFolder } from '../lib/sync'
|
||||||
|
|
||||||
interface AppState {
|
interface AppState {
|
||||||
notes: Note[]
|
notes: Note[]
|
||||||
@ -285,12 +285,21 @@ export const useAppStore = create<AppState>((set, get) => ({
|
|||||||
set({ syncStatus: 'syncing' })
|
set({ syncStatus: 'syncing' })
|
||||||
try {
|
try {
|
||||||
const remote = await pullAll()
|
const remote = await pullAll()
|
||||||
const mergedNotes = mergeNotes(notes, remote.notes)
|
// 远端有数据时以远端为准,避免本地种子数据污染云端
|
||||||
const mergedFolders = mergeFolders(folders, remote.folders)
|
// 只有远端完全为空(首次激活)才把本地数据上传
|
||||||
await Promise.all(mergedNotes.map((n: any) => db.notes.put(n)))
|
if (remote.notes.length > 0 || remote.folders.length > 0) {
|
||||||
await Promise.all(mergedFolders.map((f: any) => db.folders.put(f)))
|
await db.notes.clear()
|
||||||
await pushAll({ notes: mergedNotes, folders: mergedFolders })
|
await db.folders.clear()
|
||||||
set({ notes: mergedNotes, folders: mergedFolders, syncStatus: 'idle', _notesVersion: get()._notesVersion + 1, _filteredCache: null })
|
await Promise.all(remote.notes.map((n: any) => db.notes.put({ ...n, tags: Array.isArray(n.tags) ? n.tags : JSON.parse(n.tags as any) })))
|
||||||
|
await Promise.all(remote.folders.map((f: any) => db.folders.put(f)))
|
||||||
|
set({ notes: remote.notes.map((n: any) => ({ ...n, tags: Array.isArray(n.tags) ? n.tags : JSON.parse(n.tags as any) })), folders: remote.folders, syncStatus: 'idle', _notesVersion: get()._notesVersion + 1, _filteredCache: null })
|
||||||
|
} else {
|
||||||
|
// 远端为空:首次激活,把本地数据推上去
|
||||||
|
const localNotes = notes.filter(n => n.deletedAt === null)
|
||||||
|
const localFolders = folders
|
||||||
|
await pushAll({ notes: localNotes, folders: localFolders })
|
||||||
|
set({ syncStatus: 'idle' })
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
set({ syncStatus: 'error' })
|
set({ syncStatus: 'error' })
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user