From 5cb663ae04f72b6fd0fbd72c081102420ce2f032 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Mon, 18 Aug 2025 17:26:40 -0500 Subject: [PATCH] Add optimisticReplyCount to post shadow --- src/state/cache/post-shadow.ts | 9 ++++++++- src/state/queries/usePostThread/queryCache.ts | 12 ++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/state/cache/post-shadow.ts b/src/state/cache/post-shadow.ts index d7f1eb8b93..8693a6dbcf 100644 --- a/src/state/cache/post-shadow.ts +++ b/src/state/cache/post-shadow.ts @@ -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 > = 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, diff --git a/src/state/queries/usePostThread/queryCache.ts b/src/state/queries/usePostThread/queryCache.ts index 826932349d..4e286decbd 100644 --- a/src/state/queries/usePostThread/queryCache.ts +++ b/src/state/queries/usePostThread/queryCache.ts @@ -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)