-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRankingLists.tsx
More file actions
195 lines (186 loc) · 5.35 KB
/
RankingLists.tsx
File metadata and controls
195 lines (186 loc) · 5.35 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
import { useEffect, useMemo, useState } from "react";
import {
Box,
Button,
Grid,
Stack,
type SxProps,
type Theme,
Typography,
} from "@mui/material";
import { grey } from "@mui/material/colors";
import RankingTitleBadge from "./RankingTitleBadge";
import { formatMicroseconds } from "../../../lib/utils/ranking.ts";
import { getMedalIcon } from "../../../components/common/medal.tsx";
import { fetchCodes } from "../../../api/api.ts";
import { CodeDialog } from "./CodeDialog.tsx";
interface RankingItem {
file_name: string;
prev_score: number;
rank: number;
score: number;
user_name: string;
submission_id: number;
}
interface RankingsListProps {
rankings: Record<string, RankingItem[]>;
leaderboardId?: string;
}
const styles: Record<string, SxProps<Theme>> = {
rankingListSection: {
mt: 5,
},
rankingRow: {
borderBottom: "1px solid #ddd",
},
header: {
display: "flex",
justifyContent: "space-between",
alignItems: "center",
mb: 1,
},
fieldLabel: {
fontWeight: "bold",
fontSize: "1.1rem",
textTransform: "capitalize",
},
row: {
display: "flex",
gap: 2,
fontSize: "0.95rem",
alignItems: "center",
flexWrap: "wrap",
},
name: {
fontWeight: 800,
minWidth: "90px",
},
score: {
fontFamily: "monospace",
minWidth: "100px",
},
delta: {
color: grey[600],
minWidth: "90px",
},
};
export default function RankingsList({
rankings,
leaderboardId,
}: RankingsListProps) {
const [expanded, setExpanded] = useState<Record<string, boolean>>({});
const [colorHash, _] = useState<string>(
Math.random().toString(36).slice(2, 8),
);
const [codes, setCodes] = useState<Map<number, string>>(new Map());
const submissionIds = useMemo(() => {
if (!rankings) return [];
const ids: number[] = [];
Object.entries(rankings).forEach(([key, value]) => {
const li = value as any[];
if (Array.isArray(li) && li.length > 0) {
li.forEach((item) => {
if (item?.submission_id) {
ids.push(item.submission_id);
}
});
}
});
return ids;
}, [rankings]);
useEffect(() => {
if (!submissionIds || submissionIds.length === 0 || !leaderboardId) return;
fetchCodes(leaderboardId, submissionIds)
.then((data) => {
const map = new Map<number, string>();
for (const item of data?.results ?? []) {
map.set(item.submission_id, item.code);
}
setCodes(map);
})
.catch((err) => {
// soft error handle it since it's not critical
console.warn("[RankingsList] Failed to fetch codes:", err);
});
}, [leaderboardId, submissionIds]);
const toggleExpanded = (field: string) => {
setExpanded((prev) => ({
...prev,
[field]: !prev[field],
}));
};
return (
<Stack spacing={3} sx={styles.rankingListSection}>
{Object.entries(rankings).map(([field, items], ridx) => {
if (items.length == 0) {
return (
<Box key={field}>
<Box sx={styles.header}>
<RankingTitleBadge name={field} colorHash={colorHash} />
</Box>
<Stack spacing={0.5}>
<span> no submissions</span>
</Stack>
</Box>
);
}
const isExpanded = expanded[field] ?? false;
const visibleItems = isExpanded ? items : items.slice(0, 3);
return (
<Box key={field}>
<Box sx={styles.header}>
<RankingTitleBadge name={field} colorHash={colorHash} />
{items.length > 3 && (
<Button
data-testid={`ranking-show-all-button-${ridx}`}
onClick={() => toggleExpanded(field)}
size="small"
>
{isExpanded ? "Hide" : `Show all (${items.length})`}
</Button>
)}
</Box>
<Stack spacing={0.5}>
{visibleItems.map((item, idx) => (
<Grid
container
spacing={2}
marginBottom={2}
key={idx}
sx={styles.rankingRow}
data-testid={`ranking-${ridx}-row`}
>
<Grid size={3}>
<Typography sx={styles.name}>
{item.user_name} {getMedalIcon(item.rank)}
</Typography>
</Grid>
<Grid size={3}>
<Typography sx={styles.score}>
{formatMicroseconds(item.score)}
</Typography>
</Grid>
<Grid size={3}>
<Typography sx={styles.delta}>
{item.prev_score > 0 &&
`+${formatMicroseconds(item.prev_score)}`}
</Typography>
</Grid>
<Grid size={3}>
<Typography>
<CodeDialog
code={codes.get(item?.submission_id)}
fileName={item.file_name}
submissionId={item?.submission_id}
/>
</Typography>
</Grid>
</Grid>
))}
</Stack>
</Box>
);
})}
</Stack>
);
}