import React, { useCallback, useEffect, useRef } from 'react' import { Pressable, StyleSheet, Text, View } from 'react-native' import { VideoView, useVideoPlayer } from 'expo-video' import { videos } from '../assets' import { Portal } from '@gorhom/portal' // Fullscreen vertical video overlay without controls // Props: // - url?: string | number (require), source of the video. Defaults to videos.test // - visible?: boolean, when false returns null // - onClose: () => void, called when user skips or when video ends const CLOSE_THRESHOLD_SECONDS = 0.35 const FullscreenIntroVideo = ({ url, visible = true, onClose, contentFit = 'contain', allowSkip = true, skipLabel = 'Passer la vidéo', }) => { const source = url ? (typeof url === 'string' ? { uri: url } : url) : videos.test const hasClosedRef = useRef(false) const handleClose = useCallback(() => { if (hasClosedRef.current) return hasClosedRef.current = true try { onClose?.() } catch (e) {} }, [onClose]) const player = useVideoPlayer(source, (p) => { p.loop = false p.timeUpdateEventInterval = 0.25 }) useEffect(() => { if (visible) { hasClosedRef.current = false } else { hasClosedRef.current = true try { player?.pause?.() } catch (e) {} } }, [player, visible]) useEffect(() => { if (!visible) { return } try { player?.play?.() } catch (e) {} }, [player, visible]) useEffect(() => { if (!player || !visible) return const playToEndSub = player.addListener?.('playToEnd', handleClose) const timeUpdateSub = player.addListener?.('timeUpdate', ({ currentTime } = {}) => { if (!player?.duration || hasClosedRef.current) { return } const remaining = player.duration - currentTime if (Number.isFinite(remaining) && remaining <= CLOSE_THRESHOLD_SECONDS) { handleClose() } }) const playingChangeSub = player.addListener?.('playingChange', ({ isPlaying } = {}) => { if (isPlaying || !player?.duration || hasClosedRef.current) { return } const remaining = player.duration - (player.currentTime ?? 0) if (Number.isFinite(remaining) && remaining <= CLOSE_THRESHOLD_SECONDS) { handleClose() } }) return () => { try { playToEndSub?.remove?.() timeUpdateSub?.remove?.() playingChangeSub?.remove?.() } catch (e) {} } }, [handleClose, player, visible]) if (!visible) { return null } return ( {allowSkip ? ( {skipLabel} ) : null} ) } export default FullscreenIntroVideo