-
Notifications
You must be signed in to change notification settings - Fork 1
/
DisplayGameInfo.jsx
276 lines (247 loc) · 10.2 KB
/
DisplayGameInfo.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import ReactPlayer from "react-player";
// Icons
import { FaXbox, FaPlaystation, FaWindows, FaApple, FaLink, FaStarHalfAlt, FaAndroid, FaLinux } from "react-icons/fa";
import { GiJoystick } from "react-icons/gi";
import { SiNintendoswitch, SiNintendo } from "react-icons/si";
import { RiMacbookLine } from "react-icons/ri";
import { FcRating } from "react-icons/fc";
// Components
import GamePageStreams from "../../components/GamePageStreams";
// API
import rawgApi from "../../services/rawgApi";
import youtubeAPI from "../../services/youtubeAPI";
const DisplayGameInfo = () => {
const { gId } = useParams();
const [gameData, setGameData] = useState(null);
const [error, setError] = useState(null);
const [showFullDescription, setShowFullDescription] = useState(false);
const [isVideoPlaying, setIsVideoPlaying] = useState(false);
const [trailerUrl, setTrailerUrl] = useState(null);
const playerConfig = {
youtube: {
playerVars: {
showinfo: 0,
modestbranding: 1,
},
},
};
useEffect(() => {
const fetchGameData = async () => {
try {
const response = await rawgApi.getGameData(gId);
if (response.data) {
setGameData(response.data);
fetchGameTrailer(response.data.name);
} else {
handleInvalidApiResponse(response);
}
} catch (error) {
handleFetchError();
}
};
fetchGameData();
}, [gId]);
const fetchGameTrailer = async (gameName) => {
try {
const trailerUrl = await youtubeAPI.getGameTrailer(gameName);
setTrailerUrl(trailerUrl);
} catch (error) {
console.error("Error fetching game trailer:", error);
}
};
const handleInvalidApiResponse = (response) => {
setError("Invalid response structure from the API");
console.error("Invalid response structure from the API", response);
};
const handleFetchError = () => {
setError("Failed to fetch game data");
console.error("Failed to fetch game data");
};
const handleVideoPlay = () => {
setIsVideoPlaying(true);
};
const toggleDescription = () => {
setShowFullDescription(!showFullDescription);
};
const renderLink = (text, href) => (
<p className="text-lg">
<a className="text-info hover:underline mt-2 hover:text-accent" href={href} target="_blank" rel="noopener noreferrer">
{text}
</a>
</p>
);
const formatReleaseDate = (dateString) => {
const options = { year: "numeric", month: "short", day: "numeric" };
const formattedDate = new Date(dateString).toLocaleDateString(undefined, options);
return formattedDate;
};
// Helper function to render platform icons
const renderPlatformIcon = (platformName) => {
switch (platformName) {
case "Apple Macintosh":
return <RiMacbookLine />;
case "Xbox":
return <FaXbox />;
case "Android":
return <FaAndroid />;
case "Linux":
return <FaLinux />;
case "PlayStation":
return <FaPlaystation />;
case "PC":
return <FaWindows />;
case "macOS":
return <FaApple />;
case "Nintendo Switch":
return <SiNintendoswitch />;
case "Nintendo":
return <SiNintendo />;
case "Xbox Series X":
return <GiJoystick />;
case "Xbox One":
return <FaXbox />;
default:
// Fallback to text if no icon is available
return <span>{platformName}</span>;
}
};
if (error) {
return (
<div className="p-5 text-text">
<p className="text-xl">Error: {error}</p>
</div>
);
}
if (!gameData) {
return (
<div className="flex items-center justify-center h-screen">
<div role="status">
<span className="text-text">Loading....</span>
</div>
</div>
);
}
return (
<div className="relative text-text p-5">
{/* Background */}
<div className="absolute inset-0 overflow-hidden shadow-lg">
{/* Background overlay */}
<div className="absolute bottom-0 p-5 h-full w-full"></div>
{/* Game background image */}
<img
className="w-full object-cover rounded-md shadow-lg"
style={{ maskImage: "linear-gradient(to bottom, rgba(0, 0, 0, 0.20), rgba(0, 0, 0, 0))" }}
src={gameData.background_image}
alt={gameData.name}
/>
</div>
<div className="relative flex flex-col items-center mt-20 mb-20">
{/* Title, Release Date, and Rating */}
<div className="text-center mb-10">
<h1 className="text-5xl cursor-pointer hover:scale-125 transition duration-500 ease-in-out font-bold mb-6">{gameData.name}</h1>
<p className="text-lg">Released date: {formatReleaseDate(gameData.released)}</p>
<p className="text-xl font-bold flex items-center justify-center gap-1 mt-2">
<FaStarHalfAlt alt="The number of ratings" /> {gameData.rating} / {gameData.rating_top}
</p>
</div>
{/* Trailer and Description */}
<div className="flex flex-col md:flex-row w-full mt-20 mb-20">
<div className="md:w-1/2 md:pr-8 mb-16">
<div className="border border-accent rounded-md overflow-hidden mt-4 mb-5">
<img className="w-full hover:scale-110 duration-300 transition ease-in-out object-cover rounded-md" src={gameData.background_image} alt={gameData.name} />
</div>
{/* Video Placeholder For Youtube Trailer */}
<div className="border border-accent rounded-md overflow-hidden">
<ReactPlayer
url={trailerUrl}
width="100%"
height="500px"
controls
onPlay={handleVideoPlay}
config={playerConfig}
style={{ opacity: isVideoPlaying ? 1 : 0.5, transition: "opacity 0.5s" }}
/>
</div>
</div>
<div className="md:w-1/2 md:pl-8">
<div className="mb-6">
<h2 className="text-2xl font-bold mb-4">Game Description</h2>
<p className={`text-lg leading-relaxed ${showFullDescription ? "whitespace-pre-line" : "line-clamp-3"}`}>{gameData.description_raw}</p>
<button className="text-blue-500 hover:underline mt-2" onClick={toggleDescription}>
{showFullDescription ? "Show Less" : "Show More"}
</button>
</div>
{/* Metacritic Score */}
<div className="flex justify-between mb-6">
<div>
<h3 className="text-2xl font-bold mb-1 flex gap-2 items-center">
<FcRating alt="MetaCritic Rating" /> Metacritic Score: {gameData.metacritic}
</h3>
{renderLink("Metacritic URL", gameData.metacritic)}
</div>
</div>
{/* Additional Links */}
<div className="mb-6">
<h3 className="text-2xl font-bold mb-1 flex gap-2 items-center ">
{" "}
<FaLink />
Additional Links
</h3>
{renderLink("Game Website", gameData.website)}
{renderLink("Reddit", gameData.reddit_url)}
</div>
{/* Developers */}
<div className="mb-6">
<h3 className="text-2xl font-bold mb-4">Developers</h3>
<div className="flex flex-wrap">
{gameData.developers.map((dev) => (
<p key={dev.id} className="text-lg mr-4">
{dev.name}
</p>
))}
</div>
</div>
{/* Genres */}
<div className="mb-6">
<h3 className="text-2xl font-bold mb-4">Genres</h3>
<div className="flex flex-wrap">
{gameData.genres.map((genre) => (
<div key={genre.id} className="flex items-center mt-2 mr-4">
<img src={genre.image_background} alt={genre.name} className="w-6 h-6 object-cover rounded-lg mr-2" />
<p className="text-lg">{genre.name}</p>
</div>
))}
</div>
</div>
{/* Tags */}
<div className="mb-6">
<h3 className="text-2xl font-bold mb-4">Tags</h3>
<div className="flex flex-wrap">
{gameData.tags.map((tag) => (
<p key={tag.id} className="text-lg mr-4">
{tag.name}
</p>
))}
</div>
</div>
{/* Platforms */}
<div className="mb-6">
<h3 className="text-2xl font-bold mb-4">Platforms</h3>
<div className="flex flex-wrap">
{gameData.parent_platforms.map((platform, index) => (
<p key={index} className="text-lg mr-4 hover:scale-150 duration-300 ease-in-out">
{renderPlatformIcon(platform.platform.name)}
</p>
))}
</div>
</div>
</div>
</div>
<GamePageStreams gameName={gameData.name} />
</div>
</div>
);
};
export default DisplayGameInfo;