import { useState } from 'react'; import { cn } from '@/lib/utils'; import { extractYouTubeVideoId, generateYouTubeEmbedUrl } from '@/lib/video-utils'; interface YouTubeEmbedProps { /** YouTube video URL or video ID */ url: string; /** Video title for accessibility */ title?: string; /** Optional caption below the video */ caption?: string; /** Additional CSS classes */ className?: string; /** Autoplay the video (muted on mobile) */ autoplay?: boolean; /** Show video controls */ controls?: boolean; /** Reduce YouTube branding */ modestbranding?: boolean; /** Show related videos at the end */ showRelated?: boolean; /** Start time in seconds */ startTime?: number; /** End time in seconds */ endTime?: number; /** Show loading state */ showLoading?: boolean; } export function YouTubeEmbed({ url, title = 'YouTube video', caption, className, autoplay = false, controls = true, modestbranding = true, showRelated = false, startTime, endTime, showLoading = true, }: YouTubeEmbedProps) { const [isLoaded, setIsLoaded] = useState(false); const [hasError, setHasError] = useState(false); // Extract video ID from URL const videoId = extractYouTubeVideoId(url); if (!videoId) { return (

Invalid YouTube URL: {url}

); } // Generate embed URL with options const embedUrl = generateYouTubeEmbedUrl(videoId, { autoplay, controls, modestbranding, rel: showRelated, playsinline: true, start: startTime, end: endTime, }); return (
{/* Video container with aspect ratio */}
{/* Loading state */} {showLoading && !isLoaded && !hasError && (
)} {/* Error state */} {hasError && (

Video failed to load

The YouTube video could not be loaded. Please check the URL or try again later.

)} {/* YouTube iframe */} {!hasError && (