From ffc06bbaa3833837e68fa02b6089aab9c5bc35ee Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Wed, 23 Mar 2022 12:56:50 -0300 Subject: [PATCH] Prevent more_messages to autoscroll more than once --- app/components/post_list/index.tsx | 4 ++-- app/components/post_list/more_messages/more_messages.tsx | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/components/post_list/index.tsx b/app/components/post_list/index.tsx index a92a3d867f..46095bbe25 100644 --- a/app/components/post_list/index.tsx +++ b/app/components/post_list/index.tsx @@ -313,11 +313,11 @@ const PostList = ({ ); }, [currentTimezone, highlightPinnedOrSaved, isTimezoneEnabled, orderedPosts, shouldRenderReplyButton, theme]); - const scrollToIndex = useCallback((index: number, animated = true) => { + const scrollToIndex = useCallback((index: number, animated = true, applyOffset = true) => { listRef.current?.scrollToIndex({ animated, index, - viewOffset: Platform.select({ios: -45, default: 0}), + viewOffset: applyOffset ? Platform.select({ios: -45, default: 0}) : 0, viewPosition: 1, // 0 is at bottom }); }, []); diff --git a/app/components/post_list/more_messages/more_messages.tsx b/app/components/post_list/more_messages/more_messages.tsx index eca27f5f28..80f3c914fd 100644 --- a/app/components/post_list/more_messages/more_messages.tsx +++ b/app/components/post_list/more_messages/more_messages.tsx @@ -23,7 +23,7 @@ type Props = { posts: Array; registerScrollEndIndexListener: (fn: (endIndex: number) => void) => () => void; registerViewableItemsListener: (fn: (viewableItems: ViewToken[]) => void) => () => void; - scrollToIndex: (index: number, animated?: boolean) => void; + scrollToIndex: (index: number, animated?: boolean, applyOffset?: boolean) => void; unreadCount: number; theme: Theme; testID: string; @@ -103,6 +103,7 @@ const MoreMessages = ({ const serverUrl = useServerUrl(); const pressed = useRef(false); const resetting = useRef(false); + const initialScroll = useRef(false); const [loading, setLoading] = useState(false); const [remaining, setRemaining] = useState(0); const underlayColor = useMemo(() => `hsl(${hexToHue(theme.buttonBg)}, 50%, 38%)`, [theme]); @@ -149,13 +150,14 @@ const MoreMessages = ({ const lastViewableIndex = viewableItems.filter((v) => v.isViewable)[viewableItems.length - 1]?.index || 0; const nextViewableIndex = lastViewableIndex + 1; - if (viewableItems[0].index === 0 && nextViewableIndex > newMessageLineIndex) { + if (viewableItems[0].index === 0 && nextViewableIndex > newMessageLineIndex && !initialScroll.current) { // Auto scroll if the first post is viewable and // * the new message line is viewable OR // * the new message line will be the first next viewable item - scrollToIndex(newMessageLineIndex, true); + scrollToIndex(newMessageLineIndex, true, false); resetCount(); top.value = 0; + initialScroll.current = true; return; } @@ -212,6 +214,7 @@ const MoreMessages = ({ useEffect(() => { resetting.current = false; + initialScroll.current = false; }, [channelId]); return (