aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/pages/User/User.tsx
blob: 30c9e4596193be752d56d42a14a939b34799d6d8 (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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import React from "react";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { Helmet } from "react-helmet";

import {
  SteamIcon,
  TwitchIcon,
  YouTubeIcon,
  PortalIcon,
  FlagIcon,
  StatisticsIcon,
  SortIcon,
  ThreedotIcon,
  DownloadIcon,
  HistoryIcon,
} from "@images/Images";
import { UserProfile } from "@customTypes/Profile.ts";
import { Game, GameChapters } from "@customTypes/Game.ts";
import { Map } from "@customTypes/Map.ts";
import { API } from "@api/Api.ts";
import { ticks_to_time } from "@utils/Time.ts";
import useMessage from "@hooks/UseMessage.tsx";

interface UserProps {
  profile?: UserProfile;
  token?: string;
  gameData: Game[];
}

const User: React.FC<UserProps> = ({ token, profile, gameData }) => {
  const { message, MessageDialogComponent } = useMessage();

  const [user, setUser] = React.useState<UserProfile | undefined>(undefined);

  const [navState, setNavState] = React.useState(0);
  const [pageNumber, setPageNumber] = React.useState(1);
  const [pageMax, setPageMax] = React.useState(0);

  const [game, setGame] = React.useState("0");
  const [chapter, setChapter] = React.useState("0");
  const [chapterData, setChapterData] = React.useState<GameChapters | null>(
    null
  );
  const [maps, setMaps] = React.useState<Map[]>([]);

  const location = useLocation();
  const navigate = useNavigate();

  const _fetch_user = React.useCallback(async () => {
    const userID = location.pathname.split("/")[2];
    if (token && profile && profile.profile && profile.steam_id === userID) {
      navigate("/profile");
      return;
    }
    const userData = await API.get_user(userID);
    setUser(userData);
  }, [location.pathname, token, profile, navigate]);

  const _get_game_chapters = React.useCallback(async () => {
    if (game !== "0") {
      const gameChapters = await API.get_games_chapters(game);
      setChapterData(gameChapters);
    } else {
      setPageMax(Math.ceil(user!.records.length / 20));
      setPageNumber(1);
    }
  }, [game, user]);

  const _get_game_maps = React.useCallback(async () => {
    if (chapter === "0") {
      const gameMaps = await API.get_game_maps(game);
      setMaps(gameMaps);
      setPageMax(Math.ceil(gameMaps.length / 20));
      setPageNumber(1);
    } else {
      const gameChapters = await API.get_chapters(chapter);
      setMaps(gameChapters.maps);
      setPageMax(Math.ceil(gameChapters.maps.length / 20));
      setPageNumber(1);
    }
  }, [chapter, game]);

  React.useEffect(() => {
    _fetch_user();
  }, [location, _fetch_user]);

  React.useEffect(() => {
    if (user) {
      _get_game_chapters();
    }
  }, [user, game, location, _get_game_chapters]);

  React.useEffect(() => {
    if (user && game !== "0") {
      _get_game_maps();
    }
  }, [user, game, chapter, location, _get_game_maps]);

  if (!user) {
    return (
      <div className="flex justify-center items-center h-[50vh] text-lg text-foreground">
        Loading...
      </div>
    );
  }

  return (
    <main className="ml-20 overflow-auto overflow-x-hidden relative w-[calc(100%px)] h-screen font-[--font-barlow-semicondensed-regular] text-foreground text-xl">
      <Helmet>
        <title>LPHUB | {user.user_name}</title>
        <meta name="description" content={user.user_name} />
      </Helmet>
      
      {MessageDialogComponent}

      <section className="m-5 bg-gradient-to-t from-[#202232] from-50% to-[#2b2e46] to-50% rounded-3xl p-[30px] mb-[30px] text-foreground">
        <div className="grid grid-cols-[200px_1fr_auto] items-center gap-[25px] mb-[25px]">
          <img 
            src={user.avatar_link} 
            alt="Profile" 
            className="w-[120px] h-[120px] rounded-full border-[3px] border-[rgba(205,207,223,0.2)]" 
          />
          <div>
            <h1 className="m-0 mb-[10px] text-[50px] font-bold text-white font-[--font-barlow-semicondensed-regular]">
              {user.user_name}
            </h1>
            {user.country_code !== "XX" && (
              <div className="flex items-center gap-3 mb-[15px]">
                <img
                  src={`https://flagcdn.com/w80/${user.country_code.toLowerCase()}.jpg`}
                  alt={user.country_code}
                  className="w-6 h-4 rounded-[10px]"
                />
                <span>{user.country_code}</span>
              </div>
            )}
            <div className="flex flex-wrap gap-2">
              {user.titles.map((title, index) => (
                <span
                  key={index}
                  className="py-[6px] px-5 pt-[6px] rounded-[10px] text-lg font-normal text-white"
                  style={{ backgroundColor: `#${title.color}` }}
                >
                  {title.name}
                </span>
              ))}
            </div>
          </div>
          <div className="flex gap-[15px] items-center pr-[10px]">
            {user.links.steam !== "-" && (
              <a href={user.links.steam} className="flex items-center justify-center transition-all duration-200 hover:-translate-y-0.5">
                <img src={SteamIcon} alt="Steam" className="h-[50px] px-[5px] scale-90 brightness-200" />
              </a>
            )}
            {user.links.twitch !== "-" && (
              <a href={user.links.twitch} className="flex items-center justify-center transition-all duration-200 hover:-translate-y-0.5">
                <img src={TwitchIcon} alt="Twitch" className="h-[50px] px-[5px] scale-90 brightness-200" />
              </a>
            )}
            {user.links.youtube !== "-" && (
              <a href={user.links.youtube} className="flex items-center justify-center transition-all duration-200 hover:-translate-y-0.5">
                <img src={YouTubeIcon} alt="YouTube" className="h-[50px] px-[5px] scale-90 brightness-200" />
              </a>
            )}
            {user.links.p2sr !== "-" && (
              <a href={user.links.p2sr} className="flex items-center justify-center transition-all duration-200 hover:-translate-y-0.5">
                <img src={PortalIcon} alt="P2SR" className="h-[50px] px-[5px] scale-90 brightness-200" />
              </a>
            )}
          </div>
        </div>

        <div className="grid grid-cols-3 gap-3 mt-24">
          <div className="m-3 bg-[#2b2e46] rounded-[20px] p-5 text-center grid place-items-center grid-rows-[40%_50%]">
            <div className="text-inherit text-lg">Overall</div>
            <div className="text-white text-[40px]">
              {user.rankings.overall.rank === 0 ? "N/A" : `#${user.rankings.overall.rank}`}
            </div>
            <div className="text-white text-xl">
              {user.rankings.overall.completion_count}/{user.rankings.overall.completion_total}
            </div>
          </div>
          <div className="m-3 bg-[#2b2e46] rounded-[20px] p-5 text-center grid place-items-center grid-rows-[40%_50%]">
            <div className="text-inherit text-lg">Singleplayer</div>
            <div className="text-white text-[40px]">
              {user.rankings.singleplayer.rank === 0 ? "N/A" : `#${user.rankings.singleplayer.rank}`}
            </div>
            <div className="text-white text-xl">
              {user.rankings.singleplayer.completion_count}/{user.rankings.singleplayer.completion_total}
            </div>
          </div>
          <div className="m-3 bg-[#2b2e46] rounded-[20px] p-5 text-center grid place-items-center grid-rows-[40%_50%]">
            <div className="text-inherit text-lg">Cooperative</div>
            <div className="text-white text-[40px]">
              {user.rankings.cooperative.rank === 0 ? "N/A" : `#${user.rankings.cooperative.rank}`}
            </div>
            <div className="text-white text-xl">
              {user.rankings.cooperative.completion_count}/{user.rankings.cooperative.completion_total}
            </div>
          </div>
        </div>
      </section>

      <section className="m-5 h-[60px] grid grid-cols-2">
        <button 
          className={`flex justify-center items-center gap-2 bg-[#2b2e46] border-0 text-inherit font-inherit text-2xl cursor-pointer transition-colors duration-100 rounded-l-3xl hover:bg-[#202232] ${
            navState === 0 ? 'bg-[#202232]' : ''
          }`}
          onClick={() => setNavState(0)}
        >
          <img src={FlagIcon} alt="" className="w-5 h-5 scale-[1.2]" />
          Player Records
        </button>
        <button 
          className={`flex justify-center items-center gap-2 bg-[#2b2e46] border-0 text-inherit font-inherit text-2xl cursor-pointer transition-colors duration-100 rounded-r-3xl hover:bg-[#202232] ${
            navState === 1 ? 'bg-[#202232]' : ''
          }`}
          onClick={() => setNavState(1)}
        >
          <img src={StatisticsIcon} alt="" className="w-5 h-5 scale-[1.2]" />
          Statistics
        </button>
      </section>

      {navState === 0 && (
        <section className="m-5 block bg-[#202232] rounded-3xl overflow-hidden">
          <div className="grid grid-cols-2 mx-5 my-5 mt-[10px] mb-5">
            <select
              className="h-[50px] rounded-3xl text-center text-inherit font-inherit text-2xl border-0 bg-[#2b2e46] mr-[10px]"
              value={game}
              onChange={(e) => {
                setGame(e.target.value);
                setChapter("0");
              }}
            >
              <option value="0">All Games</option>
              {gameData?.map((g) => (
                <option key={g.id} value={g.id}>
                  {g.name}
                </option>
              ))}
            </select>

            <select
              className="h-[50px] rounded-3xl text-center text-inherit font-inherit text-2xl border-0 bg-[#2b2e46] mr-[10px] disabled:opacity-50"
              value={chapter}
              onChange={(e) => setChapter(e.target.value)}
              disabled={game === "0"}
            >
              <option value="0">All Chapters</option>
              {chapterData?.chapters
                .filter(c => !c.is_disabled)
                .map((c) => (
                  <option key={c.id} value={c.id}>
                    {c.name}
                  </option>
                ))}
            </select>
          </div>

          <div className="h-[34px] grid text-xl pl-[60px] mx-5 my-0 grid-cols-[15%_15%_5%_15%_5%_15%_15%_15%]">
            <div className="flex place-items-end cursor-pointer">
              <span>Map Name</span>
              <img src={SortIcon} alt="Sort" className="h-5 scale-[0.8]" />
            </div>
            <div className="flex place-items-end cursor-pointer">
              <span>Portals</span>
              <img src={SortIcon} alt="Sort" className="h-5 scale-[0.8]" />
            </div>
            <div className="flex place-items-end cursor-pointer">
              <span>WRΔ</span>
              <img src={SortIcon} alt="Sort" className="h-5 scale-[0.8]" />
            </div>
            <div className="flex place-items-end cursor-pointer">
              <span>Time</span>
              <img src={SortIcon} alt="Sort" className="h-5 scale-[0.8]" />
            </div>
            <div></div>
            <div className="flex place-items-end cursor-pointer">
              <span>Rank</span>
              <img src={SortIcon} alt="Sort" className="h-5 scale-[0.8]" />
            </div>
            <div className="flex place-items-end cursor-pointer">
              <span>Date</span>
              <img src={SortIcon} alt="Sort" className="h-5 scale-[0.8]" />
            </div>
            <div className="flex items-center gap-[10px] justify-center">
              <button
                className="w-8 h-8 border border-[#2b2e46] bg-[#2b2e46] rounded cursor-pointer flex items-center justify-center text-foreground transition-colors duration-100 hover:bg-[#202232] disabled:opacity-50 disabled:cursor-not-allowed"
                onClick={() => setPageNumber(Math.max(1, pageNumber - 1))}
                disabled={pageNumber === 1}
              >
                ←
              </button>
              <span className="text-sm text-foreground">{pageNumber}/{pageMax}</span>
              <button
                className="w-8 h-8 border border-[#2b2e46] bg-[#2b2e46] rounded cursor-pointer flex items-center justify-center text-foreground transition-colors duration-100 hover:bg-[#202232] disabled:opacity-50 disabled:cursor-not-allowed"
                onClick={() => setPageNumber(Math.min(pageMax, pageNumber + 1))}
                disabled={pageNumber === pageMax}
              >
                →
              </button>
            </div>
          </div>

          <div>
            {game === "0" ? (
              user.records
                .sort((a, b) => a.map_id - b.map_id)
                .map((record, index) =>
                  Math.ceil((index + 1) / 20) === pageNumber ? (
                    <div key={index} className="w-[calc(100%-40px)] mx-5 my-0 mt-[10px] h-11 rounded-[20px] pl-[40px] text-xl text-inherit font-inherit border-0 transition-colors duration-100 bg-[#2b2e46] grid grid-cols-[15%_15%_5%_15%_5%_15%_15%_15%] overflow-hidden whitespace-nowrap cursor-pointer hover:bg-[#202232]">
                      <Link to={`/maps/${record.map_id}`} className="text-[#3c91e6] no-underline font-inherit flex place-items-center h-11 hover:underline">
                        {record.map_name}
                      </Link>
                      <span className="flex place-items-center h-11">{record.scores[0]?.score_count || 'N/A'}</span>
                      <span className={`flex place-items-center h-11 ${record.scores[0]?.score_count - record.map_wr_count > 0 ? 'text-[#dc3545]' : ''}`}>
                        {record.scores[0]?.score_count - record.map_wr_count > 0
                          ? `+${record.scores[0].score_count - record.map_wr_count}`
                          : '–'}
                      </span>
                      <span className="flex place-items-center h-11">{record.scores[0] ? ticks_to_time(record.scores[0].score_time) : 'N/A'}</span>
                      <span className="flex place-items-center h-11"></span>
                      <span className="flex place-items-center h-11 font-semibold">#{record.placement}</span>
                      <span className="flex place-items-center h-11">{record.scores[0]?.date.split("T")[0] || 'N/A'}</span>
                      <div className="flex gap-[5px] justify-end flex-row-reverse place-items-center h-11">
                        <button
                          className="bg-transparent border-0 cursor-pointer transition-colors duration-100 p-0.5 hover:bg-[rgba(32,34,50,0.5)]"
                          onClick={() => message("Demo Information", `Demo ID: ${record.scores[0]?.demo_id}`)}
                          title="Demo Info"
                        >
                          <img src={ThreedotIcon} alt="Info" className="w-4 h-4" />
                        </button>
                        <button
                          className="bg-transparent border-0 cursor-pointer transition-colors duration-100 p-0.5 hover:bg-[rgba(32,34,50,0.5)]"
                          onClick={() => window.location.href = `/api/v1/demos?uuid=${record.scores[0]?.demo_id}`}
                          title="Download Demo"
                        >
                          <img src={DownloadIcon} alt="Download" className="w-4 h-4" />
                        </button>
                        {record.scores.length > 1 && (
                          <button className="bg-transparent border-0 cursor-pointer transition-colors duration-100 p-0.5 hover:bg-[rgba(32,34,50,0.5)]" title="View History">
                            <img src={HistoryIcon} alt="History" className="w-4 h-4" />
                          </button>
                        )}
                      </div>
                    </div>
                  ) : null
                )
            ) : (
              maps
                ?.filter(map => !map.is_disabled)
                .sort((a, b) => a.id - b.id)
                .map((map, index) => {
                  if (Math.ceil((index + 1) / 20) !== pageNumber) return null;
                  
                  const record = user.records.find(r => r.map_id === map.id);
                  
                  return (
                    <div key={index} className={`w-[calc(100%-40px)] mx-5 my-0 mt-[10px] h-11 rounded-[20px] pl-[40px] text-xl text-inherit font-inherit border-0 transition-colors duration-100 bg-[#2b2e46] grid grid-cols-[15%_15%_5%_15%_5%_15%_15%_15%] overflow-hidden whitespace-nowrap cursor-pointer hover:bg-[#202232] ${!record ? 'opacity-65' : ''}`}>
                      <Link to={`/maps/${map.id}`} className="text-[#3c91e6] no-underline font-inherit flex place-items-center h-11 hover:underline">
                        {map.name}
                      </Link>
                      <span className="flex place-items-center h-11">{record?.scores[0]?.score_count || 'N/A'}</span>
                      <span className={`flex place-items-center h-11 ${record?.scores[0]?.score_count && record.scores[0].score_count - record.map_wr_count > 0 ? 'text-[#dc3545]' : ''}`}>
                        {record?.scores[0]?.score_count && record.scores[0].score_count - record.map_wr_count > 0
                          ? `+${record.scores[0].score_count - record.map_wr_count}`
                          : '–'}
                      </span>
                      <span className="flex place-items-center h-11">{record?.scores[0] ? ticks_to_time(record.scores[0].score_time) : 'N/A'}</span>
                      <span className="flex place-items-center h-11"></span>
                      <span className="flex place-items-center h-11 font-semibold">{record ? `#${record.placement}` : 'N/A'}</span>
                      <span className="flex place-items-center h-11">{record?.scores[0]?.date.split("T")[0] || 'N/A'}</span>
                      <div className="flex gap-[5px] justify-end flex-row-reverse place-items-center h-11">
                        {record?.scores[0] && (
                          <>
                            <button
                              className="bg-transparent border-0 cursor-pointer transition-colors duration-100 p-0.5 hover:bg-[rgba(32,34,50,0.5)]"
                              onClick={() => message("Demo Information", `Demo ID: ${record.scores[0].demo_id}`)}
                              title="Demo Info"
                            >
                              <img src={ThreedotIcon} alt="Info" className="w-4 h-4" />
                            </button>
                            <button
                              className="bg-transparent border-0 cursor-pointer transition-colors duration-100 p-0.5 hover:bg-[rgba(32,34,50,0.5)]"
                              onClick={() => window.location.href = `/api/v1/demos?uuid=${record.scores[0].demo_id}`}
                              title="Download Demo"
                            >
                              <img src={DownloadIcon} alt="Download" className="w-4 h-4" />
                            </button>
                            {record.scores.length > 1 && (
                              <button className="bg-transparent border-0 cursor-pointer transition-colors duration-100 p-0.5 hover:bg-[rgba(32,34,50,0.5)]" title="View History">
                                <img src={HistoryIcon} alt="History" className="w-4 h-4" />
                              </button>
                            )}
                          </>
                        )}
                      </div>
                    </div>
                  );
                })
            )}
          </div>
        </section>
      )}
    </main>
  );
};

export default User;