Move indent calc to metadata, add ui field to readMore, use indent

This commit is contained in:
Eric Bailey
2025-10-14 15:08:33 -05:00
parent df9c289f55
commit c428568b0c
5 changed files with 31 additions and 9 deletions
@@ -28,7 +28,8 @@ export const ThreadItemReadMore = memo(function ThreadItemReadMore({
const t = useTheme() const t = useTheme()
const {_} = useLingui() const {_} = useLingui()
const isTreeView = view === 'tree' const isTreeView = view === 'tree'
const indent = Math.max(0, item.depth - 1) const indent = Math.max(0, item.ui.indent - 1)
console.log({indent, id: item.ui.indent})
const spacers = isTreeView const spacers = isTreeView
? Array.from(Array(indent)).map((_, n: number) => { ? Array.from(Array(indent)).map((_, n: number) => {
+7 -6
View File
@@ -9,6 +9,7 @@ import {
} from '#/state/queries/usePostThread/types' } from '#/state/queries/usePostThread/types'
import { import {
getPostRecord, getPostRecord,
getReadMoreUI,
getThreadPostNoUnauthenticatedUI, getThreadPostNoUnauthenticatedUI,
getThreadPostUI, getThreadPostUI,
getTraversalMetadata, getTraversalMetadata,
@@ -385,7 +386,9 @@ export function sortAndAnnotateThreadItems(
*/ */
if (metadata.repliesUnhydrated > 0 && metadata.isLastChild) { if (metadata.repliesUnhydrated > 0 && metadata.isLastChild) {
metadata.precedesChildReadMore = true metadata.precedesChildReadMore = true
subset.splice(i + 1, 0, views.readMore(metadata)) const view = views.readMore(metadata)
view.ui = getReadMoreUI(metadata)
subset.splice(i + 1, 0, view)
i++ // skip next iteration i++ // skip next iteration
} }
@@ -408,11 +411,9 @@ export function sortAndAnnotateThreadItems(
(metadata.nextItemDepth === undefined || (metadata.nextItemDepth === undefined ||
metadata.nextItemDepth <= metadata.upcomingParentReadMore.depth) metadata.nextItemDepth <= metadata.upcomingParentReadMore.depth)
) { ) {
subset.splice( const view = views.readMore(metadata.upcomingParentReadMore)
i + 1, view.ui = getReadMoreUI(metadata.upcomingParentReadMore)
0, subset.splice(i + 1, 0, view)
views.readMore(metadata.upcomingParentReadMore),
)
i++ i++
} }
+9
View File
@@ -106,6 +106,9 @@ export type ThreadItem =
href: string href: string
moreReplies: number moreReplies: number
skippedIndentIndices: Set<number> skippedIndentIndices: Set<number>
ui: {
indent: number
}
} }
| { | {
/* /*
@@ -137,6 +140,12 @@ export type TraversalMetadata = {
* calculated on the server. * calculated on the server.
*/ */
depth: number depth: number
/**
* The visual indentation level of the post. This is usually the same as
* `depth`, except in the case where the post is part of the OP thread,
* which is always indented to level 1 (except the root post, which is 0).
*/
indent: number
/** /**
* Indicates if this item is a "read more" link preceding this post that * Indicates if this item is a "read more" link preceding this post that
* continues the thread upwards. * continues the thread upwards.
+11 -2
View File
@@ -74,6 +74,7 @@ export function getTraversalMetadata({
const repliesUnhydrated = item.value.moreReplies || 0 const repliesUnhydrated = item.value.moreReplies || 0
const metadata = { const metadata = {
depth: item.depth, depth: item.depth,
indent: isPartOfOPThreadFromRoot ? (item.depth < 1 ? 0 : 1) : item.depth,
/* /*
* Unknown until after traversal * Unknown until after traversal
*/ */
@@ -136,10 +137,10 @@ export function storeTraversalMetadata(
export function getThreadPostUI({ export function getThreadPostUI({
depth, depth,
indent,
repliesCount, repliesCount,
prevItemDepth, prevItemDepth,
isLastChild, isLastChild,
isPartOfOPThreadFromRoot,
skippedIndentIndices, skippedIndentIndices,
repliesSeenCounter, repliesSeenCounter,
repliesUnhydrated, repliesUnhydrated,
@@ -157,7 +158,7 @@ export function getThreadPostUI({
followsReadMoreUp || followsReadMoreUp ||
(!!prevItemDepth && prevItemDepth !== 0 && prevItemDepth < depth), (!!prevItemDepth && prevItemDepth !== 0 && prevItemDepth < depth),
showChildReplyLine: depth < 0 || isReplyAndHasReplies, showChildReplyLine: depth < 0 || isReplyAndHasReplies,
indent: isPartOfOPThreadFromRoot ? (depth < 1 ? 0 : 1) : depth, indent,
/* /*
* If there are no slices below this one, or the next slice has a depth <= * If there are no slices below this one, or the next slice has a depth <=
* than the depth of this post, it's the last child of the reply tree. It * than the depth of this post, it's the last child of the reply tree. It
@@ -183,3 +184,11 @@ export function getThreadPostNoUnauthenticatedUI({
showParentReplyLine: Boolean(prevItemDepth && prevItemDepth < depth), showParentReplyLine: Boolean(prevItemDepth && prevItemDepth < depth),
} }
} }
export function getReadMoreUI({
indent,
}: TraversalMetadata): Extract<ThreadItem, {type: 'readMore'}>['ui'] {
return {
indent,
}
}
+2
View File
@@ -124,6 +124,8 @@ export function readMore({
moreReplies: repliesUnhydrated, moreReplies: repliesUnhydrated,
depth, depth,
skippedIndentIndices, skippedIndentIndices,
// @ts-ignore populated by the traversal
ui: {},
} }
} }