import { useCallback, memo, useState, useMemo, useEffect } from 'react'; import cs from 'classnames'; import { View } from 'tuikit-atomicx-react/chat'; import classes from './VideoMessage.module.scss'; import type { MessageInfo, VideoMessagePayload } from 'tuikit-atomicx-react/chat'; interface VideoMessageProps { message: MessageInfo; isLastMessage?: boolean; } const VideoMessage = (props: VideoMessageProps) => { const { message, isLastMessage = false } = props; const messageContent = message.messagePayload as VideoMessagePayload; const [windowResize, setWindowResize] = useState(false); const [loaded, setLoaded] = useState(false); const [naturalSize, setNaturalSize] = useState<{ height: number; aspectRatio: number } | null>( messageContent.videoSnapshotWidth && messageContent.videoSnapshotHeight ? { height: messageContent.videoSnapshotHeight, aspectRatio: messageContent.videoSnapshotWidth / messageContent.videoSnapshotHeight, } : null, ); const isMessageOwner = message.isSentBySelf; const onResize = useCallback(() => { setWindowResize(true); }, []); useEffect(() => { window.addEventListener('resize', onResize); return () => { window.removeEventListener('resize', onResize); }; }, [onResize]); // calculate final display size const displaySize = useMemo(() => { const MAX_HEIGHT = 360; const DEFAULT_RATIO = 3 / 4; if (!naturalSize) { return { height: MAX_HEIGHT, aspectRatio: DEFAULT_RATIO, }; } const { aspectRatio } = naturalSize; if (aspectRatio > 1) { return { aspectRatio, height: 200, }; } return { aspectRatio, height: MAX_HEIGHT, }; }, [naturalSize]); const onVideoLoad = useCallback((e: React.SyntheticEvent) => { const video = e.target as HTMLVideoElement; setNaturalSize({ aspectRatio: video.videoWidth / video.videoHeight, height: video.videoHeight, }); setLoaded(true); }, []); return ( {!loaded &&
}