From 8bab738fedae4f2f9e8ed970a16d77b16259fb99 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Tue, 20 May 2025 12:39:30 -0500 Subject: [PATCH] Simple working version, no pref for OP --- src/state/queries/usePostThread/index.ts | 2 +- src/state/queries/usePostThread/queryCache.ts | 42 ++++--- src/state/queries/usePostThread/traversal.ts | 114 +++++++++--------- 3 files changed, 86 insertions(+), 72 deletions(-) diff --git a/src/state/queries/usePostThread/index.ts b/src/state/queries/usePostThread/index.ts index 68c413cf95..8b9f786cc0 100644 --- a/src/state/queries/usePostThread/index.ts +++ b/src/state/queries/usePostThread/index.ts @@ -86,9 +86,9 @@ export function usePostThread({ showHidden: state.shownHiddenReplyKinds.has(HiddenReplyKind.Hidden), }, ) - console.log(items) const mutator = createCacheMutator({ + params, queryKey, queryClient: qc, }) diff --git a/src/state/queries/usePostThread/queryCache.ts b/src/state/queries/usePostThread/queryCache.ts index 6353f82b2d..c7d39b31bf 100644 --- a/src/state/queries/usePostThread/queryCache.ts +++ b/src/state/queries/usePostThread/queryCache.ts @@ -9,6 +9,7 @@ import {findAllPostsInQueryData as findAllPostsInSearchQueryData} from '#/state/ import { type createPostThreadQueryKey, postThreadQueryKeyRoot, + type PostThreadParams, } from '#/state/queries/usePostThread/types' import { embedViewToThreadPlaceholder, @@ -17,9 +18,11 @@ import { import {didOrHandleUriMatches, getEmbeddedPost} from '#/state/queries/util' export function createCacheMutator({ + params, queryKey, queryClient, }: { + params: PostThreadParams queryKey: ReturnType queryClient: QueryClient }) { @@ -55,20 +58,31 @@ export function createCacheMutator({ replyCount: parent.value.post.replyCount, } - /* - * Splice in new replies - */ - for (let ri = 0; ri < replies.length; ri++) { - const reply = replies[ri] - if ( - !AppBskyUnspeccedGetPostThreadV2.isThreadItemPost(reply.value) - ) - continue - const insertIndex = i + 1 + ri - thread.splice(insertIndex, 0, { - ...reply, - depth: existingParent.depth + 1 + ri, - }) + const nextItem = thread.at(i + 1) + const isReplyToRoot = existingParent.depth === 0 + const isEndOfReplyChain = + !nextItem || nextItem.depth <= existingParent.depth + const shouldInsertReplies = + isReplyToRoot || + params.view === 'tree' || + (params.view === 'linear' && isEndOfReplyChain) + + if (shouldInsertReplies) { + /* + * Splice in new replies + */ + for (let ri = 0; ri < replies.length; ri++) { + const reply = replies[ri] + if ( + !AppBskyUnspeccedGetPostThreadV2.isThreadItemPost(reply.value) + ) + continue + const insertIndex = i + 1 + ri + thread.splice(insertIndex, 0, { + ...reply, + depth: existingParent.depth + 1 + ri, + }) + } } } diff --git a/src/state/queries/usePostThread/traversal.ts b/src/state/queries/usePostThread/traversal.ts index ffe749b50e..d8eb14d7a0 100644 --- a/src/state/queries/usePostThread/traversal.ts +++ b/src/state/queries/usePostThread/traversal.ts @@ -204,66 +204,66 @@ export function sort( /* * Not moderated, probably need to insert it */ - // items.push(post) - if (view === 'tree') { - items.push(post) - } else { - if (post.depth > 1) { - const maybeNextSiblingIndex = getBranch(thread, i, post.depth).end + 1 - const maybeNextSibling = thread.at(maybeNextSiblingIndex) + items.push(post) + // if (view === 'tree') { + // items.push(post) + // } else { + // if (post.depth > 1) { + // const maybeNextSiblingIndex = getBranch(thread, i, post.depth).end + 1 + // const maybeNextSibling = thread.at(maybeNextSiblingIndex) - /* - * If we've got two replies at the same depth, we need to decide - * which one to show. - */ - if (post.depth === maybeNextSibling?.depth) { - // continue with next sibling - i = maybeNextSiblingIndex - 1 - debugger; - continue traversal - } else { - const maybePrevSiblingIndex = getBranchUp(thread, i - 1, post.depth).end - const maybePrevSibling = thread.at(maybePrevSiblingIndex) + // /* + // * If we've got two replies at the same depth, we need to decide + // * which one to show. + // */ + // if (post.depth === maybeNextSibling?.depth) { + // // continue with next sibling + // i = maybeNextSiblingIndex - 1 + // debugger; + // continue traversal + // } else { + // const maybePrevSiblingIndex = getBranchUp(thread, i - 1, post.depth).end + // const maybePrevSibling = thread.at(maybePrevSiblingIndex) - if (post.depth === maybePrevSibling?.depth) { - const post = views.threadPost({ - uri: item.uri, - depth: item.depth, - value: item.value, - oneUp: thread.at(maybePrevSiblingIndex - 1), - oneDown, - moderationOpts, - }) - debugger; - items.push(post) - } else { - items.push(post) - } - } + // if (post.depth === maybePrevSibling?.depth) { + // const post = views.threadPost({ + // uri: item.uri, + // depth: item.depth, + // value: item.value, + // oneUp: thread.at(maybePrevSiblingIndex - 1), + // oneDown, + // moderationOpts, + // }) + // debugger; + // items.push(post) + // } else { + // items.push(post) + // } + // } - /* - if (post.depth === oneDown?.depth) { - const opDid = new AtUri(post.value.post.record.reply?.root?.uri).host - const postAuthorDid = new AtUri(post.uri).host - debugger; - if (postAuthorDid === opDid) { - // prioritize OP optimistic reply - items.push(post) - // skip next reply - i = getBranch(thread, i, oneDown.depth).end - debugger; - continue traversal - } else { - i = getBranch(thread, i, post.depth).end - debugger; - continue traversal - } - } - */ - } else { - items.push(post) - } - } + // /* + // if (post.depth === oneDown?.depth) { + // const opDid = new AtUri(post.value.post.record.reply?.root?.uri).host + // const postAuthorDid = new AtUri(post.uri).host + // debugger; + // if (postAuthorDid === opDid) { + // // prioritize OP optimistic reply + // items.push(post) + // // skip next reply + // i = getBranch(thread, i, oneDown.depth).end + // debugger; + // continue traversal + // } else { + // i = getBranch(thread, i, post.depth).end + // debugger; + // continue traversal + // } + // } + // */ + // } else { + // items.push(post) + // } + // } } else { /* * Moderated in some way, we're going to walk children