Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | 50x 50x 50x 50x 50x 50x 26x 50x 50x 35x 35x 35x 50x 35x 50x 2x 2x 1x 4x 38x | import { Select, Table } from "@wintermuted/ui-react";
import { map, sortBy } from "lodash";
import React, { useId, useState } from "react";
import FontAwesome from "react-fontawesome";
import { useParams } from "react-router-dom";
import { Arrival, ArrivalData } from "../../../api/trimet/interfaces/arrivals";
import { useTimeDisplay } from "@overworld/ui";
import ArrivalListItem from "./common/ArrivalListItem";
import "./NearbyRoutes.scss";
interface StopArrivalsParams {
data: ArrivalData;
selectedRoutes: number[];
}
function sortArrivalsByEstimated(arrivals: Arrival[]): Arrival[] {
return sortBy(arrivals, arrival => arrival.scheduled);
}
interface ArrivalsTableParams {
data: ArrivalData;
}
export function ArrivalList({ data, selectedRoutes = [] }: ArrivalsTableParams & { selectedRoutes?: number[] }) {
const { id } = useParams();
const filterId = useId();
const [groupBy, setGroupBy] = useState<"none" | "route">("none");
const sortedArrivals = sortArrivalsByEstimated(data.arrival);
const filteredArrivals = selectedRoutes.length
? sortedArrivals.filter(arrival => selectedRoutes.includes(arrival.route))
: sortedArrivals;
const groups = new Map<number, Arrival[]>();
filteredArrivals.forEach(arrival => {
const group = groups.get(arrival.route) || [];
group.push(arrival);
groups.set(arrival.route, group);
});
const arrivalsList = (arrivals: Arrival[]) => arrivals.map(arrival =>
<ArrivalListItem key={arrival.id} id={id} arrival={arrival} />
);
return (
<details id="stop-departures" className="transit-simple-route-direction transit-stop-departures" open>
<summary><FontAwesome name="caret-right" aria-hidden="true" />Departures</summary>
<div className="transit-stop-departures-group-control">
<label htmlFor={filterId}>Group by</label>
<Select id={filterId} value={groupBy} onChange={event => {
Eif (event.target.value === "none" || event.target.value === "route") {
setGroupBy(event.target.value);
}
}}>
<option value="none">None</option>
<option value="route">Route</option>
</Select>
</div>
{filteredArrivals.length ? (
groupBy === "route" ? (
Array.from(groups.entries()).sort(([a], [b]) => a - b).map(([route, arrivals]) => (
<section className="transit-stop-departures-group" key={route} aria-label={`Route ${route}`}>
<h4>Route {route}</h4>
<ul className="transit-stop-departures-list">{arrivalsList(arrivals)}</ul>
</section>
))
) : <ul className="transit-stop-departures-list">{arrivalsList(filteredArrivals)}</ul>
) : (
<p className="transit-route-list-message" role="status">
{selectedRoutes.length ? "No departures for the selected routes." : "No departures available."}
</p>
)}
</details>
);
}
function getArrivalRows(data: ArrivalData, formatTime: (timestamp: number) => string) {
const sortedArrivals = sortArrivalsByEstimated(data.arrival);
return map(sortedArrivals, (arrival: Arrival) => {
const estimatedTime = arrival.estimated
? formatTime(arrival.estimated)
: "No estimation";
const scheduledTime = formatTime(arrival.scheduled);
return (
<tr key={arrival.id}>
<td>
<small>{arrival.shortSign}</small>
</td>
<td>
<small>{estimatedTime}</small>
</td>
<td>
<small>{scheduledTime}</small>
</td>
</tr>
);
});
}
function ArrivalsTable({ data }: ArrivalsTableParams) {
const { formatTime } = useTimeDisplay();
return (
<Table responsive={true}>
<thead>
<tr>
<th>Route</th>
<th>Est. Arrival</th>
<th>Scheduled</th>
</tr>
</thead>
<tbody>{getArrivalRows(data, formatTime)}</tbody>
</Table>
);
}
function StopArrivals({ data, selectedRoutes }: StopArrivalsParams) {
return <ArrivalList data={data} selectedRoutes={selectedRoutes} />;
}
export default StopArrivals;
|