import { useEffect, useRef } from 'react'; import { getVideoPreviewUrl } from './utils'; import type { MediaItem } from './types'; type VideoThumbPreviewProps = { item: MediaItem; fallbackImage?: string; durationLabel: string; isActive: boolean; }; export function VideoThumbPreview( { item, fallbackImage, durationLabel, isActive, }: VideoThumbPreviewProps ) { const videoRef = useRef( null ); const previewUrl = getVideoPreviewUrl( item.sources ); const showPreview = isActive && !! previewUrl; useEffect( () => { const video = videoRef.current; if ( ! video || ! showPreview ) { return; } video.load(); video.play().catch( () => undefined ); }, [ showPreview, previewUrl ] ); if ( showPreview ) { return ( <> { durationLabel ? { durationLabel } : null } ); } return ( <> { fallbackImage ? : null } { durationLabel ? { durationLabel } : null } ); }