-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathDisplayJurors.tsx
104 lines (92 loc) · 3.37 KB
/
DisplayJurors.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
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
import React, { useMemo } from "react";
import { useParams, useNavigate } from "react-router-dom";
import styled from "styled-components";
import { isUndefined } from "utils/index";
import { StandardPagination } from "@kleros/ui-components-library";
import { useJurorsByCoherenceScore } from "queries/useJurorsByCoherenceScore";
import { OrderDirection } from "src/graphql/graphql";
import { SkeletonDisputeListItem } from "components/StyledSkeleton";
import JurorCard from "../Home/TopJurors/JurorCard";
import { ListContainer, StyledLabel } from "../Home/TopJurors";
import Header from "../Home/TopJurors/Header";
import { decodeURIFilter } from "utils/uri";
import { useScrollTop } from "hooks/useScrollTop";
interface IDisplayJurors {
totalLeaderboardJurors: number;
}
const StyledPagination = styled(StandardPagination)`
margin-top: 24px;
margin-left: auto;
margin-right: auto;
`;
const DisplayJurors: React.FC<IDisplayJurors> = ({ totalLeaderboardJurors }) => {
const { page, order, filter } = useParams();
const scrollTop = useScrollTop();
const { id: searchValue } = decodeURIFilter(filter ?? "all");
const navigate = useNavigate();
const jurorsPerPage = 10;
const currentPage = parseInt(page ?? "1");
const jurorSkip = jurorsPerPage * (currentPage - 1);
const { data: queryJurors } = useJurorsByCoherenceScore(
jurorSkip,
jurorsPerPage,
"coherenceScore",
order === "asc" ? OrderDirection.Asc : OrderDirection.Desc,
searchValue || ""
);
const jurors = useMemo(() => {
const baseJurors = queryJurors?.users?.map((juror, index) => ({
...juror,
rank: searchValue ? undefined : jurorSkip + index + 1,
}));
if (!searchValue && order === "asc" && baseJurors) {
return baseJurors.map((juror) => ({
...juror,
rank: totalLeaderboardJurors - (juror.rank || 0) + 1,
}));
}
return baseJurors;
}, [queryJurors, jurorSkip, order, totalLeaderboardJurors, searchValue]);
const totalPages = useMemo(
() => (!isUndefined(totalLeaderboardJurors) ? Math.ceil(totalLeaderboardJurors / jurorsPerPage) : 1),
[totalLeaderboardJurors, jurorsPerPage]
);
const handlePageChange = (newPage: number) => {
scrollTop(true);
navigate(`/jurors/${newPage}/${order}/${filter}`);
};
return (
<>
{isUndefined(totalLeaderboardJurors) ? (
<>
<ListContainer>
<Header />
{[...Array(jurorsPerPage)].map((_, i) => (
<SkeletonDisputeListItem key={i} />
))}
</ListContainer>
{!searchValue && (
<StyledPagination currentPage={currentPage} numPages={totalPages} callback={handlePageChange} />
)}
</>
) : (
<ListContainer>
{!isUndefined(jurors) && jurors.length === 0 ? (
<StyledLabel>No jurors found</StyledLabel>
) : (
<>
<Header />
{!isUndefined(jurors)
? jurors.map((juror) => <JurorCard key={juror.id} address={juror.id} {...juror} />)
: [...Array(jurorsPerPage)].map((_, i) => <SkeletonDisputeListItem key={i} />)}
{!searchValue && (
<StyledPagination currentPage={currentPage} numPages={totalPages} callback={handlePageChange} />
)}
</>
)}
</ListContainer>
)}
</>
);
};
export default DisplayJurors;