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, setView: baseSetView,
prioritizeFollowedUsers, prioritizeFollowedUsers,
} = useThreadPreferences() } = useThreadPreferences()
const below = useMemo(() => {
return view === 'linear'
? LINEAR_VIEW_BELOW
: isWeb && gtPhone
? TREE_VIEW_BELOW_DESKTOP
: TREE_VIEW_BELOW
}, [view, gtPhone])
const postThreadQueryKey = createPostThreadQueryKey({ const postThreadQueryKey = createPostThreadQueryKey({
anchor, anchor,
@@ -68,12 +75,7 @@ export function usePostThread({anchor}: {anchor?: string}) {
const {data} = await agent.app.bsky.unspecced.getPostThreadV2({ const {data} = await agent.app.bsky.unspecced.getPostThreadV2({
anchor: anchor!, anchor: anchor!,
branchingFactor: view === 'linear' ? LINEAR_VIEW_BF : TREE_VIEW_BF, branchingFactor: view === 'linear' ? LINEAR_VIEW_BF : TREE_VIEW_BF,
below: below,
view === 'linear'
? LINEAR_VIEW_BELOW
: isWeb && gtPhone
? TREE_VIEW_BELOW_DESKTOP
: TREE_VIEW_BELOW,
sort: sort, sort: sort,
prioritizeFollowedUsers: prioritizeFollowedUsers, prioritizeFollowedUsers: prioritizeFollowedUsers,
}) })
@@ -144,12 +146,12 @@ export function usePostThread({anchor}: {anchor?: string}) {
const mutator = useMemo( const mutator = useMemo(
() => () =>
createCacheMutator({ createCacheMutator({
params: {view}, params: {view, below},
postThreadQueryKey, postThreadQueryKey,
postThreadOtherQueryKey, postThreadOtherQueryKey,
queryClient: qc, queryClient: qc,
}), }),
[qc, view, postThreadQueryKey, postThreadOtherQueryKey], [qc, view, below, postThreadQueryKey, postThreadOtherQueryKey],
) )
/** /**
+6 -10
View File
@@ -14,7 +14,6 @@ import {findAllPostsInQueryData as findAllPostsInNotifsQueryData} from '#/state/
import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/queries/post-feed' import {findAllPostsInQueryData as findAllPostsInFeedQueryData} from '#/state/queries/post-feed'
import {findAllPostsInQueryData as findAllPostsInQuoteQueryData} from '#/state/queries/post-quotes' import {findAllPostsInQueryData as findAllPostsInQuoteQueryData} from '#/state/queries/post-quotes'
import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/queries/search-posts' import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/queries/search-posts'
import {BELOW} from '#/state/queries/usePostThread/const'
import {getBranch} from '#/state/queries/usePostThread/traversal' import {getBranch} from '#/state/queries/usePostThread/traversal'
import { import {
type ApiThreadItem, type ApiThreadItem,
@@ -37,7 +36,7 @@ export function createCacheMutator({
queryClient: QueryClient queryClient: QueryClient
postThreadQueryKey: ReturnType<typeof createPostThreadQueryKey> postThreadQueryKey: ReturnType<typeof createPostThreadQueryKey>
postThreadOtherQueryKey: ReturnType<typeof createPostThreadOtherQueryKey> postThreadOtherQueryKey: ReturnType<typeof createPostThreadOtherQueryKey>
params: Pick<PostThreadParams, 'view'> params: Pick<PostThreadParams, 'view'> & {below: number}
}) { }) {
return { return {
insertReplies( insertReplies(
@@ -122,20 +121,17 @@ export function createCacheMutator({
* OP insertions replace other replies _in linear view_. * OP insertions replace other replies _in linear view_.
*/ */
const itemsToRemove = shouldReplaceWithOPReplies ? branch.length : 0 const itemsToRemove = shouldReplaceWithOPReplies ? branch.length : 0
const itemsToInsert = replies
thread.splice(
i + 1,
itemsToRemove,
...replies
.map((r, ri) => { .map((r, ri) => {
r.depth = existingParent.depth + 1 + ri r.depth = existingParent.depth + 1 + ri
return r return r
}) })
.filter(r => { .filter(r => {
// Filter out replies that are too deep for our UI // Filter out replies that are too deep for our UI
return r.depth <= BELOW return r.depth <= params.below
}), })
)
thread.splice(i + 1, itemsToRemove, ...itemsToInsert)
} }
} }