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 | 26x 26x 26x 3x 7x 7x 7x 26x 237x 220x 222x 117x 220x 421x 421x 421x 421x 1610x 230x 230x 230x 230x 230x 30x 30x 230x 693x 666x 5x 230x 251x 230x 27x 19x 230x 230x 230x 251x 240x 240x 230x 9x 2070x 445x 445x 445x 421x 44x 289x 289x 156x 2x 486x 449x 31x 31x | import { faBus, faCableCar, faTrain, faTrainTram } from "@fortawesome/free-solid-svg-icons";
import { Badge } from "@wintermuted/ui-react";
import { MultiSelect } from "@wintermuted/ui-react/select";
import React, { useMemo, useState } from "react";
import { searchStopCatalog, type StopCatalogStop, type StopMode } from "@overworld/trimet-client";
import "./PlannerStopSelect.scss";
export interface SavedPlannerStop {
id: number;
label: string;
}
const GROUPS = ["N", "NE", "SE", "S", "SW", "NW", "Other stops"];
const MAX_PER_GROUP = 20;
const MODE_ICONS = {
Bus: faBus,
Rail: faTrain,
Streetcar: faTrainTram,
"Aerial tram": faCableCar,
};
function StopModes({ modes }: { modes: StopMode[] }) {
return (
<span className="transit-planner-stop-modes">
{modes.map(mode => {
const { icon: [width, height, , , paths] } = MODE_ICONS[mode];
return (
<span key={mode} title={mode}>
<svg viewBox={`0 0 ${width} ${height}`} aria-hidden="true" focusable="false">
{[paths].flat().map(path => <path key={path} d={path} />)}
</svg>
<span className="wm-sr-only">{mode} </span>
</span>
);
})}
</span>
);
}
export interface CommonPlannerPlace {
id: number;
label: string;
aliases: readonly string[];
}
const EMPTY_COMMON_PLACES: readonly CommonPlannerPlace[] = [];
function normalizePlace(value: string) {
return value.trim().toLowerCase().replace(/\s+/g, " ");
}
function stopGroup(stop: StopCatalogStop) {
const prefixes = new Set(
[stop.desc, ...stop.aliases].flatMap(name =>
Array.from(name.matchAll(/(?:^\s*|(?:&|\/|\bat\b)\s*)(NE|SE|SW|NW|N|S)\s+(?=[\w])/gi),
match => match[1].toUpperCase())
)
);
return prefixes.size === 1 ? Array.from(prefixes)[0] : "Other stops";
}
export interface PlannerStopSelectProps {
id: string;
stops: StopCatalogStop[];
selected?: StopCatalogStop;
onChange: (stop: StopCatalogStop | null) => void;
loading: boolean;
savedStops?: SavedPlannerStop[];
commonPlaces?: readonly CommonPlannerPlace[];
}
export default function PlannerStopSelect({ id, stops, selected, onChange, loading, savedStops,
commonPlaces = EMPTY_COMMON_PLACES }: PlannerStopSelectProps) {
const [query, setQuery] = useState("");
const commonPlacesById = useMemo(() => new Map(commonPlaces.map(place => [place.id, place])), [commonPlaces]);
const quadrants = useMemo(() => new Map(stops.map(stop => [stop.locid, stopGroup(stop)])), [stops]);
const { options, savedLabels } = useMemo(() => {
const groups = new Map(GROUPS.map(label => [label, { label, options: [] as StopCatalogStop[] }]));
const trimmed = query.trim();
const matching = trimmed ? searchStopCatalog(stops, trimmed, { includeRouteNames: true }) : stops;
const placeQuery = normalizePlace(query);
const savedLabels = new Map<number, string>();
for (const { id, label } of savedStops ?? []) {
const previous = savedLabels.get(id);
savedLabels.set(id, previous ? `${previous} / ${label}` : label);
}
const commonStops = /^\d+$/.test(trimmed) ? [] : Array.from(commonPlacesById).flatMap(([id, place]) => {
const stop = stops.find(item => item.locid === id);
return stop && (!placeQuery || [place.label, ...place.aliases, stop.desc, ...stop.aliases]
.some(name => normalizePlace(name).includes(placeQuery))) ? [stop] : [];
});
const commonIds = new Set(commonStops.map(stop => stop.locid));
const matchingIds = new Set(matching.map(stop => stop.locid));
const savedOptions = Array.from(savedLabels).flatMap(([id, label]) => {
const stop = stops.find(item => item.locid === id);
return stop && (matchingIds.has(id) || commonIds.has(id) || normalizePlace(label).includes(placeQuery))
? [stop] : [];
});
const savedIds = new Set(savedOptions.map(stop => stop.locid));
const exact = /^\d+$/.test(trimmed) ? matching.find(stop => String(stop.locid) === trimmed) : undefined;
for (const stop of exact ? [exact, ...matching.filter(item => item !== exact)] : matching) {
if (savedIds.has(stop.locid) || commonIds.has(stop.locid)) continue;
const group = groups.get(quadrants.get(stop.locid));
if (group.options.length < MAX_PER_GROUP) group.options.push(stop);
}
return {
options: [
{ label: "Saved stops", options: savedOptions },
{ label: "Common places", options: commonStops.filter(stop => !savedIds.has(stop.locid)) },
...Array.from(groups.values())
].filter(group => group.options.length > 0),
savedLabels
};
}, [stops, quadrants, query, savedStops, commonPlacesById]);
function stopLabel(stop: StopCatalogStop) {
const label = savedLabels.get(stop.locid);
const name = commonPlacesById.get(stop.locid)?.label ?? stop.desc;
return `${label ? `${label}: ` : ""}${name} (${stop.locid})`;
}
return (
<MultiSelect<StopCatalogStop, false>
inputId={id}
className="transit-planner-stop-select"
isMulti={false}
isClearable
isLoading={loading}
isDisabled={stops.length === 0}
value={selected ?? null}
options={options}
inputValue={query}
onInputChange={value => setQuery(value)}
onChange={onChange}
getOptionLabel={stop => {
const label = stopLabel(stop);
return stop.modes?.length ? `${label}, ${stop.modes.join(", ")}` : label;
}}
formatOptionLabel={(stop, { context }) => (
<>
{stop.modes?.length ? <StopModes modes={stop.modes} /> : null}
{stopLabel(stop)}
{context === "menu" && stop.routes?.length ? (
<span className="transit-planner-stop-routes">
<span className="wm-sr-only">Routes: </span>
{stop.routes.map(({ id: routeId, label, color }) => (
<Badge
key={routeId}
className="transit-planner-stop-route"
style={color ? {
borderColor: color,
backgroundColor: `color-mix(in srgb, ${color} 12%, var(--wm-color-surface-solid))`,
} : undefined}
>
{label}
</Badge>
))}
</span>
) : null}
</>
)}
getOptionValue={stop => String(stop.locid)}
filterOption={null}
placeholder="Search stops or common places"
noOptionsMessage={() => "No matches. Try a stop ID, stop name, route name, or common place."}
menuPortalTarget={document.body}
menuPosition="fixed"
menuPlacement="auto"
maxMenuHeight={400}
styles={{
control: base => ({ ...base, minHeight: "var(--wm-control-height)" }),
menu: base => ({ ...base, fontSize: "0.875rem" }),
menuPortal: (base, { rect }) => ({
...base,
width: `max(${rect.width}px, min(30rem, calc(100% - ${rect.left}px - var(--wm-space-3))))`,
zIndex: 5
})
}}
/>
);
}
|