import React, {useCallback, useState} from 'react' import {View} from 'react-native' import {ImageBackground} from 'expo-image' import {AppBskyEmbedVideo} from '@atproto/api' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {clamp} from '#/lib/numbers' import {VideoEmbedInnerNative} from '#/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerNative' import {atoms as a} from '#/alf' import {Button} from '#/components/Button' import {useThrottledValue} from '#/components/hooks/useThrottledValue' import {Loader} from '#/components/Loader' import {PlayButtonIcon} from '#/components/video/PlayButtonIcon' import {ErrorBoundary} from '../ErrorBoundary' import * as VideoFallback from './VideoEmbedInner/VideoFallback' interface Props { embed: AppBskyEmbedVideo.View } export function VideoEmbed({embed}: Props) { const [key, setKey] = useState(0) const renderError = useCallback( (error: unknown) => ( setKey(key + 1)} /> ), [key], ) let aspectRatio = 16 / 9 if (embed.aspectRatio) { const {width, height} = embed.aspectRatio aspectRatio = width / height aspectRatio = clamp(aspectRatio, 1 / 1, 3 / 1) } return ( ) } function InnerWrapper({embed}: Props) { const {_} = useLingui() const ref = React.useRef<{togglePlayback: () => void}>(null) const [status, setStatus] = React.useState<'playing' | 'paused' | 'pending'>( 'pending', ) const [isLoading, setIsLoading] = React.useState(false) const [isActive, setIsActive] = React.useState(false) const showSpinner = useThrottledValue(isActive && isLoading, 100) const showOverlay = !isActive || isLoading || (status === 'paused' && !isActive) || status === 'pending' React.useEffect(() => { if (!isActive && status !== 'pending') { setStatus('pending') } }, [isActive, status]) return ( <> ) } function VideoError({retry}: {error: unknown; retry: () => void}) { return ( An error occurred while loading the video. Please try again later. ) }