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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | 640x 56x 584x 584x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 10x 10x 10x 10x 10x 160x 28x 1x 27x 27x 27x 26x 26x 26x 1x 25x 24x 69x 2x 2x 27x 27x 160x 160x 160x 298x 160x 160x 160x 160x 76x 76x 49x 1x 160x 1x 159x 49x 110x 4x 2x 106x 160x 160x 160x 160x 6x 2x 4x 6x 6x 2x 2x 100x 1x | import { Button, FieldGroup } from "@wintermuted/ui-react";
import React, { useCallback, useEffect, useState } from "react";
import { Link, useSearchParams } from "react-router-dom";
import { getRouteByIdAndDirection } from "../../../api/trimet/routeConfig";
import { RouteDirectionStop } from "../../../api/trimet/interfaces/routes";
import { TopNavBar } from "./common/TopNavBar";
import { DirectionsSelection } from "../util/mapbox/directionsSelection";
import NearbyTripTimes from "./NearbyTripTimes";
import NearbyDestinationPicker from "./NearbyDestinationPicker";
import { stopMode, StopMode } from "../../../api/trimet/stopCatalog";
import "./NearbyRoutes.scss";
type RouteState =
| { status: "loading"; key: string }
| { status: "error"; key: string }
| {
status: "ready";
key: string;
name: string;
mode?: StopMode;
directionName: string;
stops: RouteDirectionStop[];
};
function stopId(value: string | null): number | null {
if (!value || !/^\d+$/.test(value)) {
return null;
}
const parsed = Number(value);
return Number.isSafeInteger(parsed) ? parsed : null;
}
export default function NearbyDirections({
onSelectionChanged,
onStopHighlightChanged,
showTripTimes = false,
showTrip = false
}: {
showTripTimes?: boolean;
showTrip?: boolean;
onStopHighlightChanged?: (stopId?: number) => void;
onSelectionChanged: (
selection: DirectionsSelection,
onError: () => void
) => void | (() => void);
}) {
const [searchParams, setSearchParams] = useSearchParams();
const routeId = stopId(searchParams.get("route"));
const direction = stopId(searchParams.get("direction"));
const from = stopId(searchParams.get("from"));
const to = stopId(searchParams.get("to"));
const validRoute = routeId !== null && routeId > 0 &&
(direction === 0 || direction === 1);
const key = `${routeId}-${direction}`;
const [state, setState] = useState<RouteState>({ status: "loading", key });
const [retry, setRetry] = useState(0);
const [mapError, setMapError] = useState(false);
const [mapRetry, setMapRetry] = useState(0);
const [choosingDestination, setChoosingDestination] = useState(false);
const changeDestination = useCallback((stop: RouteDirectionStop) => {
setChoosingDestination(false);
setSearchParams(previous => {
const next = new URLSearchParams(previous);
next.set("to", String(stop.locid));
return next;
}, { replace: true });
}, [setSearchParams]);
useEffect(() => {
if (!validRoute) {
return;
}
let cancelled = false;
setState({ status: "loading", key });
getRouteByIdAndDirection(routeId, direction)
.then(data => {
const route = data?.route?.find(item => item.route === routeId);
const routeDirection = route?.dir?.find(item => item.dir === direction);
if (!routeDirection?.stop?.length) {
throw new Error("No stops available for this route and direction");
}
if (!cancelled) {
setState({
status: "ready",
key,
name: route.desc,
mode: stopMode(route),
directionName: routeDirection.desc,
stops: [...routeDirection.stop].sort((a, b) => a.seq - b.seq)
});
}
})
.catch(() => {
Eif (!cancelled) {
setState({ status: "error", key });
}
});
return () => {
cancelled = true;
};
}, [routeId, direction, validRoute, key, retry]);
const route = state.key === key && state.status === "ready" ? state : null;
const originIndex = route?.stops.findIndex(stop => stop.locid === from) ?? -1;
const destinationIndex = route?.stops.findIndex(
(stop, index) => index > originIndex && stop.locid === to
) ?? -1;
const origin = route?.stops[originIndex];
const destination = origin && route?.stops[destinationIndex];
const showDestinationChoices = Boolean(destination && choosingDestination);
useEffect(() => {
setMapError(false);
if (validRoute && origin) {
return onSelectionChanged(
{
routeId, direction, origin, destination,
...(!showTripTimes && !showTrip ? {
destinationStops: route.stops.slice(originIndex + 1),
...(showDestinationChoices ? { showDestinationChoices: true } : {}),
onChooseDestination: changeDestination
} : {})
},
() => setMapError(true)
);
}
}, [routeId, direction, validRoute, origin, originIndex, destination, onSelectionChanged, mapRetry, route, showTripTimes, showTrip, changeDestination, showDestinationChoices]);
if (!validRoute) {
return (
<div className="transit-directions">
<TopNavBar id="Choose a destination" />
<p role="alert">Choose a route and direction from Routes to get started.</p>
</div>
);
}
if (state.key !== key || state.status === "loading") {
return (
<div className="transit-directions">
<TopNavBar id="Choose a destination" />
<p role="status">Loading route stops...</p>
</div>
);
}
if (state.status === "error") {
return (
<div className="transit-directions">
<TopNavBar id="Choose a destination" />
<p role="alert">Could not load stops for this route and direction.</p>
<Button onClick={() => setRetry(value => value + 1)}>Try again</Button>
</div>
);
}
const remainingStops = origin ? state.stops.slice(originIndex + 1) : [];
const backTo = origin
? `/nearby/simple-routes/${routeId}?stop=${from}&direction=${direction}`
: "/nearby/simple-routes";
const selectionQuery = new URLSearchParams({
route: String(routeId), direction: String(direction), from: String(from), to: String(to)
}).toString();
const selectionUrl = `/nearby/directions?${selectionQuery}`;
if (showTripTimes || showTrip) {
if (!origin || !destination) {
return (
<div className="transit-directions">
<TopNavBar id="Trip times" backTo={selectionUrl} />
<p role="alert">Choose a boarding stop and a downstream destination to view trip times.</p>
<Link className="wm-btn wm-btn-primary" to={selectionUrl}>Choose stops</Link>
</div>
);
}
const tripBackTo = showTrip && searchParams.get("source") === "arrivals"
? `/nearby/directions/arrivals?${selectionQuery}` : selectionUrl;
const departureId = searchParams.get("departure");
if (showTrip && !departureId) {
return (
<div className="transit-directions">
<TopNavBar id="Trip details" backTo={tripBackTo} />
<p role="alert">Choose a departure to view its trip.</p>
<Link className="wm-btn wm-btn-primary" to={tripBackTo}>Choose a trip</Link>
</div>
);
}
return (
<>
{mapError && (
<div>
<p role="alert">Could not load the route line. Trip times are still available.</p>
<Button variant="secondary" onClick={() => setMapRetry(value => value + 1)}>
Retry map
</Button>
</div>
)}
<NearbyTripTimes
routeId={routeId}
direction={direction}
origin={origin}
destination={destination}
routeName={state.name}
routeMode={state.mode}
directionName={state.directionName}
backTo={tripBackTo}
selectedTrip={showTrip && departureId ? {
departureId, tripId: searchParams.get("trip") || undefined
} : undefined}
/>
</>
);
}
return (
<div className={`scrollarea transit-directions${choosingDestination ? " transit-directions-picking" : ""}`}>
<TopNavBar
id={state.name}
subtitle={destination ? `To ${destination.desc}` : state.directionName}
backTo={backTo}
truncateTitle
/>
{mapError && (
<div className="transit-directions-map-error">
<p role="alert">Could not load the route line. Your stop selections are still available.</p>
<Button variant="secondary" onClick={() => setMapRetry(value => value + 1)}>
Retry map
</Button>
</div>
)}
<div className="transit-boarding-stop">
{origin ? (
<dl className="transit-trip-stops">
<div><dt>Board at</dt><dd>{origin.desc} ({from})</dd></div>
</dl>
) : (
<p role="alert">Go back to select a boarding stop on this route and direction.</p>
)}
<Link to={backTo}>Go back to change boarding stop</Link>
</div>
<FieldGroup
className="transit-destination-field"
label="Get off at"
htmlFor="trip-destination"
hint="Stops ahead of your boarding stop, in route order."
>
<NearbyDestinationPicker
stops={remainingStops}
destination={destination}
routeId={routeId}
open={choosingDestination}
onOpenChange={setChoosingDestination}
onStopHighlightChanged={onStopHighlightChanged}
onSelect={changeDestination}
/>
</FieldGroup>
{origin && !remainingStops.length && (
<p role="status">This is the last stop in this direction. Go back to choose an earlier boarding stop.</p>
)}
{searchParams.has("to") && !destination && remainingStops.length > 0 && (
<p role="alert">Choose a destination after your boarding stop.</p>
)}
{destination && (
<>
<p role="status">
{destinationIndex - originIndex}{" "}
{destinationIndex - originIndex === 1 ? "stop" : "stops"} to{" "}
<strong>{destination.desc}</strong>.
</p>
<NearbyTripTimes
inline
routeId={routeId}
direction={direction}
origin={origin}
destination={destination}
routeName={state.name}
routeMode={state.mode}
directionName={state.directionName}
backTo={selectionUrl}
/>
<Link
className="wm-btn wm-btn-primary"
to={`/nearby/directions/arrivals?${selectionQuery}`}
>
View destination arrivals
</Link>
</>
)}
</div>
);
}
|