diff options
Diffstat (limited to 'src/view/com/post-thread/PostThread.tsx')
-rw-r--r-- | src/view/com/post-thread/PostThread.tsx | 58 |
1 files changed, 43 insertions, 15 deletions
diff --git a/src/view/com/post-thread/PostThread.tsx b/src/view/com/post-thread/PostThread.tsx index cb7fd3f41..379fe4f99 100644 --- a/src/view/com/post-thread/PostThread.tsx +++ b/src/view/com/post-thread/PostThread.tsx @@ -139,7 +139,7 @@ function PostThreadLoaded({ const {hasSession} = useSession() const {_} = useLingui() const pal = usePalette('default') - const {isTablet, isDesktop} = useWebMediaQueries() + const {isTablet, isDesktop, isTabletOrMobile} = useWebMediaQueries() const ref = useRef<ListMethods>(null) const highlightedPostRef = useRef<View | null>(null) const needsScrollAdjustment = useRef<boolean>( @@ -157,7 +157,11 @@ function PostThreadLoaded({ const posts = React.useMemo(() => { let arr = [TOP_COMPONENT].concat( Array.from( - flattenThreadSkeleton(sortThread(thread, threadViewPrefs), hasSession), + flattenThreadSkeleton( + sortThread(thread, threadViewPrefs), + hasSession, + treeView, + ), ), ) if (arr.length > maxVisible) { @@ -167,7 +171,7 @@ function PostThreadLoaded({ arr.push(BOTTOM_COMPONENT) } return arr - }, [thread, maxVisible, threadViewPrefs, hasSession]) + }, [thread, treeView, maxVisible, threadViewPrefs, hasSession]) /** * NOTE @@ -197,17 +201,35 @@ function PostThreadLoaded({ // wait for loading to finish if (thread.type === 'post' && !!thread.parent) { - highlightedPostRef.current?.measure( - (_x, _y, _width, _height, _pageX, pageY) => { - ref.current?.scrollToOffset({ - animated: false, - offset: pageY - (isDesktop ? 0 : 50), - }) - }, - ) + function onMeasure(pageY: number) { + let spinnerHeight = 0 + if (isDesktop) { + spinnerHeight = 40 + } else if (isTabletOrMobile) { + spinnerHeight = 82 + } + ref.current?.scrollToOffset({ + animated: false, + offset: pageY - spinnerHeight, + }) + } + if (isNative) { + highlightedPostRef.current?.measure( + (_x, _y, _width, _height, _pageX, pageY) => { + onMeasure(pageY) + }, + ) + } else { + // Measure synchronously to avoid a layout jump. + const domNode = highlightedPostRef.current + if (domNode) { + const pageY = (domNode as any as Element).getBoundingClientRect().top + onMeasure(pageY) + } + } needsScrollAdjustment.current = false } - }, [thread, isDesktop]) + }, [thread, isDesktop, isTabletOrMobile]) const onPTR = React.useCallback(async () => { setIsPTRing(true) @@ -280,8 +302,10 @@ function PostThreadLoaded({ // -prf return ( <View + // @ts-ignore web-only style={{ - height: 400, + // Leave enough space below that the scroll doesn't jump + height: isNative ? 400 : '100vh', borderTopWidth: 1, borderColor: pal.colors.border, }} @@ -468,10 +492,11 @@ function isThreadPost(v: unknown): v is ThreadPost { function* flattenThreadSkeleton( node: ThreadNode, hasSession: boolean, + treeView: boolean, ): Generator<YieldedItem, void> { if (node.type === 'post') { if (node.parent) { - yield* flattenThreadSkeleton(node.parent, hasSession) + yield* flattenThreadSkeleton(node.parent, hasSession, treeView) } else if (node.ctx.isParentLoading) { yield PARENT_SPINNER } @@ -484,7 +509,10 @@ function* flattenThreadSkeleton( } if (node.replies?.length) { for (const reply of node.replies) { - yield* flattenThreadSkeleton(reply, hasSession) + yield* flattenThreadSkeleton(reply, hasSession, treeView) + if (!treeView && !node.ctx.isHighlightedPost) { + break + } } } else if (node.ctx.isChildLoading) { yield CHILD_SPINNER |