forked from codechefPesuecc/CodeChef-PESUECC-Chapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
243 lines (229 loc) · 8.46 KB
/
Copy pathpage.tsx
File metadata and controls
243 lines (229 loc) · 8.46 KB
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
import type { Metadata } from "next";
import Link from "next/link";
import { redirect } from "next/navigation";
import { getCurrentUser } from "@/server/auth/session";
import { getUserSubmissions, getProfileStats } from "@/server/profile";
import { FLAG_LIMIT, ordinal } from "@/lib/points";
import MechaPanel from "@/components/cp-arena/MechaPanel";
export const metadata: Metadata = {
title: "Your profile",
};
// Reads the session cookie + DB — never prerender.
export const dynamic = "force-dynamic";
const MONTHS = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
];
function formatDay(ms: number): string {
const d = new Date(ms);
return `${MONTHS[d.getMonth()]} ${d.getDate()}, ${d.getFullYear()}`;
}
function formatClock(seconds: number | null): string {
if (seconds == null) return "—";
const m = Math.floor(seconds / 60);
const s = seconds % 60;
return `${m}:${String(s).padStart(2, "0")}`;
}
const VERDICT_STYLES: Record<string, string> = {
AC: "bg-emerald-500/15 text-emerald-700 dark:text-emerald-400",
WA: "bg-red-500/15 text-red-700 dark:text-red-400",
TLE: "bg-amber-500/15 text-amber-700 dark:text-amber-400",
RE: "bg-red-500/15 text-red-700 dark:text-red-400",
CE: "bg-red-500/15 text-red-700 dark:text-red-400",
};
export default async function ProfilePage() {
const user = await getCurrentUser();
if (!user) redirect("/login");
const subs = await getUserSubmissions(user.id);
const stats = await getProfileStats(user.srn ?? user.prn, subs.length);
return (
<main className="flex-1">
<section className="mx-auto max-w-4xl px-6 pt-6 pb-24">
{/* Identity header */}
<div className="flex flex-wrap items-center gap-4">
<span className="flex h-16 w-16 items-center justify-center rounded-2xl bg-bronze/15 font-display text-2xl font-bold text-bronze">
{user.username.charAt(0).toUpperCase()}
</span>
<div>
<h1 className="font-display text-2xl font-bold tracking-tight text-chocolate sm:text-3xl">
@{user.username}
</h1>
<p className="mt-1 text-sm text-charcoal/60">
Member since {formatDay(user.createdAt)}
</p>
</div>
<Link
href="/cp-arena"
className="mecha-btn mecha-btn--solid mecha-btn--sm ml-auto"
>
Today's problem
</Link>
</div>
{/* Private identity (only the owner sees this page) */}
<MechaPanel className="mt-6" label="Identity">
<div className="flex flex-wrap gap-x-8 gap-y-2 px-5 pb-4 pt-3 text-sm">
<DetailRow label="Email">
<span className="text-charcoal/80">{user.email}</span>
{user.emailVerified ? (
<span className="mecha-chip ml-2 bg-emerald-500/15 text-emerald-700 dark:text-emerald-400">
verified
</span>
) : (
<Link
href="/verify"
className="mecha-chip ml-2 bg-amber-500/15 text-amber-700 hover:underline dark:text-amber-400"
>
verify
</Link>
)}
</DetailRow>
<DetailRow label="PRN">
<span className="font-mono text-charcoal/80">{user.prn}</span>
</DetailRow>
<DetailRow label="SRN">
<span className="font-mono text-charcoal/80">
{user.srn ?? "—"}
</span>
</DetailRow>
</div>
</MechaPanel>
{/* Stats */}
<div className="mt-6 grid grid-cols-2 gap-3 sm:grid-cols-4">
<StatCard
label="All-time"
value={stats.allPoints.toLocaleString()}
sub={stats.allRank ? `${ordinal(stats.allRank)} overall` : "unranked"}
/>
<StatCard
label="This month"
value={stats.monthPoints.toLocaleString()}
sub={stats.monthRank ? `${ordinal(stats.monthRank)} this month` : "unranked"}
/>
<StatCard label="Solved" value={String(stats.solved)} sub="problems" />
<StatCard
label="Submissions"
value={String(stats.submissions)}
sub="recorded"
/>
</div>
{/* Submission history */}
<h2 className="mt-10 font-display text-lg font-bold text-chocolate">
Submission history
</h2>
{subs.length === 0 ? (
<MechaPanel className="mt-4">
<div className="px-6 py-10 text-center">
<p className="text-sm text-charcoal/60">
No ranked submissions yet.
</p>
<Link
href="/cp-arena"
className="mt-3 inline-block text-sm font-semibold text-bronze hover:underline"
>
Solve today's Problem of the Day
</Link>
</div>
</MechaPanel>
) : (
<MechaPanel className="mt-4">
<table className="w-full text-sm">
<thead>
<tr className="border-b border-hairline text-left font-mono text-[11px] uppercase tracking-wider text-charcoal/45">
<th className="px-4 py-3 font-medium">Problem</th>
<th className="px-4 py-3 font-medium">Verdict</th>
<th className="hidden px-4 py-3 font-medium sm:table-cell">
Lang
</th>
<th className="hidden px-4 py-3 font-medium sm:table-cell">
Time
</th>
<th className="px-4 py-3 text-right font-medium">When</th>
</tr>
</thead>
<tbody className="divide-y divide-hairline">
{subs.map((s) => (
<tr key={s.id} className="transition-colors hover:bg-cream/40 dark:hover:bg-white/[0.02]">
<td className="px-4 py-3">
<Link
href={`/cp-arena/archive/${s.slug}`}
className="font-medium text-chocolate hover:text-bronze"
>
{s.title}
</Link>
{s.flags > 0 && (
<span
className={`mecha-chip ml-2 ${
s.flags > FLAG_LIMIT
? "bg-red-500/15 text-red-700 dark:text-red-400"
: "bg-amber-500/15 text-amber-700 dark:text-amber-400"
}`}
>
{s.flags} flag{s.flags === 1 ? "" : "s"}
</span>
)}
</td>
<td className="px-4 py-3">
<span
className={`mecha-chip ${
VERDICT_STYLES[s.status] ?? "bg-charcoal/10 text-charcoal/60"
}`}
>
{s.status}
</span>
</td>
<td className="hidden px-4 py-3 font-mono text-xs text-charcoal/60 sm:table-cell">
{s.language}
</td>
<td className="hidden px-4 py-3 font-mono text-xs text-charcoal/60 sm:table-cell">
{formatClock(s.elapsedSeconds)}
</td>
<td className="px-4 py-3 text-right text-xs text-charcoal/50">
{formatDay(s.createdAt)}
</td>
</tr>
))}
</tbody>
</table>
</MechaPanel>
)}
</section>
</main>
);
}
function DetailRow({
label,
children,
}: {
label: string;
children: React.ReactNode;
}) {
return (
<div className="flex items-center gap-2">
<span className="font-mono text-[11px] uppercase tracking-wider text-charcoal/40">
{label}
</span>
<span className="flex items-center">{children}</span>
</div>
);
}
function StatCard({
label,
value,
sub,
}: {
label: string;
value: string;
sub: string;
}) {
return (
<MechaPanel bodyClassName="px-4 py-4">
<div className="font-mono text-[11px] uppercase tracking-wider text-charcoal/45">
{label}
</div>
<div className="mt-1 font-display text-2xl font-bold text-chocolate">
{value}
</div>
<div className="text-[11px] text-charcoal/50">{sub}</div>
</MechaPanel>
);
}