From 7cae1e1e4b2560255e3bc2439d7d9c380af72330 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 19 Aug 2025 16:00:49 -0500 Subject: [PATCH] Fix subtle bug in query cache --- src/state/queries/usePostThread/queryCache.ts | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/state/queries/usePostThread/queryCache.ts b/src/state/queries/usePostThread/queryCache.ts index 2c1fd603b0..5e27ebb875 100644 --- a/src/state/queries/usePostThread/queryCache.ts +++ b/src/state/queries/usePostThread/queryCache.ts @@ -90,11 +90,25 @@ export function createCacheMutator({ * Update parent data */ const shadow = dangerousGetPostShadow(parent.value.post) - const prevOptimisticCount = shadow?.optimisticReplyCount || 0 - const prevReplyCount = parent.value.post.replyCount || 0 + const prevOptimisticCount = shadow?.optimisticReplyCount + const prevReplyCount = parent.value.post.replyCount + // prefer optimistic count, if we already have some + const currentReplyCount = + (prevOptimisticCount ?? prevReplyCount ?? 0) + 1 + + /* + * We must update the value in the query cache in order for thread + * traversal to properly compute required metadata. + */ + parent.value.post.replyCount = currentReplyCount + + /** + * Additionally, we need to update the post shadow to keep track of + * these new values, since mutating the post object above does not + * cause a re-render. + */ updatePostShadow(queryClient, parent.value.post.uri, { - // prefer optimistic count, if we already have some - optimisticReplyCount: (prevOptimisticCount || prevReplyCount) + 1, + optimisticReplyCount: currentReplyCount, }) const opDid = getRootPostAtUri(parent.value.post)?.host