aboutsummaryrefslogtreecommitdiff
path: root/src/components/GroupedTable.tsx
diff options
context:
space:
mode:
authorAriel Costas Guerrero <94913521+arielcostas@users.noreply.github.com>2025-03-04 00:51:42 +0100
committerAriel Costas Guerrero <94913521+arielcostas@users.noreply.github.com>2025-03-04 00:51:42 +0100
commit79f3c42b0c04c7fd77481c14e6e9c345677b8c42 (patch)
tree6022fdb9d4cad1a77bc98eea88ad29803290f67d /src/components/GroupedTable.tsx
parent0a96e26ade5d3eafe64807fcbd877742d6bcf6da (diff)
Add table layout like iTranvias, remake settings page
Diffstat (limited to 'src/components/GroupedTable.tsx')
-rw-r--r--src/components/GroupedTable.tsx84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/components/GroupedTable.tsx b/src/components/GroupedTable.tsx
new file mode 100644
index 0000000..6581967
--- /dev/null
+++ b/src/components/GroupedTable.tsx
@@ -0,0 +1,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>
+} \ No newline at end of file