import React, {useCallback, useId, useState} from 'react' import {View} from 'react-native' import {Image} 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 {useGate} from '#/lib/statsig/statsig' import {VideoEmbedInnerNative} from '#/view/com/util/post-embeds/VideoEmbedInner/VideoEmbedInnerNative' import {atoms as a} from '#/alf' import {Button} from '#/components/Button' import {PlayButtonIcon} from '#/components/video/PlayButtonIcon' import {VisibilityView} from '../../../../../modules/expo-bluesky-swiss-army' import {ErrorBoundary} from '../ErrorBoundary' import {useActiveVideoNative} from './ActiveVideoNativeContext' import * as VideoFallback from './VideoEmbedInner/VideoFallback' export function VideoEmbed({embed}: {embed: AppBskyEmbedVideo.View}) { const {_} = useLingui() const {activeSource, activeViewId, setActiveSource, player} = useActiveVideoNative() const viewId = useId() const [isFullscreen, setIsFullscreen] = React.useState(false) const isActive = embed.playlist === activeSource && activeViewId === viewId const [key, setKey] = useState(0) const renderError = useCallback( (error: unknown) => ( setKey(key + 1)} /> ), [key], ) const gate = useGate() const onChangeStatus = (isVisible: boolean) => { if (isVisible) { setActiveSource(embed.playlist, viewId) if (!player.playing) { player.play() } } else if (!isFullscreen) { player.muted = true if (player.playing) { player.pause() } } } if (!gate('video_view_on_posts')) { return null } let aspectRatio = 16 / 9 if (embed.aspectRatio) { const {width, height} = embed.aspectRatio aspectRatio = width / height aspectRatio = clamp(aspectRatio, 1 / 1, 3 / 1) } return ( {isActive ? ( ) : ( <> {embed.alt} )} ) } function VideoError({retry}: {error: unknown; retry: () => void}) { return ( An error occurred while loading the video. Please try again later. ) }