blob: 65c15ad20297fcd0ac7b968731f15f84d9c0268d (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
"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<string | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(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 (
<div className="flex items-center justify-center h-full text-gray-400">
No YouTube URL provided
</div>
)
}
if (loading) {
return (
<div className="flex items-center justify-center h-full text-gray-400">
Loading video...
</div>
)
}
if (error || !videoId) {
return (
<div className="flex items-center justify-center h-full text-red-400">
Error: {error || "Failed to extract video ID"}
</div>
)
}
return (
<div className="flex-1 flex items-center justify-center w-full p-4">
<div className="w-full max-w-4xl aspect-video">
<iframe
src={`https://www.youtube.com/embed/${videoId}`}
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
allowFullScreen
className="w-full h-full rounded-lg shadow-lg"
/>
</div>
</div>
)
}
|