@@ -4,6 +4,7 @@ import Gravatar from 'react-gravatar';
4
4
import { CheckIcon , SelectorIcon } from '@heroicons/react/solid' ;
5
5
import { useLeaderboardList } from '../../hooks/leaderboard' ;
6
6
import { Listbox , Transition } from '@headlessui/react' ;
7
+ import LeaderboardModel from './LeaderboardModel' ;
7
8
8
9
function classNames ( ...classes : string [ ] ) {
9
10
return classes . filter ( Boolean ) . join ( ' ' ) ;
@@ -42,10 +43,32 @@ interface LeaderboardProps {
42
43
43
44
const Leaderboard : React . FC < LeaderboardProps > = ( { benchmarkId } ) => {
44
45
const [ selected , setSelected ] = useState ( languages [ 0 ] ) ;
46
+ const [ sortedField , setSortedField ] = useState ( 'qualityScore' ) ;
45
47
46
48
const leaderboard = useLeaderboardList ( benchmarkId ) . data ;
49
+ leaderboard ?. sort ( ) ;
47
50
let rankCounter = 1 ;
48
51
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
+
49
72
return (
50
73
< div className = "mt-3 ml-2" >
51
74
< div className = "flex justify-between" >
@@ -62,7 +85,7 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
62
85
< span className = "flex items-center" >
63
86
< img
64
87
src = { selected . avatar }
65
- alt = ""
88
+ alt = "Language logo "
66
89
className = "flex-shrink-0 h-6 w-6 rounded-full"
67
90
/>
68
91
< span className = "ml-3 block truncate" >
@@ -106,7 +129,7 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
106
129
< div className = "flex items-center" >
107
130
< img
108
131
src = { language . avatar }
109
- alt = ""
132
+ alt = "Language logo "
110
133
className = "flex-shrink-0 h-6 w-6 rounded-full"
111
134
/>
112
135
< span
@@ -170,17 +193,34 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
170
193
>
171
194
Date / Language
172
195
</ th >
196
+
173
197
< th
174
198
scope = "col"
175
199
className = "px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
200
+ onClick = { ( ) => setSortedField ( 'qualityScore' ) }
176
201
>
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 >
178
210
</ th >
179
211
< th
180
212
scope = "col"
181
213
className = "px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider"
214
+ onClick = { ( ) => setSortedField ( 'lintScore' ) }
182
215
>
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 >
184
224
</ th >
185
225
</ tr >
186
226
</ thead >
@@ -192,8 +232,9 @@ const Leaderboard: React.FC<LeaderboardProps> = ({ benchmarkId }) => {
192
232
row . language === selected . name ||
193
233
selected . name === 'all' ,
194
234
)
235
+ . sort ( compareFunction )
195
236
. map ( ( row ) => (
196
- < tr key = { row . user ?. email } >
237
+ < tr key = { row . id } >
197
238
< td className = "px-6 py-4 whitespace-nowrap" >
198
239
< div className = "ml-4" >
199
240
< div className = "text-sm font-medium text-gray-900" >
0 commit comments