fix: lists and list items deleted on another device are now deleted locally

This commit is contained in:
2026-08-21 22:53:18 +02:00
parent b3ee69686e
commit 61d1b84fff
2 changed files with 83 additions and 0 deletions
+31
View File
@@ -369,6 +369,7 @@ export const useListsStore = defineStore('lists', () => {
try {
const serverLists = await getListsApi()
const serverListIds = new Set(serverLists.map((serverList) => serverList.id))
for (const serverList of serverLists) {
const existingList = await db.lists.get(serverList.id)
@@ -376,6 +377,23 @@ export const useListsStore = defineStore('lists', () => {
await db.lists.put({ ...serverList, pendingSync: false })
}
}
// Lists that were already synced but are no longer reported by the
// server have been deleted there (e.g. from another device), so
// remove them locally too, along with their items. Lists that still
// have pending local changes (not yet synced, e.g. a not-yet-pushed
// "createList") are left alone since the server doesn't know about
// them yet.
const localLists = await db.lists.toArray()
for (const localList of localLists) {
if (!localList.pendingSync && !serverListIds.has(localList.id)) {
await db.lists.delete(localList.id)
const orphanedItems = await db.listItems.where('list_id').equals(localList.id).toArray()
for (const item of orphanedItems) {
await db.listItems.delete(item.id)
}
}
}
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to load lists from server'
} finally {
@@ -391,12 +409,25 @@ export const useListsStore = defineStore('lists', () => {
try {
const serverItems = await getListItemsApi(listId)
const serverItemIds = new Set(serverItems.map((serverItem) => serverItem.id))
for (const serverItem of serverItems) {
const existingItem = await db.listItems.get(serverItem.id)
if (!existingItem || !existingItem.pendingSync) {
await db.listItems.put({ ...serverItem, pendingSync: false })
}
}
// Items that were already synced but are no longer reported by the
// server for this list have been deleted there, so remove them
// locally too. Items with pending local changes are left alone since
// the server doesn't know about them yet.
const localItems = await db.listItems.where('list_id').equals(listId).toArray()
for (const localItem of localItems) {
if (!localItem.pendingSync && !serverItemIds.has(localItem.id)) {
await db.listItems.delete(localItem.id)
}
}
} catch (err) {
error.value = err instanceof Error ? err.message : 'Failed to load list items from server'
} finally {