"use client" import { useState, useEffect } from "react" interface YoutubeVideoProps { url: string | null | undefined } // Extract YouTube video ID from various URL formats function extractVideoId(url: string): string | null { if (!url) return null const patterns = [ /(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([^&\n?#]+)/, /youtube\.com\/watch\?.*v=([^&\n?#]+)/, ] for (const pattern of patterns) { const match = url.match(pattern) if (match?.[1]) { return match[1] } } return null } export function YoutubeVideo({ url }: YoutubeVideoProps) { const [videoId, setVideoId] = useState(null) const [loading, setLoading] = useState(true) const [error, setError] = useState(null) useEffect(() => { if (!url) { setError("No YouTube URL provided") setLoading(false) return } const id = extractVideoId(url) if (!id) { setError("Invalid YouTube URL format") setLoading(false) return } setVideoId(id) setLoading(false) setError(null) }, [url]) if (!url) { return (
No YouTube URL provided
) } if (loading) { return (
Loading video...
) } if (error || !videoId) { return (
Error: {error || "Failed to extract video ID"}
) } return (