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
|
import { useState, useEffect } from "react";
import { useNavigate } from "react-router";
import { isBrowser } from "react-device-detect";
import LoadingDisplay from "../components/LoadingDisplay";
import SessionExpiredPopup from "../components/SessionExpiredPopup";
import { NavBar } from "../components/NavBar";
import { useAuth } from "../contexts/AuthContext";
import Heatmap from "../components/Heatmap";
import type { HeatmapData } from "../components/Heatmap";
import type { User } from "../utils/authApi";
interface recentPlayedGame {
gameInternalName: string;
formattedName: string;
timestamp: number;
}
interface UserData {
user: User;
recentPlayedGames: recentPlayedGame[];
isAdmin: boolean;
}
const Profile = () => {
const { user, isLoading, logout } = useAuth();
const targetUser =
new URLSearchParams(window.location.search).get("userId") || ""; // looking at profile of this user
const navigate = useNavigate();
const [fetchingHeatmapData, setFetchingHeatmapData] = useState(true);
const [fetchingUserData, setFetchingUserData] = useState(true);
const [heatmapData, setHeatmapData] = useState<HeatmapData>({ data: [] });
const [userData, setUserData] = useState<UserData | null>(null);
useEffect(() => {
if (targetUser) {
setFetchingUserData(true);
const fetchUserData = async () => {
try {
const response = await fetch(
new URL(
import.meta.env.VITE_API_URL + "/me?userId=" + targetUser,
),
{ credentials: "include" },
);
const data = await response.json();
setUserData(data);
setFetchingUserData(false);
} catch (error) {
console.error("Failed to fetch user data:", error);
}
};
fetchUserData();
}
}, [targetUser]);
useEffect(() => {
if (targetUser) {
setFetchingHeatmapData(true);
const fetchHeatmapData = async () => {
try {
const response = await fetch(
new URL(
import.meta.env.VITE_API_URL + "/heatmap?userId=" + targetUser,
),
{ credentials: "include" },
);
const data = await response.json();
return data;
} catch (error) {
setFetchingHeatmapData(false);
console.error("Failed to fetch heatmap data:", error);
throw error;
}
};
fetchHeatmapData().then((data) => {
const heatmapDates: { [key: string]: number } = {};
for (let i = 0; i < data.scores.length; i++) {
const date = new Date(data.scores[i].timestamp);
const dateString = date.toDateString();
if (!heatmapDates[dateString]) {
heatmapDates[dateString] = 1;
} else {
heatmapDates[dateString] += 1;
}
}
setHeatmapData({
data: Object.entries(heatmapDates).map(([date, count]) => ({
date,
count,
})),
});
setFetchingHeatmapData(false);
});
}
}, [targetUser]);
if (!targetUser) {
navigate("/");
}
if (!user) {
return <SessionExpiredPopup />;
}
if (isLoading || fetchingHeatmapData || fetchingUserData) {
return <LoadingDisplay message="Loading Profile Page..." />;
}
const handleLogout = async () => {
try {
await logout();
navigate("/");
} catch (error) {
console.error("Logout failed:", error);
alert("Network error during logout. Please try again.");
}
};
if (!user) {
return <SessionExpiredPopup />;
}
return (
<div className="min-h-screen bg-slate-950">
<NavBar user={user} handleLogout={handleLogout} currentPage="" />
{/* Main Content */}
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4 sm:py-8">
{/* Header */}
<div className="mb-6 sm:mb-8">
<h1 className="text-2xl sm:text-3xl font-bold text-white mb-2">
{user.username}
</h1>
<p className="text-sm sm:text-base text-slate-400">
{userData?.user.bio ? userData.user.bio.replace(/</g, '<').replace(/>/g, '>') : "I'm a fairly non-descript person"}
</p>
</div>
<div className="mb-6 sm:mb-8">
<h2 className="text-xl sm:text-2xl font-bold text-white mb-4">Recently Played Games</h2>
<ul className="list-disc list-inside space-y-2 text-white">
{userData?.recentPlayedGames.map((game, index) => (
<li key={index} className="flex items-center justify-between p-2 bg-slate-800 rounded-lg border border-slate-700">
<span className="font-bold hover:underline"><a href={`/score?game=${game.gameInternalName}&userId=${userData.user.id}`}>{game.formattedName}</a> </span>
<span className="text-sm text-slate-400">{new Date(game.timestamp).toLocaleString()}</span>
</li>
))}
</ul>
</div>
{isBrowser ? (
<div className="flex flex-col items-center justify-center">
<Heatmap data={heatmapData.data} />
</div>
) : null}
</div>
</div>
);
};
export default Profile;
|