From 7d714a9b262143f183563a9cd3aeaeba89b507d0 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 17 Oct 2022 17:35:04 +0200 Subject: [PATCH] [Perf] Profile: Do not play video when Right Column is closed --- src/components/common/ProfileInfo.tsx | 4 +++- src/components/common/ProfilePhoto.tsx | 19 +++++++++++++------ src/components/left/settings/SettingsMain.tsx | 1 + src/components/right/Profile.tsx | 16 ++++++---------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/components/common/ProfileInfo.tsx b/src/components/common/ProfileInfo.tsx index dab0c5d97..b7a7228ac 100644 --- a/src/components/common/ProfileInfo.tsx +++ b/src/components/common/ProfileInfo.tsx @@ -31,6 +31,7 @@ import './ProfileInfo.scss'; type OwnProps = { userId: string; forceShowSelf?: boolean; + canPlayVideo: boolean; }; type StateProps = @@ -48,6 +49,7 @@ type StateProps = const ProfileInfo: FC = ({ forceShowSelf, + canPlayVideo, user, userStatus, chat, @@ -184,7 +186,7 @@ const ProfileInfo: FC = ({ photo={photo} isSavedMessages={isSavedMessages} isFirstPhoto={isFirst} - notActive={!isActive} + canPlayVideo={Boolean(isActive && canPlayVideo)} onClick={handleProfilePhotoClick} /> ); diff --git a/src/components/common/ProfilePhoto.tsx b/src/components/common/ProfilePhoto.tsx index ea7302198..ff4a831b8 100644 --- a/src/components/common/ProfilePhoto.tsx +++ b/src/components/common/ProfilePhoto.tsx @@ -15,10 +15,12 @@ import { } from '../../global/helpers'; import renderText from './helpers/renderText'; import buildClassName from '../../util/buildClassName'; -import safePlay from '../../util/safePlay'; import { getFirstLetters } from '../../util/textFormat'; import useMedia from '../../hooks/useMedia'; import useLang from '../../hooks/useLang'; +import useVideoAutoPause from '../middle/message/hooks/useVideoAutoPause'; +import useVideoCleanup from '../../hooks/useVideoCleanup'; +import safePlay from '../../util/safePlay'; import Spinner from '../ui/Spinner'; @@ -31,7 +33,7 @@ type OwnProps = { isSavedMessages?: boolean; photo?: ApiPhoto; lastSyncTime?: number; - notActive?: boolean; + canPlayVideo: boolean; onClick: NoneToVoidFunction; }; @@ -41,12 +43,13 @@ const ProfilePhoto: FC = ({ photo, isFirstPhoto, isSavedMessages, - notActive, + canPlayVideo, lastSyncTime, onClick, }) => { // eslint-disable-next-line no-null/no-null const videoRef = useRef(null); + const lang = useLang(); const isDeleted = user && isDeletedUser(user); const isRepliesChat = chat && isChatWithRepliesBot(chat.id); @@ -77,13 +80,16 @@ const ProfilePhoto: FC = ({ useEffect(() => { if (!videoRef.current) return; - if (notActive) { + if (!canPlayVideo) { videoRef.current.pause(); videoRef.current.currentTime = 0; } else { safePlay(videoRef.current); } - }, [notActive]); + }, [canPlayVideo]); + + const { handlePlaying } = useVideoAutoPause(videoRef, canPlayVideo); + useVideoCleanup(videoRef, []); const photoHash = getMediaHash('big', 'photo'); const photoBlobUrl = useMedia(photoHash, false, ApiMediaFormat.BlobUrl, lastSyncTime); @@ -108,9 +114,10 @@ const ProfilePhoto: FC = ({ className="avatar-media" muted disablePictureInPicture - autoPlay={!notActive} + autoPlay={canPlayVideo} loop playsInline + onPlaying={handlePlaying} /> ); } else { diff --git a/src/components/left/settings/SettingsMain.tsx b/src/components/left/settings/SettingsMain.tsx index 5b3578175..0879ad46a 100644 --- a/src/components/left/settings/SettingsMain.tsx +++ b/src/components/left/settings/SettingsMain.tsx @@ -68,6 +68,7 @@ const SettingsMain: FC = ({ {currentUser && ( )} diff --git a/src/components/right/Profile.tsx b/src/components/right/Profile.tsx index b5657debe..42bf538e1 100644 --- a/src/components/right/Profile.tsx +++ b/src/components/right/Profile.tsx @@ -11,15 +11,11 @@ import type { ApiUser, ApiUserStatus, } from '../../api/types'; -import { - MAIN_THREAD_ID, -} from '../../api/types'; +import { MAIN_THREAD_ID } from '../../api/types'; import type { ISettings, ProfileState, ProfileTabType, SharedMediaType, } from '../../types'; -import { - NewChatMembersProgress, MediaViewerOrigin, AudioOrigin, -} from '../../types'; +import { NewChatMembersProgress, MediaViewerOrigin, AudioOrigin } from '../../types'; import { MEMBERS_SLICE, @@ -48,6 +44,7 @@ import useProfileState from './hooks/useProfileState'; import useTransitionFixes from './hooks/useTransitionFixes'; import useAsyncRendering from './hooks/useAsyncRendering'; import useLang from '../../hooks/useLang'; +import { useIntersectionObserver } from '../../hooks/useIntersectionObserver'; import Transition from '../ui/Transition'; import InfiniteScroll from '../ui/InfiniteScroll'; @@ -67,7 +64,6 @@ import DeleteMemberModal from './DeleteMemberModal'; import GroupChatInfo from '../common/GroupChatInfo'; import './Profile.scss'; -import { useIntersectionObserver } from '../../hooks/useIntersectionObserver'; type OwnProps = { chatId: string; @@ -455,7 +451,7 @@ const Profile: FC = ({ onLoadMore={getMore} onScroll={handleScroll} > - {!noProfileInfo && renderProfileInfo(chatId, resolvedUserId)} + {!noProfileInfo && renderProfileInfo(chatId, resolvedUserId, isRightColumnShown && canRenderContent)} {!isRestricted && (
= ({ ); }; -function renderProfileInfo(chatId: string, resolvedUserId?: string) { +function renderProfileInfo(chatId: string, resolvedUserId: string | undefined, isReady: boolean) { return (
- +
);