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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 77x 77x 77x 77x 77x 77x 77x 77x 77x 77x 52x 17x 17x 17x 17x 35x 14x 52x 77x 61x 45x 16x 16x 16x 16x 15x 14x 29x 43x 1x 14x 7x 7x 2x 2x 2x 16x 16x 77x 62x 113x 62x 36x 2x 19x 36x 51x 19x 34x 15x 62x 77x 77x 142x 2x 9x 9x 13x 13x 77x 18x 1x 17x 17x 2x 2x 2x 2x 2x 2x 2x 71x 9x 3x 2x 1x 1x 10x | import { Button } from "@wintermuted/ui-react";
import React, { useEffect, useMemo, useRef, useState } from "react";
import { getAllRoutes } from "../../../api/trimet/routeConfig";
import {
RouteDirectionStop,
TrimetRoute,
} from "../../../api/trimet/interfaces/routes";
import useStopHighlight from "../util/useStopHighlight";
import { TopNavBar } from "./common/TopNavBar";
export default function NearbyDestinationPicker({
stops,
destination,
routeId,
open,
onSelect,
onOpenChange,
onStopHighlightChanged,
}: {
stops: RouteDirectionStop[];
destination?: RouteDirectionStop;
routeId: number;
open: boolean;
onSelect: (stop: RouteDirectionStop) => void;
onOpenChange: (open: boolean) => void;
onStopHighlightChanged?: (stopId?: number) => void;
}) {
const [search, setSearch] = useState("");
const [routes, setRoutes] = useState<TrimetRoute[] | null>(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(false);
const [retry, setRetry] = useState(0);
const picker = useRef<HTMLDivElement>(null);
const searchInput = useRef<HTMLInputElement>(null);
const wasOpen = useRef(false);
const { clearHighlight, focusStop, hoverStop } = useStopHighlight(onStopHighlightChanged);
useEffect(() => {
if (open) {
const sidebar = picker.current?.closest(".scrollarea");
Eif (sidebar) {
sidebar.scrollTop = 0;
}
searchInput.current?.focus({ preventScroll: true });
} else if (wasOpen.current) {
picker.current?.querySelector<HTMLButtonElement>("#trip-destination")?.focus();
}
wasOpen.current = open;
}, [open]);
useEffect(() => {
if (!open || routes) {
return;
}
let active = true;
setLoading(true);
setError(false);
getAllRoutes()
.then((data) => {
if (
!data ||
"errorMessage" in data ||
!Array.isArray(data.route) ||
!data.route.some((route) => route.route === routeId) ||
!data.route.every(
(route) =>
Array.isArray(route.dir) &&
route.dir.every(
(direction) =>
direction.stop === undefined || Array.isArray(direction.stop)
)
)
) {
throw new Error("Invalid stop route information");
}
if (active) {
setRoutes(data.route);
setLoading(false);
}
})
.catch(() => {
Eif (active) {
setLoading(false);
setError(true);
}
});
return () => {
active = false;
};
}, [open, routes, retry, routeId]);
const otherRoutes = useMemo(() => {
const byStop = new Map<number, TrimetRoute[]>();
const stopIds = new Set(stops.map((stop) => stop.locid));
routes
?.filter((route) => route.route !== routeId)
.sort((a, b) => a.routeSortOrder - b.routeSortOrder || a.route - b.route)
.forEach((route) => {
const servedStops = new Set(
route.dir.flatMap((direction) =>
(direction.stop ?? []).map((stop) => stop.locid)
)
);
servedStops.forEach((id) => {
if (stopIds.has(id)) {
byStop.set(id, [...(byStop.get(id) || []), route]);
}
});
});
return byStop;
}, [routes, stops, routeId]);
const query = search.trim().toLowerCase();
const visibleStops = stops.filter(
(stop) =>
`${stop.desc} ${stop.locid}`.toLowerCase().includes(query) ||
otherRoutes
.get(stop.locid)
?.some((route) =>
`${route.desc} ${route.route}`.toLowerCase().includes(query)
)
);
function choose(stop: RouteDirectionStop) {
onSelect(stop);
close();
}
function close() {
onOpenChange(false);
clearHighlight();
}
return (
<div ref={picker} className="transit-destination-picker">
<Button
id="trip-destination"
variant="secondary"
className="transit-destination-trigger"
aria-controls="trip-destination-options"
aria-expanded={open}
disabled={!stops.length}
onClick={() => {
if (open) {
close();
} else {
setSearch("");
onOpenChange(true);
}
}}
>
{destination
? `${destination.desc} (${destination.locid})`
: "Choose a destination"}
</Button>
{open && (
<section
id="trip-destination-options"
aria-label="Choose a destination"
className="transit-destination-panel"
onKeyDown={(event) => {
Eif (event.key === "Escape") {
event.preventDefault();
event.stopPropagation();
close();
}
}}
>
<div className="transit-destination-toolbar">
<TopNavBar id="Choose a destination" onBack={close} />
<input
type="search"
className="wm-input"
aria-label="Search stops or routes"
placeholder="Search stops or routes"
ref={searchInput}
value={search}
onChange={(event) => {
setSearch(event.target.value);
clearHighlight();
}}
/>
</div>
{loading && (
<p role="status">Loading other routes serving these stops...</p>
)}
{error && (
<div role="alert">
<p>Could not load other routes. You can still select a stop.</p>
<Button
variant="secondary"
onClick={() => setRetry((value) => value + 1)}
>
Retry routes
</Button>
</div>
)}
<ul className="transit-destination-options">
{visibleStops.map((stop) => (
<li key={`${stop.locid}-${stop.seq}`}>
<button
type="button"
className="transit-destination-option"
data-stop-id={stop.locid}
aria-pressed={destination?.locid === stop.locid}
onClick={() => choose(stop)}
onMouseEnter={() => hoverStop(stop.locid)}
onMouseLeave={() => hoverStop()}
onFocus={() => focusStop(stop.locid)}
onBlur={() => focusStop()}
>
<span className="transit-destination-name">
<strong title={stop.desc}>{stop.desc}</strong>
<small>
Stop {stop.locid}
{destination?.locid === stop.locid ? " - Selected" : ""}
</small>
</span>
{routes && (
<span className="transit-destination-connections">
<small>Other routes: </small>
{otherRoutes.get(stop.locid)?.length ? (
otherRoutes.get(stop.locid).map((route) => (
<span
className="wm-badge"
key={route.route}
title={route.desc}
aria-label={route.desc}
>
{route.route}
</span>
))
) : (
<small>None reported at this stop</small>
)}
</span>
)}
</button>
</li>
))}
</ul>
{!visibleStops.length && (
<p role="status">
No stops match. Try another stop name, number, or route.
</p>
)}
</section>
)}
</div>
);
}
|