-
Notifications
You must be signed in to change notification settings - Fork 1
/
TwitchTopStreams.jsx
105 lines (99 loc) · 4.82 KB
/
TwitchTopStreams.jsx
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { useEffect, useState } from "react";
import ReactPlayer from "react-player";
import twitchApi from "../services/twitchApi";
import "../assets/styles/twitch.css";
const TwitchTopStreams = ({ gameId, limit }) => {
// State for top streams from Twitch Api
const [twitchTopStreams, setTwitchTopStreams] = useState([]);
// State to track the currently selected stream
const [selectedStream, setSelectedStream] = useState(null);
useEffect(() => {
// Fetch top streams when the component mounts
fetchTwitchTopStreams(gameId, limit);
}, []);
// Get top streams from Twitch Api
const fetchTwitchTopStreams = async (gameId, limit = 100) => {
try {
const response = await twitchApi.getTwitchStreams({
params: {
game_id: gameId,
first: limit,
sort: "viewers", // Sort by viewership
},
});
setTwitchTopStreams(response.data.data);
} catch (error) {
console.error("Error fetching twitch streams:", error);
}
};
// Handle thumbnail click and start playing the stream
const handleThumbnailClick = (stream) => {
setSelectedStream(stream);
};
return (
<div className="p-5">
<h2 className="text-3xl font-bold text-gray-400 mb-4 mt-6 text-center">
Top 100 Most viewed Live Streams on Twitch
</h2>
<div className="flex flex-wrap justify-center">
{twitchTopStreams.map((stream) => (
<div
key={stream.id}
className="w-auto sm:w-1/2 md:w-1/3 lg:w-1/4 xl:w-1/4 bg-secondary rounded overflow-hidden shadow-lg m-5 group hover:scale-105 transition-all duration-300 ease cursor-pointer"
>
{selectedStream && selectedStream.id === stream.id ? (
<div className="player-wrapper">
<ReactPlayer
className="react-player"
url={`https://www.twitch.tv/${stream.user_name}`}
width="100%"
height="100%"
controls
playing
/>
</div>
) : (
<div className="thumbnail-container relative w-full h-40 cursor-pointer">
{stream.thumbnail_url ? (
<img
src={stream.thumbnail_url.replace("{width}", "640").replace("{height}", "360")}
alt={`Thumbnail for ${stream.user_name}`}
width="100%"
height="100%"
onClick={() => handleThumbnailClick(stream)}
className="w-full h-full object-cover rounded-md mb-2 scale-95"
/>
) : (
<div className="placeholder w-full h-full bg-gray-300 m-3 flex items-center justify-center">
<span className="text-gray-500">
No Thumbnail
</span>
</div>
)}
<div className="play-button absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2" onClick={() => handleThumbnailClick(stream)}>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
width="48"
height="48"
fill="#fff"
>
<path d="M8 5v14l11-7z" />
</svg>
</div>
</div>
)}
<div className="px-6 py-4">
<div className="font-bold text-base mb-2">
<h3 className="text-lg font-semibold">
{stream.user_name}</h3>
</div>
<p>Viewers: {stream.viewer_count}</p>
</div>
</div>
))}
</div>
</div>
);
}
export default TwitchTopStreams;