fix: clear collection form entries fetch

This commit is contained in:
Stephen Zhou 2025-06-26 10:15:00 +08:00
parent fbd0b3e08f
commit 601dfeb8f2
No known key found for this signature in database
3 changed files with 42 additions and 19 deletions

View File

@ -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()
}

View File

@ -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")

View File

@ -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 {