I’m creating an AGPL-licensed Lemmy web UI.
While working on the comment section I wondered: what’s the best way to retrieve the total amount of direct descendants of a comment? I’m not talking about child_count
as this appears to count descendants at any depth.
My use case is displaying a ‘Load X more’ button that for a given comment loads any direct descendants that are not yet being displayed.
In my web app, I just say “X more replies…” on that button using the child_count property. It really doesn’t matter much to the user how many comments are going to be loaded next in my opinion, and that keeps it simple on our end.
If you really need to know this, you could consider faking it a bit by fetching an extra level/depth each time the user presses that button. Then, just hide that extra level until the user presses the load more button, and manually count how many comments there are in that next level. This way you can have the correct direct descendant count, each level will load instantly to the user, and you’ll be fetching the next level in the background.
It really doesn’t matter much to the user how many comments are going to be loaded next in my opinion, and that keeps it simple on our end.
Here’s what trips me up: even if I don’t display the number, I still need to know whether extra comments can be loaded on that depth. If not I don’t want to display a “Load more”. You could of course let the “Load more” load hidden comments at any level of that subtree, but I think that causes unexpected behavior.
Maybe this clarifies what I’m talking about:
In the case on the right, there’s an unexpected downward shift of content which may disorient the user.
Ah, I wasn’t fully understanding, I see what you’re saying now. There’s no way to know the number of comments on a single level/depth as of now without some extra fetching. I think your best bet at this point would be preloading the next page of comments on the top level, and if no comments are return you then know not to show the “load more” button.
Then, each time the next page of top-level comments is fetched, you can also fetch the children of each comment. This will prevent unwanted layout shift and ensure all children are fetched. If you don’t do this, I’ve noticed sometimes child comments go missing when I try to fetch multiple levels of comments in one request from a larger comment tree (100+ comments)