[Perf] Profile: Do not play video when Right Column is closed

This commit is contained in:
Alexander Zinchuk 2022-10-17 17:35:04 +02:00
parent e01a878bba
commit 7d714a9b26
4 changed files with 23 additions and 17 deletions

View File

@ -31,6 +31,7 @@ import './ProfileInfo.scss';
type OwnProps = { type OwnProps = {
userId: string; userId: string;
forceShowSelf?: boolean; forceShowSelf?: boolean;
canPlayVideo: boolean;
}; };
type StateProps = type StateProps =
@ -48,6 +49,7 @@ type StateProps =
const ProfileInfo: FC<OwnProps & StateProps> = ({ const ProfileInfo: FC<OwnProps & StateProps> = ({
forceShowSelf, forceShowSelf,
canPlayVideo,
user, user,
userStatus, userStatus,
chat, chat,
@ -184,7 +186,7 @@ const ProfileInfo: FC<OwnProps & StateProps> = ({
photo={photo} photo={photo}
isSavedMessages={isSavedMessages} isSavedMessages={isSavedMessages}
isFirstPhoto={isFirst} isFirstPhoto={isFirst}
notActive={!isActive} canPlayVideo={Boolean(isActive && canPlayVideo)}
onClick={handleProfilePhotoClick} onClick={handleProfilePhotoClick}
/> />
); );

View File

@ -15,10 +15,12 @@ import {
} from '../../global/helpers'; } from '../../global/helpers';
import renderText from './helpers/renderText'; import renderText from './helpers/renderText';
import buildClassName from '../../util/buildClassName'; import buildClassName from '../../util/buildClassName';
import safePlay from '../../util/safePlay';
import { getFirstLetters } from '../../util/textFormat'; import { getFirstLetters } from '../../util/textFormat';
import useMedia from '../../hooks/useMedia'; import useMedia from '../../hooks/useMedia';
import useLang from '../../hooks/useLang'; 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'; import Spinner from '../ui/Spinner';
@ -31,7 +33,7 @@ type OwnProps = {
isSavedMessages?: boolean; isSavedMessages?: boolean;
photo?: ApiPhoto; photo?: ApiPhoto;
lastSyncTime?: number; lastSyncTime?: number;
notActive?: boolean; canPlayVideo: boolean;
onClick: NoneToVoidFunction; onClick: NoneToVoidFunction;
}; };
@ -41,12 +43,13 @@ const ProfilePhoto: FC<OwnProps> = ({
photo, photo,
isFirstPhoto, isFirstPhoto,
isSavedMessages, isSavedMessages,
notActive, canPlayVideo,
lastSyncTime, lastSyncTime,
onClick, onClick,
}) => { }) => {
// eslint-disable-next-line no-null/no-null // eslint-disable-next-line no-null/no-null
const videoRef = useRef<HTMLVideoElement>(null); const videoRef = useRef<HTMLVideoElement>(null);
const lang = useLang(); const lang = useLang();
const isDeleted = user && isDeletedUser(user); const isDeleted = user && isDeletedUser(user);
const isRepliesChat = chat && isChatWithRepliesBot(chat.id); const isRepliesChat = chat && isChatWithRepliesBot(chat.id);
@ -77,13 +80,16 @@ const ProfilePhoto: FC<OwnProps> = ({
useEffect(() => { useEffect(() => {
if (!videoRef.current) return; if (!videoRef.current) return;
if (notActive) { if (!canPlayVideo) {
videoRef.current.pause(); videoRef.current.pause();
videoRef.current.currentTime = 0; videoRef.current.currentTime = 0;
} else { } else {
safePlay(videoRef.current); safePlay(videoRef.current);
} }
}, [notActive]); }, [canPlayVideo]);
const { handlePlaying } = useVideoAutoPause(videoRef, canPlayVideo);
useVideoCleanup(videoRef, []);
const photoHash = getMediaHash('big', 'photo'); const photoHash = getMediaHash('big', 'photo');
const photoBlobUrl = useMedia(photoHash, false, ApiMediaFormat.BlobUrl, lastSyncTime); const photoBlobUrl = useMedia(photoHash, false, ApiMediaFormat.BlobUrl, lastSyncTime);
@ -108,9 +114,10 @@ const ProfilePhoto: FC<OwnProps> = ({
className="avatar-media" className="avatar-media"
muted muted
disablePictureInPicture disablePictureInPicture
autoPlay={!notActive} autoPlay={canPlayVideo}
loop loop
playsInline playsInline
onPlaying={handlePlaying}
/> />
); );
} else { } else {

View File

@ -68,6 +68,7 @@ const SettingsMain: FC<OwnProps & StateProps> = ({
{currentUser && ( {currentUser && (
<ProfileInfo <ProfileInfo
userId={currentUser.id} userId={currentUser.id}
canPlayVideo={Boolean(isActive)}
forceShowSelf forceShowSelf
/> />
)} )}

View File

@ -11,15 +11,11 @@ import type {
ApiUser, ApiUser,
ApiUserStatus, ApiUserStatus,
} from '../../api/types'; } from '../../api/types';
import { import { MAIN_THREAD_ID } from '../../api/types';
MAIN_THREAD_ID,
} from '../../api/types';
import type { import type {
ISettings, ProfileState, ProfileTabType, SharedMediaType, ISettings, ProfileState, ProfileTabType, SharedMediaType,
} from '../../types'; } from '../../types';
import { import { NewChatMembersProgress, MediaViewerOrigin, AudioOrigin } from '../../types';
NewChatMembersProgress, MediaViewerOrigin, AudioOrigin,
} from '../../types';
import { import {
MEMBERS_SLICE, MEMBERS_SLICE,
@ -48,6 +44,7 @@ import useProfileState from './hooks/useProfileState';
import useTransitionFixes from './hooks/useTransitionFixes'; import useTransitionFixes from './hooks/useTransitionFixes';
import useAsyncRendering from './hooks/useAsyncRendering'; import useAsyncRendering from './hooks/useAsyncRendering';
import useLang from '../../hooks/useLang'; import useLang from '../../hooks/useLang';
import { useIntersectionObserver } from '../../hooks/useIntersectionObserver';
import Transition from '../ui/Transition'; import Transition from '../ui/Transition';
import InfiniteScroll from '../ui/InfiniteScroll'; import InfiniteScroll from '../ui/InfiniteScroll';
@ -67,7 +64,6 @@ import DeleteMemberModal from './DeleteMemberModal';
import GroupChatInfo from '../common/GroupChatInfo'; import GroupChatInfo from '../common/GroupChatInfo';
import './Profile.scss'; import './Profile.scss';
import { useIntersectionObserver } from '../../hooks/useIntersectionObserver';
type OwnProps = { type OwnProps = {
chatId: string; chatId: string;
@ -455,7 +451,7 @@ const Profile: FC<OwnProps & StateProps> = ({
onLoadMore={getMore} onLoadMore={getMore}
onScroll={handleScroll} onScroll={handleScroll}
> >
{!noProfileInfo && renderProfileInfo(chatId, resolvedUserId)} {!noProfileInfo && renderProfileInfo(chatId, resolvedUserId, isRightColumnShown && canRenderContent)}
{!isRestricted && ( {!isRestricted && (
<div <div
className="shared-media" className="shared-media"
@ -496,10 +492,10 @@ const Profile: FC<OwnProps & StateProps> = ({
); );
}; };
function renderProfileInfo(chatId: string, resolvedUserId?: string) { function renderProfileInfo(chatId: string, resolvedUserId: string | undefined, isReady: boolean) {
return ( return (
<div className="profile-info"> <div className="profile-info">
<ProfileInfo userId={resolvedUserId || chatId} /> <ProfileInfo userId={resolvedUserId || chatId} canPlayVideo={isReady} />
<ChatExtra chatOrUserId={resolvedUserId || chatId} /> <ChatExtra chatOrUserId={resolvedUserId || chatId} />
</div> </div>
); );