From 9d788a2fd28d54a9ec2d8afc585592a4130ba371 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 6 Sep 2024 15:43:14 +0200 Subject: [PATCH] Photo, Video: Leverage the new `useShowTransition` --- src/components/middle/message/Photo.tsx | 37 +++++++++++------------ src/components/middle/message/Video.tsx | 39 +++++++++++++------------ src/components/ui/Modal.tsx | 5 ++-- src/components/ui/ShowTransition.tsx | 5 ++-- src/hooks/useShowTransition.ts | 11 +++++-- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/src/components/middle/message/Photo.tsx b/src/components/middle/message/Photo.tsx index 6a1c3de93..3452838d0 100644 --- a/src/components/middle/message/Photo.tsx +++ b/src/components/middle/message/Photo.tsx @@ -8,10 +8,7 @@ import type { IMediaDimensions } from './helpers/calculateAlbumLayout'; import { CUSTOM_APPENDIX_ATTRIBUTE, MESSAGE_CONTENT_SELECTOR } from '../../../config'; import { requestMutation } from '../../../lib/fasterdom/fasterdom'; import { - getMediaFormat, - getMediaThumbUri, - getMediaTransferState, - getPhotoMediaHash, + getMediaFormat, getMediaThumbUri, getMediaTransferState, getPhotoMediaHash, } from '../../../global/helpers'; import buildClassName from '../../../util/buildClassName'; import getCustomAppendixBg from './helpers/getCustomAppendixBg'; @@ -22,10 +19,10 @@ import useFlag from '../../../hooks/useFlag'; import { useIsIntersecting } from '../../../hooks/useIntersectionObserver'; import useLastCallback from '../../../hooks/useLastCallback'; import useLayoutEffectWithPrevDeps from '../../../hooks/useLayoutEffectWithPrevDeps'; -import useMediaTransitionDeprecated from '../../../hooks/useMediaTransitionDeprecated'; +import useMediaTransition from '../../../hooks/useMediaTransition'; import useMediaWithLoadProgress from '../../../hooks/useMediaWithLoadProgress'; import usePreviousDeprecated from '../../../hooks/usePreviousDeprecated'; -import useShowTransitionDeprecated from '../../../hooks/useShowTransitionDeprecated'; +import useShowTransition from '../../../hooks/useShowTransition'; import useBlurredMediaThumbRef from './hooks/useBlurredMediaThumbRef'; import MediaSpoiler from '../../common/MediaSpoiler'; @@ -105,8 +102,8 @@ const Photo = ({ const [withThumb] = useState(!fullMediaData); const noThumb = Boolean(fullMediaData); const thumbRef = useBlurredMediaThumbRef(photo, noThumb); + useMediaTransition(!noThumb, { ref: thumbRef }); const blurredBackgroundRef = useBlurredMediaThumbRef(photo, !withBlurredBackground); - const thumbClassNames = useMediaTransitionDeprecated(!noThumb); const thumbDataUri = getMediaThumbUri(photo); const [isSpoilerShown, showSpoiler, hideSpoiler] = useFlag(isPaidPreview || photo.isSpoiler); @@ -137,13 +134,21 @@ const Photo = ({ const wasLoadDisabled = usePreviousDeprecated(isLoadAllowed) === false; const { + ref: spinnerRef, shouldRender: shouldRenderSpinner, - transitionClassNames: spinnerClassNames, - } = useShowTransitionDeprecated(isTransferring, undefined, wasLoadDisabled, 'slow'); + } = useShowTransition({ + isOpen: isTransferring, + noMountTransition: wasLoadDisabled, + className: 'slow', + withShouldRender: true, + }); const { + ref: downloadButtonRef, shouldRender: shouldRenderDownloadButton, - transitionClassNames: downloadButtonClassNames, - } = useShowTransitionDeprecated(!fullMediaData && !isLoadAllowed); + } = useShowTransition({ + isOpen: !fullMediaData && !isLoadAllowed, + withShouldRender: true, + }); const handleClick = useLastCallback((e: React.MouseEvent) => { if (isUploading) { @@ -228,19 +233,15 @@ const Photo = ({ /> )} {withThumb && ( - + )} {isProtected && } {shouldRenderSpinner && !shouldRenderDownloadButton && ( -
+
)} - {shouldRenderDownloadButton - && } + {shouldRenderDownloadButton && } ({ const [isPreviewPreloaded] = useState(Boolean(previewMediaHash && mediaLoader.getFromMemory(previewMediaHash))); const canLoadPreview = isIntersectingForLoading; const previewBlobUrl = useMedia(previewMediaHash, !canLoadPreview); - const previewClassNames = useMediaTransitionDeprecated((hasThumb || previewBlobUrl) && !isPlayerReady); + const previewRef = useMediaTransition((hasThumb || previewBlobUrl) && !isPlayerReady); const noThumb = Boolean(!hasThumb || previewBlobUrl || isPlayerReady); const thumbRef = useBlurredMediaThumbRef(video, noThumb); + useMediaTransition(!noThumb, { ref: thumbRef }); const blurredBackgroundRef = useBlurredMediaThumbRef(video, !withBlurredBackground); - const thumbClassNames = useMediaTransitionDeprecated(!noThumb); const isInline = fullMediaData && wasIntersectedRef.current; @@ -154,12 +151,18 @@ const Video = ({ const wasLoadDisabled = usePreviousDeprecated(isLoadAllowed) === false; const { + ref: spinnerRef, shouldRender: shouldRenderSpinner, - transitionClassNames: spinnerClassNames, - } = useShowTransitionDeprecated(isTransferring && !isUnsupported, undefined, wasLoadDisabled); + } = useShowTransition({ + isOpen: isTransferring && !isUnsupported, + noMountTransition: wasLoadDisabled, + withShouldRender: true, + }); const { - transitionClassNames: playButtonClassNames, - } = useShowTransitionDeprecated(Boolean((isLoadAllowed || fullMediaData) && !isPlayAllowed && !shouldRenderSpinner)); + ref: playButtonRef, + } = useShowTransition({ + isOpen: Boolean((isLoadAllowed || fullMediaData) && !isPlayAllowed && !shouldRenderSpinner), + }); const [playProgress, setPlayProgress] = useState(0); const handleTimeUpdate = useLastCallback((e: React.SyntheticEvent) => { @@ -253,20 +256,18 @@ const Video = ({ /> )} {hasThumb && !isPreviewPreloaded && ( - + )} {isProtected && } - + ({ className="media-spoiler" /> {shouldRenderSpinner && ( -
+
= ({ }) => { const { ref: modalRef, - getShouldRender, + shouldRender, } = useShowTransition({ isOpen, onCloseAnimationEnd, + withShouldRender: true, }); - const shouldRender = useDerivedState(getShouldRender); const withCloseButton = hasCloseButton || hasAbsoluteCloseButton; diff --git a/src/components/ui/ShowTransition.tsx b/src/components/ui/ShowTransition.tsx index dbcdf9cff..6d1de2f0d 100644 --- a/src/components/ui/ShowTransition.tsx +++ b/src/components/ui/ShowTransition.tsx @@ -1,7 +1,6 @@ import type { FC } from '../../lib/teact/teact'; import React, { useRef } from '../../lib/teact/teact'; -import useDerivedState from '../../hooks/useDerivedState'; import usePreviousDeprecated from '../../hooks/usePreviousDeprecated'; import useShowTransition from '../../hooks/useShowTransition'; @@ -37,14 +36,14 @@ const ShowTransition: FC = ({ const fromChildrenRef = useRef(); const isFirstRender = prevIsOpen === undefined; - const { ref, getShouldRender } = useShowTransition({ + const { ref, shouldRender } = useShowTransition({ isOpen: isOpen && !isHidden, ref: externalRef, noMountTransition: isFirstRender && !shouldAnimateFirstRender, className: isCustom ? false : undefined, noCloseTransition, + withShouldRender: true, }); - const shouldRender = useDerivedState(getShouldRender); if (prevIsOpen && !isOpen) { fromChildrenRef.current = prevChildren; diff --git a/src/hooks/useShowTransition.ts b/src/hooks/useShowTransition.ts index f16e6f507..75a973fbb 100644 --- a/src/hooks/useShowTransition.ts +++ b/src/hooks/useShowTransition.ts @@ -3,7 +3,7 @@ import { useLayoutEffect, useRef, useSignal } from '../lib/teact/teact'; import { addExtraClass, toggleExtraClass } from '../lib/teact/teact-dom'; import { requestMeasure } from '../lib/fasterdom/fasterdom'; -import useDerivedSignal from './useDerivedSignal'; +import useDerivedState from './useDerivedState'; import useLastCallback from './useLastCallback'; import { useStateRef } from './useStateRef'; import useSyncEffectWithPrevDeps from './useSyncEffectWithPrevDeps'; @@ -26,6 +26,7 @@ export default function useShowTransition; @@ -35,6 +36,7 @@ export default function useShowTransition getState() !== 'closed', [getState]); + const shouldRender = useDerivedState( + () => (withShouldRender && getState() !== 'closed'), + [withShouldRender, getState], + ); - return { ref, getShouldRender }; + return { ref, shouldRender }; }