-
Notifications
You must be signed in to change notification settings - Fork 17
Show how long trains have been stopped #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,6 +43,8 @@ type MapProps = { | |
| serverId: string | string[]; | ||
| }; | ||
|
|
||
| const getTrainStopKey = (train: Train) => train.id ?? train.TrainNoLocal; | ||
|
|
||
| const LeaftletMap = ({ serverId }: MapProps) => { | ||
| const [map, setMap] = useState<LeafletMap | null>(null); | ||
|
|
||
|
|
@@ -78,12 +80,33 @@ const LeaftletMap = ({ serverId }: MapProps) => { | |
|
|
||
| const { selectedTrain, setSelectedTrain } = useSelectedTrain(); | ||
| const [stations, setStations] = useState<Station[] | null>(null); | ||
| const [stoppedTrainsSince, setStoppedTrainsSince] = useState< | ||
| Record<string, number> | ||
| >({}); | ||
|
|
||
| const getTrains = useCallback(() => { | ||
| fetch(`https://panel.simrail.eu:8084/trains-open?serverCode=${serverId}`) | ||
| .then((res) => res.json()) | ||
| .then((fetchedTrains) => { | ||
| setTrains(fetchedTrains.data); | ||
| const trainsData: Train[] = fetchedTrains.data; | ||
|
|
||
| setStoppedTrainsSince((previousStoppedTrainsSince) => { | ||
| const nextStoppedTrainsSince: Record<string, number> = {}; | ||
|
|
||
| for (const train of trainsData) { | ||
| const trainKey = getTrainStopKey(train); | ||
| const isStopped = Math.round(train.TrainData.Velocity) === 0; | ||
|
|
||
| if (isStopped) { | ||
| nextStoppedTrainsSince[trainKey] = | ||
| previousStoppedTrainsSince[trainKey] ?? Date.now(); | ||
| } | ||
| } | ||
|
|
||
| return nextStoppedTrainsSince; | ||
| }); | ||
|
|
||
| setTrains(trainsData); | ||
|
Comment on lines
+83
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== Map.tsx slice =="
sed -n '1,220p' packages/map/components/Map.tsx
echo
echo "== Search getTrainStopKey =="
rg -n "getTrainStopKey|stoppedTrainsSince|setStoppedTrainsSince|TrainNoLocal|TrainData\.Velocity" packages/map -SRepository: simrail/map-v2 Length of output: 8148 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== Train type definition =="
rg -n "export type Train|type Train|interface Train" -S .
echo
echo "== stoppedSince consumers =="
sed -n '1,140p' packages/map/components/TrainsList.tsx
echo
sed -n '1,220p' packages/map/components/SelectedTrainPopup.tsxRepository: simrail/map-v2 Length of output: 2626 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "== Search for Train id field usage in repo =="
rg -n "\.id\b|TrainNoLocal|ControlledBySteamID|TrainData" packages -S
echo
echo "== Look for local type re-exports or docs around `@simrail/types` =="
rg -n "`@simrail/types`|Train =" packages -SRepository: simrail/map-v2 Length of output: 6209 Preserve stopped timestamps across a missed poll — 🤖 Prompt for AI Agents |
||
| }); | ||
| }, [serverId]); | ||
|
|
||
|
|
@@ -176,7 +199,13 @@ const LeaftletMap = ({ serverId }: MapProps) => { | |
|
|
||
| return ( | ||
| <> | ||
| <SelectedTrainPopup /> | ||
| <SelectedTrainPopup | ||
| stoppedSince={ | ||
| selectedTrain | ||
| ? stoppedTrainsSince[getTrainStopKey(selectedTrain)] | ||
| : undefined | ||
| } | ||
| /> | ||
| <MapContainer | ||
| center={[50.270908, 19.039993]} | ||
| zoom={10} | ||
|
|
@@ -300,7 +329,10 @@ const LeaftletMap = ({ serverId }: MapProps) => { | |
| name="Trains" | ||
| > | ||
| <LayerGroup> | ||
| <TrainsList trains={trains} /> | ||
| <TrainsList | ||
| trains={trains} | ||
| stoppedTrainsSince={stoppedTrainsSince} | ||
| /> | ||
| </LayerGroup> | ||
| </LayersControl.Overlay> | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Duplicate
getTrainStopKeyimplementation across files.The exact same helper is redefined independently in
packages/map/components/TrainsList.tsx(const getTrainStopKey = (train: Train) => train.id ?? train.TrainNoLocal;). SinceTrainsListlooks up entries in thestoppedTrainsSincemap produced here using its own copy of this key function, any future edit to one copy without the other will silently desynchronize the lookup key and break the stopped-duration feature.Extract this into a shared module (e.g.
packages/map/utils/trains.ts) and import it in both files.♻️ Proposed fix
🤖 Prompt for AI Agents