Skip to content

Commit 3949283

Browse files
authored
Allow sorting columns RRDP/rsync (#72)
1 parent 1326351 commit 3949283

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/components/connections/RrdpTable.tsx

+23-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext } from 'react';
1+
import React, { useContext, useState } from 'react';
22
import { t, tryFormatNumber } from '../../core/util';
33
import { Rrdp } from '../../types';
44
import Duration from './Duration';
@@ -19,6 +19,19 @@ const RRDP_FIELDS: RrdpKey[] = [
1919

2020
export default function RrdpTable() {
2121
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+
2235
const maxDuration = Object.values(status.rrdp).reduce(
2336
(acc, i) => Math.max(acc, i.duration),
2437
0
@@ -30,14 +43,20 @@ export default function RrdpTable() {
3043
<table>
3144
<thead>
3245
<tr>
33-
<th>URL</th>
46+
<th
47+
onClick={() => setSort(null)}
48+
className={`${sort === null ? 'active' : ''}`}
49+
>URL</th>
3450
{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>
3655
))}
3756
</tr>
3857
</thead>
3958
<tbody>
40-
{Object.entries(status.rrdp).map(([key, rrdp]: [string, Rrdp]) => (
59+
{values.map(([key, rrdp]: [string, Rrdp]) => (
4160
<tr key={key}>
4261
<th role="column" title={key}>
4362
<a href={key} target="_blank" rel="noreferrer">

src/components/connections/RsyncTable.tsx

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useContext } from 'react';
1+
import React, { useContext, useState } from 'react';
22
import { t, tryFormatNumber } from '../../core/util';
33
import { Rsync } from '../../types';
44
import Duration from './Duration';
@@ -10,6 +10,20 @@ const RSYNC_FIELDS: RsyncKey[] = ['duration', 'status'];
1010

1111
export default function RsyncTable() {
1212
const { status } = useContext(StatusContext);
13+
const [sort, setSort] = useState<RsyncKey | null>(null);
14+
let values = Object.entries(status.rsync);
15+
values = values.sort((a, b) => {
16+
if (sort === null) {
17+
return a[0].localeCompare(b[0]);
18+
} else if (typeof a[1][sort] === "number") {
19+
return ((b[1][sort] as number) || 0) - ((a[1][sort] as number) || 0);
20+
} else {
21+
return ("" + a[1][sort]).localeCompare("" + b[1][sort])
22+
}
23+
});
24+
25+
26+
1327
const maxDuration = Object.values(status.rsync).reduce(
1428
(acc, i) => Math.max(acc, i.duration),
1529
0
@@ -21,14 +35,20 @@ export default function RsyncTable() {
2135
<table>
2236
<thead>
2337
<tr>
24-
<th>URL</th>
38+
<th
39+
onClick={() => setSort(null)}
40+
className={`${sort === null ? 'active' : ''}`}
41+
>URL</th>
2542
{RSYNC_FIELDS.map((key) => (
26-
<th key={key}>{t(`connections.${key}`)}</th>
43+
<th key={key}
44+
onClick={() => setSort(key)}
45+
className={`${sort === key ? 'active' : ''}`}
46+
>{t(`connections.${key}`)}</th>
2747
))}
2848
</tr>
2949
</thead>
3050
<tbody>
31-
{Object.entries(status.rsync).map(
51+
{values.map(
3252
([key, rsync]: [string, Rsync]) => (
3353
<tr key={key}>
3454
<th role="column" title={key}>

0 commit comments

Comments
 (0)