Add optimisticReplyCount to post shadow

This commit is contained in:
Eric Bailey
2025-08-18 17:26:40 -05:00
parent cced762a7f
commit 5cb663ae04
2 changed files with 16 additions and 5 deletions
+8 -1
View File
@@ -24,12 +24,13 @@ export interface PostShadow {
isDeleted: boolean
embed: AppBskyEmbedRecord.View | AppBskyEmbedRecordWithMedia.View | undefined
pinned: boolean
optimisticReplyCount: number | undefined
}
export const POST_TOMBSTONE = Symbol('PostTombstone')
const emitter = new EventEmitter()
const shadows: WeakMap<
export const shadows: WeakMap<
AppBskyFeedDefs.PostView,
Partial<PostShadow>
> = new WeakMap()
@@ -95,6 +96,11 @@ function mergeShadow(
repostCount = Math.max(0, repostCount)
}
let replyCount = post.replyCount ?? 0
if ('optimisticReplyCount' in shadow) {
replyCount = shadow.optimisticReplyCount ?? replyCount
}
let embed: typeof post.embed
if ('embed' in shadow) {
if (
@@ -112,6 +118,7 @@ function mergeShadow(
embed: embed || post.embed,
likeCount: likeCount,
repostCount: repostCount,
replyCount: replyCount,
viewer: {
...(post.viewer || {}),
like: 'likeUri' in shadow ? shadow.likeUri : post.viewer?.like,
@@ -9,6 +9,7 @@ import {
} from '@atproto/api'
import {type QueryClient} from '@tanstack/react-query'
import {shadows, updatePostShadow} from '#/state/cache/post-shadow'
import {findAllPostsInQueryData as findAllPostsInExploreFeedPreviewsQueryData} from '#/state/queries/explore-feed-previews'
import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '#/state/queries/notifications/feed'
import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/queries/post-feed'
@@ -85,10 +86,13 @@ export function createCacheMutator({
/*
* Update parent data
*/
parent.value.post = {
...parent.value.post,
replyCount: (parent.value.post.replyCount || 0) + 1,
}
const shadow = shadows.get(parent.value.post)
const prevOptimisticCount = shadow?.optimisticReplyCount || 0
const prevReplyCount = parent.value.post.replyCount || 0
updatePostShadow(queryClient, parent.value.post.uri, {
// prefer optimistic count, if we already have some
optimisticReplyCount: (prevOptimisticCount || prevReplyCount) + 1,
})
const opDid = getRootPostAtUri(parent.value.post)?.host
const nextPreexistingItem = thread.at(i + 1)