-
Notifications
You must be signed in to change notification settings - Fork 1
/
GamePageStreams.jsx
122 lines (115 loc) · 5.89 KB
/
GamePageStreams.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, { useEffect, useState } from "react";
import ReactPlayer from "react-player";
import Loading from "../components/Loading";
import twitchApi from "../services/twitchApi";
import "../assets/styles/twitch.css";
const GamePageStreams = ({ gameName }) => {
const [streamsByGameId, setStreamsByGameId] = useState([]);
const [selectedStream, setSelectedStream] = useState(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
fetchTwitchGameId(gameName);
}, [gameName]);
// Get the game ID from Twitch API by game name
const fetchTwitchGameId = async (gameName) => {
try {
const response = await twitchApi.getTwitchGameId(gameName);
const twitchGameId = response.data.data[0]?.id;
if (twitchGameId) {
fetchTwitchStreamsByGameId(twitchGameId);
}
} catch (error) {
console.error("Error fetching twitch game ID:", error);
setIsLoading(false);
}
};
// Get top streams from Twitch API using the game ID
const fetchTwitchStreamsByGameId = async (twitchGameId) => {
try {
const response = await twitchApi.getTwitchStreamsByGameId(twitchGameId);
setStreamsByGameId(response.data.data);
setIsLoading(false);
} catch (error) {
console.error("Error fetching twitch streams by game ID:", error);
setIsLoading(false);
}
};
// Handle thumbnail click and start playing the stream
const handleThumbnailClick = (stream) => {
setSelectedStream(stream);
};
return (
<div className="text-text">
<h2 className="text-3xl font-bold text-gray-400 mb-4 mt-6 text-center">
Most viewed Live Streams on Twitch - {gameName}
</h2>
{isLoading ? (
<Loading />
) : (
<div className="flex flex-wrap justify-center">
{streamsByGameId.length === 0 ? (
<p>No live streams available.</p>
) : (
streamsByGameId.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-text text-lg font-semibold">
{stream.user_name}</h3>
</div>
<p className="text-text">Viewers: {stream.viewer_count}</p>
</div>
</div>
))
)}
</div>
)}
</div>
);
}
export default GamePageStreams;