-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpage.tsx
75 lines (59 loc) · 2.28 KB
/
page.tsx
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
import type { Metadata } from 'next';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
// Components
import Filters from '@/app/challenges/Filters';
import Challenges from '@/app/challenges/Challenges';
import DisplayToggle from '@/app/challenges/DisplayToggle';
import CTFNotStarted from '@/components/CTFNotStarted';
// Utils
import { getChallenges } from '@/util/challenges';
import { getMyProfile } from '@/util/profile';
import { getAdminChallenges } from '@/util/admin';
import { AUTH_COOKIE_NAME } from '@/util/config';
export const metadata: Metadata = {
title: 'Challenges'
}
export default async function ChallengesPage() {
const c = await cookies();
const token = c.get(AUTH_COOKIE_NAME)!.value;
const challenges = await getChallenges(token);
const profile = await getMyProfile(token);
if (profile.kind === 'badToken')
return redirect('/logout');
if (challenges.kind !== 'goodChallenges') return (
<CTFNotStarted />
);
// Support non-standard properties by sourcing them from the admin endpoint.
const adminData = await getAdminChallData();
let challs = challenges.data;
if (adminData) {
// Filter out challs with prereqs that are not met yet
const solved = new Set(profile.data.solves.map((c) => c.id));
challs = challs.filter((c) => !adminData[c.id].prereqs || adminData[c.id].prereqs!.every((p) => solved.has(p)));
// Inject additional properties back into client challenges
for (const c of challs) {
c.difficulty = adminData[c.id].difficulty;
c.tags = adminData[c.id].tags;
}
}
return (
<div className="container relative pt-32 pb-14 flex flex-col md:flex-row gap-6">
<Filters
challenges={challs}
solves={profile.data.solves}
/>
<Challenges
challenges={challs}
solves={profile.data.solves}
/>
<DisplayToggle />
</div>
);
}
async function getAdminChallData() {
if (!process.env.ADMIN_TOKEN) return;
const res = await getAdminChallenges(process.env.ADMIN_TOKEN);
if (res.kind !== 'goodChallenges') return;
return Object.fromEntries(res.data.map((c) => [c.id, c]));
}