From 73fb1fd2cf625a5f062d52deb99b032b3b72464c Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Thu, 29 Aug 2024 15:52:18 +0200 Subject: [PATCH] Profile: Fix scroll on opening (#4877) --- src/components/right/Profile.tsx | 16 +++++++++++++++- src/components/right/hooks/useProfileState.ts | 8 ++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/components/right/Profile.tsx b/src/components/right/Profile.tsx index a98a4ba3e..1f7c17003 100644 --- a/src/components/right/Profile.tsx +++ b/src/components/right/Profile.tsx @@ -63,6 +63,7 @@ import { getSenderName } from '../left/search/helpers/getSenderName'; import usePeerStoriesPolling from '../../hooks/polling/usePeerStoriesPolling'; import useCacheBuster from '../../hooks/useCacheBuster'; import useEffectWithPrevDeps from '../../hooks/useEffectWithPrevDeps'; +import useFlag from '../../hooks/useFlag'; import { useIntersectionObserver } from '../../hooks/useIntersectionObserver'; import useLastCallback from '../../hooks/useLastCallback'; import useOldLang from '../../hooks/useOldLang'; @@ -256,6 +257,8 @@ const Profile: FC = ({ return index === -1 ? 0 : index; }, [nextProfileTab, tabs]); + const [allowAutoScrollToTabs, startAutoScrollToTabsIfNeeded, stopAutoScrollToTabs] = useFlag(false); + const [activeTab, setActiveTab] = useState(initialTab); useEffect(() => { @@ -266,6 +269,11 @@ const Profile: FC = ({ setActiveTab(index); }, [nextProfileTab, tabs]); + const handleSwitchTab = useCallback((index: number) => { + startAutoScrollToTabsIfNeeded(); + setActiveTab(index); + }, []); + useEffect(() => { if (isChannel && !similarChannels) { loadChannelRecommendations({ chatId }); @@ -310,12 +318,18 @@ const Profile: FC = ({ usePeerStoriesPolling(resultType === 'members' ? viewportIds as string[] : undefined); + const handleStopAutoScrollToTabs = useLastCallback(() => { + stopAutoScrollToTabs(); + }); + const { handleScroll } = useProfileState( containerRef, resultType, profileState, onProfileStateChange, forceScrollProfileTab, + allowAutoScrollToTabs, + handleStopAutoScrollToTabs, ); const { applyTransitionFix, releaseTransitionFix } = useTransitionFixes(containerRef); @@ -649,7 +663,7 @@ const Profile: FC = ({ > {renderContent()} - + )} diff --git a/src/components/right/hooks/useProfileState.ts b/src/components/right/hooks/useProfileState.ts index b92d09cbb..00192a0ff 100644 --- a/src/components/right/hooks/useProfileState.ts +++ b/src/components/right/hooks/useProfileState.ts @@ -21,12 +21,15 @@ export default function useProfileState( profileState: ProfileState, onProfileStateChange: (state: ProfileState) => void, forceScrollProfileTab = false, + allowAutoScrollToTabs = false, + handleStopAutoScrollToTabs: () => void, ) { // Scroll to tabs if needed useEffectWithPrevDeps(([prevTabType]) => { - if ((prevTabType && prevTabType !== tabType) || (tabType && forceScrollProfileTab)) { + if ((prevTabType && prevTabType !== tabType && allowAutoScrollToTabs) || (tabType && forceScrollProfileTab)) { const container = containerRef.current!; const tabsEl = container.querySelector('.TabList')!; + handleStopAutoScrollToTabs(); if (container.scrollTop < tabsEl.offsetTop) { onProfileStateChange(getStateFromTabType(tabType)); isScrollingProgrammatically = true; @@ -36,7 +39,8 @@ export default function useProfileState( }, PROGRAMMATIC_SCROLL_TIMEOUT_MS); } } - }, [tabType, onProfileStateChange, containerRef, forceScrollProfileTab]); + }, [tabType, onProfileStateChange, containerRef, forceScrollProfileTab, + allowAutoScrollToTabs, handleStopAutoScrollToTabs]); // Scroll to top useEffectWithPrevDeps(([prevProfileState]) => {