From cae42f999d0911c8259ef2b9f03fdc3526fe74d6 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Mon, 16 Jun 2025 10:24:52 -0500 Subject: [PATCH] Handle edge case where last sibling is moderated --- src/state/queries/usePostThread/traversal.ts | 30 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/state/queries/usePostThread/traversal.ts b/src/state/queries/usePostThread/traversal.ts index 10ae693c77..124591125f 100644 --- a/src/state/queries/usePostThread/traversal.ts +++ b/src/state/queries/usePostThread/traversal.ts @@ -265,12 +265,36 @@ export function sortAndAnnotateThreadItems( metadata.nextItemDepth = nextItem?.depth /* - * We can now officially calculate `isLastSibling` and `isLastChild` - * based on the actual data that we've seen. + * Item is the last "sibling" if we know for sure we're out of + * replies on the parent (even though this item itself may have its + * own reply branches). */ - metadata.isLastSibling = + const isLastSiblingByCounts = metadata.replyIndex === metadata.parentMetadata.repliesIndexCounter - 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 + * depth (meaning it's a sibling of the parent, not a child of this + * item). + */ + const isImplicitlyLastSibling = + metadata.nextItemDepth === undefined || + metadata.nextItemDepth < metadata.depth + + /* + * Ok now we can set the last sibling state. + */ + metadata.isLastSibling = + isLastSiblingByCounts || isImplicitlyLastSibling + + /* + * Item is the last "child" in a branch if there is no next item, + * or if the next item's depth is less than this item's depth (a + * sibling of the parent) or equal to this item's depth (a sibling + * of this item) + */ metadata.isLastChild = metadata.nextItemDepth === undefined || metadata.nextItemDepth <= metadata.depth