Fix optimistic insertion

This commit is contained in:
Eric Bailey
2025-06-10 14:27:16 -05:00
parent fcc5ced9fc
commit 63befb7e24
2 changed files with 21 additions and 23 deletions
+10 -8
View File
@@ -49,6 +49,13 @@ export function usePostThread({anchor}: {anchor?: string}) {
setView: baseSetView,
prioritizeFollowedUsers,
} = useThreadPreferences()
const below = useMemo(() => {
return view === 'linear'
? LINEAR_VIEW_BELOW
: isWeb && gtPhone
? TREE_VIEW_BELOW_DESKTOP
: TREE_VIEW_BELOW
}, [view, gtPhone])
const postThreadQueryKey = createPostThreadQueryKey({
anchor,
@@ -68,12 +75,7 @@ export function usePostThread({anchor}: {anchor?: string}) {
const {data} = await agent.app.bsky.unspecced.getPostThreadV2({
anchor: anchor!,
branchingFactor: view === 'linear' ? LINEAR_VIEW_BF : TREE_VIEW_BF,
below:
view === 'linear'
? LINEAR_VIEW_BELOW
: isWeb && gtPhone
? TREE_VIEW_BELOW_DESKTOP
: TREE_VIEW_BELOW,
below,
sort: sort,
prioritizeFollowedUsers: prioritizeFollowedUsers,
})
@@ -144,12 +146,12 @@ export function usePostThread({anchor}: {anchor?: string}) {
const mutator = useMemo(
() =>
createCacheMutator({
params: {view},
params: {view, below},
postThreadQueryKey,
postThreadOtherQueryKey,
queryClient: qc,
}),
[qc, view, postThreadQueryKey, postThreadOtherQueryKey],
[qc, view, below, postThreadQueryKey, postThreadOtherQueryKey],
)
/**
+11 -15
View File
@@ -14,7 +14,6 @@ import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '#/state/
import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/queries/post-feed'
import {findAllPostsInQueryData as findAllPostsInQuoteQueryData} from '#/state/queries/post-quotes'
import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/queries/search-posts'
import {BELOW} from '#/state/queries/usePostThread/const'
import {getBranch} from '#/state/queries/usePostThread/traversal'
import {
type ApiThreadItem,
@@ -37,7 +36,7 @@ export function createCacheMutator({
queryClient: QueryClient
postThreadQueryKey: ReturnType<typeof createPostThreadQueryKey>
postThreadOtherQueryKey: ReturnType<typeof createPostThreadOtherQueryKey>
params: Pick<PostThreadParams, 'view'>
params: Pick<PostThreadParams, 'view'> & {below: number}
}) {
return {
insertReplies(
@@ -122,20 +121,17 @@ export function createCacheMutator({
* OP insertions replace other replies _in linear view_.
*/
const itemsToRemove = shouldReplaceWithOPReplies ? branch.length : 0
const itemsToInsert = replies
.map((r, ri) => {
r.depth = existingParent.depth + 1 + ri
return r
})
.filter(r => {
// Filter out replies that are too deep for our UI
return r.depth <= params.below
})
thread.splice(
i + 1,
itemsToRemove,
...replies
.map((r, ri) => {
r.depth = existingParent.depth + 1 + ri
return r
})
.filter(r => {
// Filter out replies that are too deep for our UI
return r.depth <= BELOW
}),
)
thread.splice(i + 1, itemsToRemove, ...itemsToInsert)
}
}