-
-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathListTable.tsx
More file actions
670 lines (654 loc) · 19 KB
/
ListTable.tsx
File metadata and controls
670 lines (654 loc) · 19 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import {
BootstrapTable,
Options,
SelectRow,
TableHeaderColumn,
} from "react-bootstrap-table";
import { Badge } from "reactstrap";
import React, { ReactElement } from "react";
import { useHistory, useLocation } from "react-router-dom";
import Contest from "../../interfaces/Contest";
import {
constructStatusLabelMap,
noneStatus,
ProblemId,
ProblemStatus,
StatusLabel,
} from "../../interfaces/Status";
import ProblemModel, {
isProblemModelWithDifficultyModel,
isProblemModelWithTimeModel,
} from "../../interfaces/ProblemModel";
import MergedProblem from "../../interfaces/MergedProblem";
import Submission from "../../interfaces/Submission";
import * as Url from "../../utils/Url";
import {
formatPredictedSolveTime,
formatPredictedSolveProbability,
predictSolveTime,
predictSolveProbability,
} from "../../utils/ProblemModelUtil";
import { ColorMode, statusToTableColor } from "../../utils/TableColor";
import { formatMomentDate, parseSecond } from "../../utils/DateUtil";
import { ContestLink } from "../../components/ContestLink";
import { ProblemLink } from "../../components/ProblemLink";
import {
ListPaginationPanel,
ListPaginationPanelProps,
} from "../../components/ListPaginationPanel";
import {
useContestMap,
useMergedProblemMap,
useProblemModelMap,
useRatingInfo,
} from "../../api/APIClient";
import { classifyContest } from "../../utils/ContestClassifier";
import { getLikeContestCategory } from "../../utils/LikeContestUtils";
export const INF_POINT = 1e18;
export interface ProblemRowData {
readonly id: string;
readonly title: string;
readonly contest?: Contest;
readonly contestDate: string;
readonly contestTitle: string;
readonly lastAcceptedDate: string;
readonly solverCount: number;
readonly point: number;
readonly problemModel?: ProblemModel;
readonly firstUserId: string;
readonly executionTime: number;
readonly codeLength: number;
readonly mergedProblem: MergedProblem;
readonly shortestUserId: string;
readonly fastestUserId: string;
readonly status: ProblemStatus;
}
export type ProblemRowDataField =
| keyof ProblemRowData
| "solveProbability"
| "timeEstimation";
export const statusFilters = [
"All",
"Only Trying",
"Only AC",
"AC during Contest",
"AC after Contest",
] as const;
export type StatusFilter = typeof statusFilters[number];
const convertSortByParam = (value: string | null): ProblemRowDataField => {
return (
([
"id",
"title",
"contest",
"contestDate",
"contestTitle",
"lastAcceptedDate",
"solverCount",
"point",
"problemModel",
"firstUserId",
"executionTime",
"codeLength",
"mergedProblem",
"shortestUserId",
"fastestUserId",
"status",
"solveProbability",
"timeEstimation",
] as const).find((v) => v === value) ?? "contestDate"
);
};
interface Props {
userId: string;
fromPoint: number;
toPoint: number;
statusFilterState: StatusFilter;
ratedFilterState:
| "All"
| "Only Rated"
| "Only Unrated"
| "Only Unrated without Difficulty";
contestCategoryFilterState: string;
mergeLikeContest: boolean;
fromDifficulty: number;
toDifficulty: number;
filteredSubmissions: Submission[];
selectRow?: SelectRow;
selectLanguage: string;
}
export const ListTable: React.FC<Props> = (props) => {
const history = useHistory();
const location = useLocation();
const searchParams = new URLSearchParams(location.search);
const sortBy = convertSortByParam(searchParams.get("sortBy"));
const sortOrder = searchParams.get("sortOrder") === "asc" ? "asc" : "desc";
const mergedProblemMap =
useMergedProblemMap().data ?? new Map<ProblemId, MergedProblem>();
const problemModels = useProblemModelMap();
const { userId, filteredSubmissions } = props;
const contestMap = useContestMap();
const statusLabelMap = constructStatusLabelMap(filteredSubmissions, userId);
const rowData = Array.from(mergedProblemMap.values())
.map(
(p: MergedProblem): ProblemRowData => {
const contest = contestMap?.get(p.contest_id);
const contestDate =
contest && contest.start_epoch_second > 0
? formatMomentDate(parseSecond(contest.start_epoch_second))
: "";
const contestTitle = contest ? contest.title : "";
const status = statusLabelMap.get(p.id) ?? noneStatus();
const lastAcceptedDate =
status.label === StatusLabel.Success
? formatMomentDate(parseSecond(status.lastAcceptedEpochSecond))
: "";
const point = p.point ?? INF_POINT;
const firstUserId = p.first_user_id ? p.first_user_id : "";
const executionTime =
p.execution_time != null ? p.execution_time : INF_POINT;
const codeLength = p.source_code_length
? p.source_code_length
: INF_POINT;
const shortestUserId = p.shortest_user_id ? p.shortest_user_id : "";
const fastestUserId = p.fastest_user_id ? p.fastest_user_id : "";
const problemModel = problemModels?.get(p.id);
return {
id: p.id,
title: `${p.problem_index}. ${p.name}`,
contest,
contestDate,
contestTitle,
lastAcceptedDate,
solverCount: p.solver_count ? p.solver_count : 0,
point,
problemModel,
firstUserId,
executionTime,
codeLength,
mergedProblem: p,
shortestUserId,
fastestUserId,
status,
};
}
)
.sort((a, b) => {
const dateOrder = b.contestDate.localeCompare(a.contestDate);
return dateOrder === 0 ? a.title.localeCompare(b.title) : dateOrder;
});
const userRatingInfo = useRatingInfo(props.userId);
const userInternalRating = userRatingInfo.internalRating;
const readDifficultyAsNumber: (row: ProblemRowData) => number = (row) => {
const problemModel = row.problemModel;
if (problemModel === undefined) {
return -1;
}
if (!isProblemModelWithDifficultyModel(problemModel)) {
return -1;
}
return problemModel.difficulty;
};
const predictSolveTimeOfRow: (row: ProblemRowData) => number | null = (
row
) => {
if (userInternalRating === null) {
return null;
}
const problemModel = row.problemModel;
if (problemModel === undefined) {
return null;
}
if (!isProblemModelWithTimeModel(problemModel)) {
return null;
}
return predictSolveTime(problemModel, userInternalRating);
};
const predictSolveProbabilityOfRow: (row: ProblemRowData) => number | null = (
row
) => {
if (userInternalRating === null) {
return null;
}
const problemModel = row.problemModel;
if (problemModel === undefined) {
return null;
}
if (!isProblemModelWithDifficultyModel(problemModel)) {
return null;
}
return predictSolveProbability(problemModel, userInternalRating);
};
const columns: {
header: string;
dataField: ProblemRowDataField;
dataSort?: boolean;
dataAlign?: "center";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
dataFormat?: (cell: any, row: ProblemRowData) => ReactElement | string;
hidden?: boolean;
sortFunc?: (
fieldA: ProblemRowData,
fieldB: ProblemRowData,
order: "asc" | "desc"
) => number;
}[] = [
{
header: "Date",
dataField: "contestDate",
dataSort: true,
},
{
header: "Problem",
dataField: "title",
dataSort: true,
dataFormat: function DataFormat(_, row): React.ReactElement {
return (
<ProblemLink
showDifficulty={true}
isExperimentalDifficulty={row.problemModel?.is_experimental}
problemId={row.mergedProblem.id}
problemName={row.title}
contestId={row.mergedProblem.contest_id}
problemModel={row.problemModel}
userRatingInfo={userRatingInfo}
/>
);
},
},
{
header: "Contest",
dataField: "contest",
dataSort: true,
dataFormat: function DataFormat(
contest: Contest | undefined,
row
): React.ReactElement {
return contest ? (
<ContestLink contest={contest} />
) : (
<a
href={Url.formatContestUrl(row.mergedProblem.contest_id)}
target="_blank"
rel="noopener noreferrer"
>
{row.contestTitle}
</a>
);
},
},
{
header: "Result",
dataField: "status",
dataAlign: "center",
dataFormat: (_: string, row): string | React.ReactElement => {
const { status } = row;
switch (status.label) {
case StatusLabel.Success: {
return <Badge color="success">AC</Badge>;
}
case StatusLabel.Failed: {
return (
<div>
{Array.from(status.solvedRivals).map((rivalId) => (
<Badge key={rivalId} color="danger">
{rivalId}
</Badge>
))}
</div>
);
}
case StatusLabel.Warning: {
return <Badge color="warning">{status.result}</Badge>;
}
case StatusLabel.None: {
return "";
}
}
},
},
{
header: "Last AC Date",
dataField: "lastAcceptedDate",
dataSort: true,
},
{
header: "Solvers",
dataField: "solverCount",
dataSort: true,
dataFormat: function DataFormat(
solverCount: number,
row
): React.ReactElement {
return (
<a
href={Url.formatSolversUrl(
row.mergedProblem.contest_id,
row.mergedProblem.id
)}
target="_blank"
rel="noopener noreferrer"
>
{solverCount}
</a>
);
},
},
{
header: "Point",
dataField: "point",
dataSort: true,
dataFormat: (point: number): React.ReactElement => {
if (point >= INF_POINT) {
return <p>-</p>;
} else {
if (point % 100 === 0) {
return <p>{point}</p>;
} else {
return <p>{point.toFixed(2)}</p>;
}
}
},
},
{
header: "Difficulty",
dataField: "problemModel",
dataSort: true,
sortFunc: (a, b, order): number => {
const delta = readDifficultyAsNumber(a) - readDifficultyAsNumber(b);
const sign = order === "asc" ? 1 : -1;
return delta * sign;
},
dataFormat: (problemModel: ProblemModel): React.ReactElement => {
if (!isProblemModelWithDifficultyModel(problemModel)) {
return <p>-</p>;
} else {
return <p>{problemModel.difficulty}</p>;
}
},
},
{
header: "Solve Prob",
dataField: "solveProbability",
dataSort: true,
sortFunc: (a, b, order): number => {
const aPred = predictSolveProbabilityOfRow(a);
const bPred = predictSolveProbabilityOfRow(b);
const aV = aPred === null ? -1 : aPred;
const bV = bPred === null ? -1 : bPred;
const delta = aV - bV;
const sign = order === "asc" ? 1 : -1;
return delta * sign;
},
dataFormat: function DataFormat(_: string, row): React.ReactElement {
const solveProb = predictSolveProbabilityOfRow(row);
if (solveProb === null) {
return <p>-</p>;
}
return <p>{formatPredictedSolveProbability(solveProb)}</p>;
},
},
{
header: "Time",
dataField: "timeEstimation",
dataSort: true,
sortFunc: (a, b, order): number => {
const aPred = predictSolveTimeOfRow(a);
const bPred = predictSolveTimeOfRow(b);
const aV = aPred === null ? -1 : aPred;
const bV = bPred === null ? -1 : bPred;
const delta = aV - bV;
const sign = order === "asc" ? 1 : -1;
return delta * sign;
},
dataFormat: function DataFormat(_: string, row): React.ReactElement {
const solveTime = predictSolveTimeOfRow(row);
if (solveTime === null) {
return <p>-</p>;
}
return <p>{formatPredictedSolveTime(solveTime)}</p>;
},
},
{
header: "Fastest",
dataField: "executionTime",
dataSort: true,
dataFormat: (executionTime: number, row): React.ReactElement => {
const {
fastest_submission_id,
fastest_contest_id,
fastest_user_id,
} = row.mergedProblem;
if (fastest_submission_id && fastest_contest_id && fastest_user_id) {
return (
<a
href={Url.formatSubmissionUrl(
fastest_submission_id,
fastest_contest_id
)}
target="_blank"
rel="noopener noreferrer"
>
{fastest_user_id} ({executionTime} ms)
</a>
);
} else {
return <p />;
}
},
},
{
header: "Shortest",
dataField: "codeLength",
dataSort: true,
dataFormat: (codeLength: number, row): React.ReactElement => {
const {
shortest_submission_id,
shortest_contest_id,
shortest_user_id,
} = row.mergedProblem;
if (shortest_contest_id && shortest_submission_id && shortest_user_id) {
return (
<a
href={Url.formatSubmissionUrl(
shortest_submission_id,
shortest_contest_id
)}
target="_blank"
rel="noopener noreferrer"
>
{shortest_user_id} ({codeLength} Bytes)
</a>
);
} else {
return <p />;
}
},
},
{
header: "First",
dataField: "firstUserId",
dataSort: true,
dataFormat: (_: string, row): React.ReactElement => {
const {
first_submission_id,
first_contest_id,
first_user_id,
} = row.mergedProblem;
if (first_submission_id && first_contest_id && first_user_id) {
return (
<a
href={Url.formatSubmissionUrl(
first_submission_id,
first_contest_id
)}
target="_blank"
rel="noopener noreferrer"
>
{first_user_id}
</a>
);
} else {
return <p />;
}
},
},
{
header: "Shortest User for Search",
dataField: "shortestUserId",
hidden: true,
},
{
header: "Fastest User for Search",
dataField: "fastestUserId",
hidden: true,
},
{
header: "Contest name for Search",
dataField: "contestTitle",
hidden: true,
},
];
const options: Options<{ [key in ProblemRowDataField]: unknown }> = {
paginationPosition: "top",
sizePerPage: 20,
sizePerPageList: [
{
text: "20",
value: 20,
},
{
text: "50",
value: 50,
},
{
text: "100",
value: 100,
},
{
text: "200",
value: 200,
},
{
text: "All",
value: rowData.length,
},
],
paginationPanel: function DataFormat(
paginationPanelProps: ListPaginationPanelProps
): React.ReactElement {
return <ListPaginationPanel {...paginationPanelProps} />;
},
defaultSortName: sortBy,
defaultSortOrder: sortOrder,
onSortChange: function (
sortName: ProblemRowDataField,
sortOrder: "asc" | "desc"
): void {
const params = new URLSearchParams(location.search);
params.set("sortBy", sortName);
params.set("sortOrder", sortOrder);
history.push({ ...location, search: params.toString() });
},
};
return (
<BootstrapTable
pagination
keyField="id"
height="auto"
hover
striped
search
selectRow={props.selectRow}
tableContainerClass="list-table"
trClassName={(row: ProblemRowData): string => {
const { status, contest } = row;
const selectedLanguages =
props.selectLanguage === "All"
? undefined
: new Set([props.selectLanguage]);
const colorMode = selectedLanguages
? ColorMode.Language
: ColorMode.ContestResult;
return statusToTableColor({
colorMode,
status,
contest,
selectedLanguages,
});
}}
data={rowData
.filter(
(row) => props.fromPoint <= row.point && row.point <= props.toPoint
) // eslint-disable-next-line
.filter((row) => {
const { status, contest } = row;
const isSuccess = status.label === StatusLabel.Success;
const isSubmittedInContest =
status.label === StatusLabel.Success &&
contest !== undefined &&
status.epoch < contest.start_epoch_second + contest.duration_second;
switch (props.statusFilterState) {
case "All":
return true;
case "Only AC":
return isSuccess;
case "Only Trying":
return !isSuccess;
case "AC during Contest":
return isSuccess && isSubmittedInContest;
case "AC after Contest":
return isSuccess && !isSubmittedInContest;
}
}) // eslint-disable-next-line
.filter((row): boolean => {
const isRated = !!row.mergedProblem.point;
const hasDifficulty = isProblemModelWithDifficultyModel(
row.problemModel
);
switch (props.ratedFilterState) {
case "All":
return true;
case "Only Rated":
return isRated;
case "Only Unrated":
return !isRated;
case "Only Unrated without Difficulty":
return !isRated && !hasDifficulty;
}
})
.filter((row): boolean => {
if (props.contestCategoryFilterState === "All") return true;
if (!row.contest) return false;
const contest = contestMap?.get(row.contest.id);
const contestCategory = classifyContest(contest);
return (
props.contestCategoryFilterState === contestCategory ||
(props.mergeLikeContest &&
getLikeContestCategory(props.contestCategoryFilterState) ===
contestCategory)
);
})
.filter((row) => {
const difficulty = isProblemModelWithDifficultyModel(row.problemModel)
? row.problemModel.difficulty
: -1;
return (
props.fromDifficulty <= difficulty &&
difficulty <= props.toDifficulty
);
})}
options={options}
>
{columns.map((c) => (
<TableHeaderColumn
key={c.header}
tdAttr={{ "data-col-name": c.header }}
{...c}
>
{c.header}
</TableHeaderColumn>
))}
</BootstrapTable>
);
};