diff --git a/src/state/queries/usePostThread/traversal.ts b/src/state/queries/usePostThread/traversal.ts index 2e7693fab3..f4e6e39c28 100644 --- a/src/state/queries/usePostThread/traversal.ts +++ b/src/state/queries/usePostThread/traversal.ts @@ -193,7 +193,9 @@ export function sortAndAnnotateThreadItems( storeTraversalMetadata(metadatas, childMetadata) if (childParentMetadata) { /* - * Set this value before incrementing the parent's repliesIndexCounter + * Set this value before incrementing the parent's + * `repliesIndexCounter`, since `repliesIndexCounter` is + * 1-indexed and `replyIndex` is 0-indexed. */ childMetadata!.replyIndex = childParentMetadata.repliesIndexCounter @@ -273,6 +275,14 @@ export function sortAndAnnotateThreadItems( metadata.replyIndex === metadata.parentMetadata.repliesIndexCounter - 1 + /** + * Item is also the last "sibling" if its index matches the total + * number of replies we're actually able to render to the page. + */ + const isLastSiblingDueToMissingReplies = + metadata.replyIndex === + metadata.parentMetadata.repliesSeenCounter - 1 + /* * Item can also be the last "sibling" if we know we don't have a * next item, OR if that next item's depth is less than this item's @@ -280,6 +290,7 @@ export function sortAndAnnotateThreadItems( * item). */ const isImplicitlyLastSibling = + nextItem === undefined || metadata.nextItemDepth === undefined || metadata.nextItemDepth < metadata.depth @@ -287,7 +298,9 @@ export function sortAndAnnotateThreadItems( * Ok now we can set the last sibling state. */ metadata.isLastSibling = - isLastSiblingByCounts || isImplicitlyLastSibling + isLastSiblingByCounts || + isImplicitlyLastSibling || + isLastSiblingDueToMissingReplies /* * Item is the last "child" in a branch if there is no next item, @@ -296,6 +309,7 @@ export function sortAndAnnotateThreadItems( * of this item) */ metadata.isLastChild = + nextItem === undefined || metadata.nextItemDepth === undefined || metadata.nextItemDepth <= metadata.depth diff --git a/src/state/queries/usePostThread/types.ts b/src/state/queries/usePostThread/types.ts index 5df7c2e42e..926f223b68 100644 --- a/src/state/queries/usePostThread/types.ts +++ b/src/state/queries/usePostThread/types.ts @@ -200,12 +200,20 @@ export type TraversalMetadata = { * * After traversal, we can use this to calculate if we actually got all the * replies we expected, or if some were blocked, etc. + * + * Because this value is incremented startging from 0, it is 1-indexed. So to + * when comparing to the `replyIndex`, you'll need to subtract 1 from this + * value. */ repliesSeenCounter: number /** * The total number of replies to this post hydrated in this response. Used * for populating the `replyIndex` of the post by referencing this value on * the parent. + * + * Because this value is incremented startging from 0, it is 1-indexed. So to + * when comparing to the `replyIndex`, you'll need to subtract 1 from this + * value. */ repliesIndexCounter: number /**