Skip to content

Commit 59268f6

Browse files
committed
feat: sorting leaderboard by quality and lint scores
1 parent 3cfd43d commit 59268f6

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/components/leaderboard/Leaderboard.tsx

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Gravatar from 'react-gravatar';
44
import { CheckIcon, SelectorIcon } from '@heroicons/react/solid';
55
import { useLeaderboardList } from '../../hooks/leaderboard';
66
import { Listbox, Transition } from '@headlessui/react';
7+
import LeaderboardModel from './LeaderboardModel';
78

89
function classNames(...classes: string[]) {
910
return classes.filter(Boolean).join(' ');
@@ -42,10 +43,32 @@ interface LeaderboardProps {
4243

4344
const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
4445
const [selected, setSelected] = useState(languages[0]);
46+
const [sortedField, setSortedField] = useState('qualityScore');
4547

4648
const leaderboard = useLeaderboardList(benchmarkId).data;
49+
leaderboard?.sort();
4750
let rankCounter = 1;
4851

52+
console.log(sortedField);
53+
54+
function compareFunction(
55+
row1: LeaderboardModel,
56+
row2: LeaderboardModel,
57+
): number {
58+
if (row1 === undefined || row2 === undefined) return 0;
59+
switch (sortedField) {
60+
case 'lintScore':
61+
if (row1.lintScore! > row2.lintScore!) {
62+
return -1;
63+
}
64+
if (row1.lintScore! < row1.lintScore!) {
65+
return 1;
66+
}
67+
return 0;
68+
}
69+
return 0;
70+
}
71+
4972
return (
5073
<div className="mt-3 ml-2">
5174
<div className="flex justify-between">
@@ -62,7 +85,7 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
6285
<span className="flex items-center">
6386
<img
6487
src={selected.avatar}
65-
alt=""
88+
alt="Language logo"
6689
className="flex-shrink-0 h-6 w-6 rounded-full"
6790
/>
6891
<span className="ml-3 block truncate">
@@ -106,7 +129,7 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
106129
<div className="flex items-center">
107130
<img
108131
src={language.avatar}
109-
alt=""
132+
alt="Language logo"
110133
className="flex-shrink-0 h-6 w-6 rounded-full"
111134
/>
112135
<span
@@ -170,17 +193,34 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
170193
>
171194
Date / Language
172195
</th>
196+
173197
<th
174198
scope="col"
175199
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
200+
onClick={() => setSortedField('qualityScore')}
176201
>
177-
Quality Score
202+
<div className="flex">
203+
Quality Score
204+
<img
205+
className="w-4 h-4 ml-2"
206+
alt="Sort by quality score"
207+
src="https://image.flaticon.com/icons/png/512/162/162735.png"
208+
/>
209+
</div>
178210
</th>
179211
<th
180212
scope="col"
181213
className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
214+
onClick={() => setSortedField('lintScore')}
182215
>
183-
Lint Score
216+
<div className="flex">
217+
Lint Score
218+
<img
219+
className="w-4 h-4 ml-2"
220+
alt="Sort by lint score"
221+
src="https://image.flaticon.com/icons/png/512/162/162735.png"
222+
/>
223+
</div>
184224
</th>
185225
</tr>
186226
</thead>
@@ -192,8 +232,9 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
192232
row.language === selected.name ||
193233
selected.name === 'all',
194234
)
235+
.sort(compareFunction)
195236
.map((row) => (
196-
<tr key={row.user?.email}>
237+
<tr key={row.id}>
197238
<td className="px-6 py-4 whitespace-nowrap">
198239
<div className="ml-4">
199240
<div className="text-sm font-medium text-gray-900">

src/hooks/leaderboard.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export function useLeaderboardList(benchmarkId: string) {
1010
const { data } = await authenticatedRequest({
1111
url: `benchmarks/${benchmarkId}/leaderboard`,
1212
});
13-
console.log(data);
1413
return data;
1514
}
1615
},

0 commit comments

Comments
 (0)