Skip to content

Commit 2cf9aec

Browse files
authored
Merge pull request #205 from MKuranowski/ensure-alphabetical
Ensure alphabetical order of stations & presets
2 parents 94c428f + 2fe35d2 commit 2cf9aec

3 files changed

Lines changed: 49 additions & 41 deletions

File tree

src/components/ZoneSidebar.tsx

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ import {
4848
trainLineNodeFinder,
4949
} from "@/maps/api";
5050
import {
51+
extractStationLabel,
52+
extractStationName,
5153
geoSpatialVoronoi,
5254
holedMask,
5355
lngLatToText,
@@ -93,6 +95,8 @@ export const ZoneSidebar = () => {
9395
const includeDefaultStations = useStore(includeDefaultStationsAtom);
9496
const $customStations = useStore(customStationsAtom);
9597
const [commandValue, setCommandValue] = useState<string>("");
98+
const [stationSearch, setStationSearch] = useState<string>("");
99+
const isStationSearchActive = stationSearch.trim().length > 0;
96100
const setStations = trainStations.set;
97101
const sidebarRef = useRef<HTMLDivElement>(null);
98102
const [importUrl, setImportUrl] = useState("");
@@ -160,9 +164,7 @@ export const ZoneSidebar = () => {
160164

161165
marker.bindPopup(
162166
`<b>${
163-
geoJsonPoint.properties["name:en"] ||
164-
geoJsonPoint.properties.name ||
165-
"No Name Found"
167+
extractStationName(geoJsonPoint) || "No Name Found"
166168
} (${lngLatToText(
167169
geoJsonPoint.geometry.coordinates as [number, number],
168170
)})</b>`,
@@ -331,11 +333,9 @@ export const ZoneSidebar = () => {
331333

332334
if (nodes.length === 0) {
333335
toast.warning(
334-
`No train line found for ${
335-
nearestTrainStation.properties[
336-
"name:en"
337-
] || nearestTrainStation.properties.name
338-
}`,
336+
`No train line found for ${extractStationName(
337+
nearestTrainStation,
338+
)}`,
339339
);
340340
continue;
341341
} else {
@@ -354,9 +354,7 @@ export const ZoneSidebar = () => {
354354
}
355355
}
356356

357-
const englishName =
358-
nearestTrainStation.properties["name:en"] ||
359-
nearestTrainStation.properties.name;
357+
const englishName = extractStationName(nearestTrainStation);
360358

361359
if (!englishName)
362360
return toast.error("No English name found");
@@ -365,10 +363,7 @@ export const ZoneSidebar = () => {
365363
const letter = englishName[0].toUpperCase();
366364

367365
circles = circles.filter((circle: any) => {
368-
const name =
369-
circle.properties.properties["name:en"] ||
370-
circle.properties.properties.name;
371-
366+
const name = extractStationName(circle.properties);
372367
if (!name) return false;
373368

374369
return question.data.same
@@ -380,10 +375,9 @@ export const ZoneSidebar = () => {
380375
const comparison = question.data.lengthComparison;
381376

382377
circles = circles.filter((circle: any) => {
383-
const name =
384-
circle.properties.properties["name:en"] ||
385-
circle.properties.properties.name;
378+
const name = extractStationName(circle.properties);
386379
if (!name) return false;
380+
387381
if (comparison === "same") {
388382
return name.length === seekerLength;
389383
} else if (comparison === "shorter") {
@@ -942,16 +936,9 @@ export const ZoneSidebar = () => {
942936
x.properties.properties.id ===
943937
commandValue,
944938
);
945-
const displayName =
946-
selected?.properties.properties[
947-
"name:en"
948-
] ||
949-
selected?.properties.properties
950-
.name ||
951-
lngLatToText(
952-
selected?.properties.geometry
953-
.coordinates,
954-
);
939+
const displayName = extractStationLabel(
940+
selected?.properties,
941+
);
955942
const id = selected?.properties
956943
.properties.id as string;
957944
const coords = selected?.properties
@@ -1012,9 +999,18 @@ export const ZoneSidebar = () => {
1012999
</SidebarMenuItem>
10131000
)}
10141001
{$displayHidingZones && (
1015-
<Command>
1002+
<Command
1003+
key={
1004+
isStationSearchActive
1005+
? "station-search-active"
1006+
: "station-search-idle"
1007+
}
1008+
shouldFilter={isStationSearchActive}
1009+
>
10161010
<CommandInput
10171011
placeholder="Search for a hiding zone..."
1012+
value={stationSearch}
1013+
onValueChange={setStationSearch}
10181014
disabled={$isLoading}
10191015
/>
10201016
<CommandList className="max-h-full">
@@ -1108,17 +1104,9 @@ export const ZoneSidebar = () => {
11081104
}}
11091105
disabled={$isLoading}
11101106
>
1111-
{station.properties
1112-
.properties[
1113-
"name:en"
1114-
] ||
1115-
station.properties
1116-
.properties.name ||
1117-
lngLatToText(
1118-
station.properties
1119-
.geometry
1120-
.coordinates,
1121-
)}
1107+
{extractStationLabel(
1108+
station.properties,
1109+
)}
11221110
<button
11231111
onClick={async () => {
11241112
if (!map) return;

src/lib/context.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { persistentAtom } from "@nanostores/persistent";
22
import type { FeatureCollection, MultiPolygon, Polygon } from "geojson";
33
import type { Map } from "leaflet";
4-
import { atom, computed } from "nanostores";
4+
import { atom, computed, onSet } from "nanostores";
55

66
import type {
77
AdditionalMapGeoLocations,
88
CustomStation,
99
OpenStreetMap,
1010
} from "@/maps/api";
11+
import { extractStationLabel } from "@/maps/geo-utils";
1112
import {
1213
type DeepPartial,
1314
type Question,
@@ -106,7 +107,16 @@ export const displayHidingZonesOptions = persistentAtom<string[]>(
106107
},
107108
);
108109
export const questionFinishedMapData = atom<any>(null);
110+
109111
export const trainStations = atom<any[]>([]);
112+
onSet(trainStations, ({ newValue }) => {
113+
newValue.sort((a, b) => {
114+
const aName = (extractStationLabel(a.properties) || "") as string;
115+
const bName = (extractStationLabel(b.properties) || "") as string;
116+
return aName.localeCompare(bName);
117+
});
118+
});
119+
110120
export const useCustomStations = persistentAtom<boolean>(
111121
"useCustomStations",
112122
false,
@@ -197,6 +207,9 @@ export const customPresets = persistentAtom<CustomPreset[]>(
197207
decode: JSON.parse,
198208
},
199209
);
210+
onSet(customPresets, ({ newValue }) => {
211+
newValue.sort((a, b) => a.name.localeCompare(b.name));
212+
});
200213

201214
export const saveCustomPreset = (
202215
preset: Omit<CustomPreset, "id" | "createdAt">,

src/maps/geo-utils/special.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ export const lngLatToText = (coordinates: [number, number]) => {
55
return `${Math.abs(coordinates[1])}°${coordinates[1] > 0 ? "N" : "S"}, ${Math.abs(coordinates[0])}°${coordinates[0] > 0 ? "E" : "W"}`;
66
};
77

8+
export const extractStationName = (stationPoint: any) =>
9+
stationPoint.properties["name:en"] || stationPoint.properties.name;
10+
11+
export const extractStationLabel = (stationPoint: any) =>
12+
extractStationName(stationPoint) ||
13+
lngLatToText(stationPoint.geometry.coordinates);
14+
815
export const groupObjects = (objects: any[]): any[][] => {
916
const filteredObjects = objects.filter(
1017
(obj) =>

0 commit comments

Comments
 (0)