aboutsummaryrefslogtreecommitdiff
path: root/src/frontend/app/components/RegularTable.tsx
blob: e5b3782440e63d3d9f3e74fd07847ca11995c57e (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
import { useTranslation } from "react-i18next";
import { type StopDetails } from "../routes/estimates-$id";
import LineIcon from "./LineIcon";

interface RegularTableProps {
    data: StopDetails;
    dataDate: Date | null;
}

export const RegularTable: React.FC<RegularTableProps> = ({ data, dataDate }) => {
    const { t } = useTranslation();

    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} ${t('estimates.meters', 'm')}`;
        }
    }

    return <table className="table">
        <caption>{t('estimates.caption', 'Estimaciones de llegadas a las {{time}}', { time: dataDate?.toLocaleTimeString() })}</caption>

        <thead>
            <tr>
                <th>{t('estimates.line', 'Línea')}</th>
                <th>{t('estimates.route', 'Ruta')}</th>
                <th>{t('estimates.arrival', 'Llegada')}</th>
                <th>{t('estimates.distance', 'Distancia')}</th>
            </tr>
        </thead>

        <tbody>
            {data.estimates
                .sort((a, b) => a.minutes - b.minutes)
                .map((estimate, idx) => (
                    <tr key={idx}>
                        <td><LineIcon line={estimate.line} /></td>
                        <td>{estimate.route}</td>
                        <td>
                            {estimate.minutes > 15
                                ? absoluteArrivalTime(estimate.minutes)
                                : `${estimate.minutes} ${t('estimates.minutes', 'min')}`}
                        </td>
                        <td>
                            {estimate.meters > -1
                                ? formatDistance(estimate.meters)
                                : t('estimates.not_available', 'No disponible')
                            }
                        </td>
                    </tr>
                ))}
        </tbody>

        {data?.estimates.length === 0 && (
            <tfoot>
                <tr>
                    <td colSpan={4}>{t('estimates.none', 'No hay estimaciones disponibles')}</td>
                </tr>
            </tfoot>
        )}
    </table>
}