1
- import React , { useContext } from 'react' ;
1
+ import React , { useContext , useState } from 'react' ;
2
2
import { t , tryFormatNumber } from '../../core/util' ;
3
3
import { Rrdp } from '../../types' ;
4
4
import Duration from './Duration' ;
@@ -19,6 +19,19 @@ const RRDP_FIELDS: RrdpKey[] = [
19
19
20
20
export default function RrdpTable ( ) {
21
21
const { status } = useContext ( StatusContext ) ;
22
+ const [ sort , setSort ] = useState < RrdpKey | null > ( null ) ;
23
+ let values = Object . entries ( status . rrdp ) ;
24
+ values = values . sort ( ( a , b ) => {
25
+ if ( sort === null ) {
26
+ return a [ 0 ] . localeCompare ( b [ 0 ] ) ;
27
+ } else if ( typeof a [ 1 ] [ sort ] === "number" ) {
28
+ return ( ( b [ 1 ] [ sort ] as number ) || 0 ) - ( ( a [ 1 ] [ sort ] as number ) || 0 ) ;
29
+ } else {
30
+ return ( "" + a [ 1 ] [ sort ] ) . localeCompare ( "" + b [ 1 ] [ sort ] )
31
+ }
32
+ } ) ;
33
+
34
+
22
35
const maxDuration = Object . values ( status . rrdp ) . reduce (
23
36
( acc , i ) => Math . max ( acc , i . duration ) ,
24
37
0
@@ -30,14 +43,20 @@ export default function RrdpTable() {
30
43
< table >
31
44
< thead >
32
45
< tr >
33
- < th > URL</ th >
46
+ < th
47
+ onClick = { ( ) => setSort ( null ) }
48
+ className = { `${ sort === null ? 'active' : '' } ` }
49
+ > URL</ th >
34
50
{ RRDP_FIELDS . map ( ( key ) => (
35
- < th key = { key } > { t ( `connections.${ key } ` ) } </ th >
51
+ < th key = { key }
52
+ onClick = { ( ) => setSort ( key ) }
53
+ className = { `${ sort === key ? 'active' : '' } ` }
54
+ > { t ( `connections.${ key } ` ) } </ th >
36
55
) ) }
37
56
</ tr >
38
57
</ thead >
39
58
< tbody >
40
- { Object . entries ( status . rrdp ) . map ( ( [ key , rrdp ] : [ string , Rrdp ] ) => (
59
+ { values . map ( ( [ key , rrdp ] : [ string , Rrdp ] ) => (
41
60
< tr key = { key } >
42
61
< th role = "column" title = { key } >
43
62
< a href = { key } target = "_blank" rel = "noreferrer" >
0 commit comments