blob: 6581967ac308fed4fb0f7c88aac625f20a5e83bc (
plain)
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
|
import { StopDetails } from "../pages/Estimates";
import LineIcon from "./LineIcon";
interface GroupedTable {
data: StopDetails;
dataDate: Date | null;
}
export const GroupedTable: React.FC<GroupedTable> = ({ data, dataDate }) => {
const absoluteArrivalTime = (minutes: number) => {
const now = new Date()
const arrival = new Date(now.getTime() + minutes * 60000)
return Intl.DateTimeFormat(navigator.language, {
hour: '2-digit',
minute: '2-digit'
}).format(arrival)
}
const formatDistance = (meters: number) => {
if (meters > 1024) {
return `${(meters / 1000).toFixed(1)} km`;
} else {
return `${meters} m`;
}
}
const groupedEstimates = data.estimates.reduce((acc, estimate) => {
if (!acc[estimate.line]) {
acc[estimate.line] = [];
}
acc[estimate.line].push(estimate);
return acc;
}, {} as Record<string, typeof data.estimates>);
const sortedLines = Object.keys(groupedEstimates).sort((a, b) => {
const firstArrivalA = groupedEstimates[a][0].minutes;
const firstArrivalB = groupedEstimates[b][0].minutes;
return firstArrivalA - firstArrivalB;
});
return <table className="table">
<caption>Estimaciones de llegadas a las {dataDate?.toLocaleTimeString()}</caption>
<thead>
<tr>
<th>Línea</th>
<th>Ruta</th>
<th>Llegada</th>
<th>Distancia</th>
</tr>
</thead>
<tbody>
{sortedLines.map((line) => (
groupedEstimates[line].map((estimate, idx) => (
<tr key={`${line}-${idx}`}>
{idx === 0 && (
<td rowSpan={groupedEstimates[line].length} style={{ verticalAlign: 'top' }}>
<LineIcon line={line} />
</td>
)}
<td>{estimate.route}</td>
<td>{`${estimate.minutes} min`}</td>
<td>
{estimate.meters > -1
? formatDistance(estimate.meters)
: "No disponible"
}
</td>
</tr>
))
))}
</tbody>
{data?.estimates.length === 0 && (
<tfoot>
<tr>
<td colSpan={4}>No hay estimaciones disponibles</td>
</tr>
</tfoot>
)}
</table>
}
|