From 601dfeb8f2ec272e7c64fc5278880f998554cdb3 Mon Sep 17 00:00:00 2001 From: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Date: Thu, 26 Jun 2025 10:15:00 +0800 Subject: [PATCH] fix: clear collection form entries fetch --- .../internal/store/src/collection/store.ts | 20 ++++++++-- packages/internal/store/src/entry/store.ts | 3 +- packages/internal/store/src/morph/hono.ts | 38 ++++++++++++------- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/packages/internal/store/src/collection/store.ts b/packages/internal/store/src/collection/store.ts index af55bdf8c..f3462486e 100644 --- a/packages/internal/store/src/collection/store.ts +++ b/packages/internal/store/src/collection/store.ts @@ -117,25 +117,37 @@ class CollectionActions implements Hydratable, Resetable { await tx.run() } - deleteInSession(entryId: string) { + deleteInSession(entryId: string | string[]) { + const normalizedEntryId = Array.isArray(entryId) ? entryId : [entryId] + const state = useCollectionStore.getState() const nextCollections: CollectionState["collections"] = { ...state.collections, } - delete nextCollections[entryId] + + normalizedEntryId.forEach((id) => { + delete nextCollections[id] + }) set({ ...state, collections: nextCollections, }) } - async delete(entryId: string) { + async delete(entryId: string | string[]) { + const entryIdsInCollection = new Set(Object.keys(get().collections)) + const normalizedEntryId = (Array.isArray(entryId) ? entryId : [entryId]).filter((id) => + entryIdsInCollection.has(id), + ) + + if (normalizedEntryId.length === 0) return + const tx = createTransaction() tx.store(() => { this.deleteInSession(entryId) }) tx.persist(() => { - return CollectionService.delete(entryId) + return CollectionService.deleteMany(normalizedEntryId) }) tx.run() } diff --git a/packages/internal/store/src/entry/store.ts b/packages/internal/store/src/entry/store.ts index 830d57a32..555eb57f7 100644 --- a/packages/internal/store/src/entry/store.ts +++ b/packages/internal/store/src/entry/store.ts @@ -490,10 +490,11 @@ class EntrySyncServices { await entryActions.upsertMany(entries) if (typeof view === "number") { - const collections = honoMorph.toCollections(res.data, view) + const { collections, entryIdsNotInCollections } = honoMorph.toCollections(res.data, view) await collectionActions.upsertMany(collections, { reset: params.isCollection && !pageParam, }) + await collectionActions.delete(entryIdsNotInCollections) } const dataFeeds = res.data?.map((e) => e.feeds).filter((f) => f.type === "feed") diff --git a/packages/internal/store/src/morph/hono.ts b/packages/internal/store/src/morph/hono.ts index f82937ebd..be9bf2e20 100644 --- a/packages/internal/store/src/morph/hono.ts +++ b/packages/internal/store/src/morph/hono.ts @@ -148,21 +148,31 @@ class Morph { toCollections( data: HonoApiClient.Entry_Post | HonoApiClient.Entry_Inbox_Post | undefined, view: FeedViewType, - ): CollectionModel[] { - if (!data) return [] satisfies CollectionModel[] - return data - .map((item) => { - if (!item.collections) { - return null - } - return { - createdAt: item.collections.createdAt, - entryId: item.entries.id, - feedId: item.feeds.id, - view, - } satisfies CollectionModel + ): { + collections: CollectionModel[] + entryIdsNotInCollections: string[] + } { + if (!data) return { collections: [], entryIdsNotInCollections: [] } + + const collections: CollectionModel[] = [] + const entryIdsNotInCollections: string[] = [] + for (const item of data) { + if (!item.collections) { + entryIdsNotInCollections.push(item.entries.id) + continue + } + collections.push({ + createdAt: item.collections.createdAt, + entryId: item.entries.id, + feedId: item.feeds.id, + view, }) - .filter((i) => i !== null) + } + + return { + collections, + entryIdsNotInCollections, + } } toEntry(data?: HonoApiClient.Entry_Get | HonoApiClient.Entry_Inbox_Get): EntryModel | null {