From 04dee2ce7b3ec84384c49d6f64903a9e1d60f296 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Mon, 17 Oct 2022 17:35:06 +0200 Subject: [PATCH] [Perf] Introduce Optimized Video component --- src/components/common/Avatar.tsx | 36 ++++-------- src/components/common/CustomEmoji.tsx | 56 ++++++------------- src/components/common/GifButton.tsx | 15 ++--- src/components/common/ProfilePhoto.tsx | 18 ++---- src/components/common/StickerButton.tsx | 5 +- .../previews/PremiumFeaturePreviewVideo.tsx | 27 ++------- src/components/middle/MessageList.tsx | 5 +- .../middle/composer/StickerSetCover.tsx | 4 +- src/components/middle/message/RoundVideo.tsx | 26 ++------- src/components/middle/message/Sticker.tsx | 17 +----- src/components/middle/message/Video.tsx | 18 ++---- .../middle/message/hooks/useVideoAutoPause.ts | 34 ++++++----- src/components/ui/OptimizedVideo.tsx | 34 +++++++++++ 13 files changed, 119 insertions(+), 176 deletions(-) create mode 100644 src/components/ui/OptimizedVideo.tsx diff --git a/src/components/common/Avatar.tsx b/src/components/common/Avatar.tsx index 225cb9d97..b20504d62 100644 --- a/src/components/common/Avatar.tsx +++ b/src/components/common/Avatar.tsx @@ -32,8 +32,8 @@ import useMedia from '../../hooks/useMedia'; import useShowTransition from '../../hooks/useShowTransition'; import useLang from '../../hooks/useLang'; import { useIsIntersecting } from '../../hooks/useIntersectionObserver'; -import useVideoAutoPause from '../middle/message/hooks/useVideoAutoPause'; -import useVideoCleanup from '../../hooks/useVideoCleanup'; + +import OptimizedVideo from '../ui/OptimizedVideo'; import './Avatar.scss'; @@ -79,8 +79,6 @@ const Avatar: FC = ({ const { loadFullUser } = getActions(); // eslint-disable-next-line no-null/no-null const ref = useRef(null); - // eslint-disable-next-line no-null/no-null - const videoRef = useRef(null); const videoLoopCountRef = useRef(0); const isIntersecting = useIsIntersecting(ref, observeIntersection); const isDeleted = user && isDeletedUser(user); @@ -117,24 +115,14 @@ const Avatar: FC = ({ const { transitionClassNames } = useShowTransition(hasBlobUrl, undefined, hasBlobUrl, 'slow'); - const { handlePlaying } = useVideoAutoPause(videoRef, shouldPlayVideo); - useVideoCleanup(videoRef, [shouldPlayVideo]); + const handleVideoEnded = useCallback((e) => { + const video = e.currentTarget; + if (!videoBlobUrl) return; - useEffect(() => { - const video = videoRef.current; - if (!video || !videoBlobUrl) return undefined; - - const returnToStart = () => { - videoLoopCountRef.current += 1; - if (videoLoopCountRef.current >= LOOP_COUNT || noLoop) { - video.style.display = 'none'; - } else { - video.play(); - } - }; - - video.addEventListener('ended', returnToStart); - return () => video.removeEventListener('ended', returnToStart); + videoLoopCountRef.current += 1; + if (videoLoopCountRef.current >= LOOP_COUNT || noLoop) { + video.style.display = 'none'; + } }, [noLoop, videoBlobUrl]); const userId = user?.id; @@ -165,15 +153,15 @@ const Avatar: FC = ({ decoding="async" /> {shouldPlayVideo && ( -