forked from simrail/map-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStationMarker.tsx
More file actions
117 lines (107 loc) · 3.04 KB
/
Copy pathStationMarker.tsx
File metadata and controls
117 lines (107 loc) · 3.04 KB
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
import { Space, useMantineColorScheme } from "@mantine/core";
import type { Station } from "@simrail/types";
import L from "leaflet";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import { Marker, Popup, Tooltip } from "react-leaflet";
import type { ProfileResponse } from "types/SteamProfile";
import stationsList from "../EDR_station.json";
type StationMarkerProps = {
station: Station;
};
export const StationMarker = ({ station }: StationMarkerProps) => {
const [avatar, setAvatar] = useState<string | null>(null);
const [username, setUsername] = useState<string | null>(null);
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
async function getData() {
if (station.DispatchedBy[0]) {
if (station.DispatchedBy[0].SteamId != "null") {
const avatarRequest = await fetch(
`https://simrail-edr.emeraldnetwork.xyz/steam/${station.DispatchedBy[0].SteamId}`,
);
const profile: ProfileResponse = await avatarRequest.json();
setAvatar(profile.avatar);
setUsername(profile.personaname);
}
if (station.DispatchedBy[0].XboxId != "null") {
setAvatar(null);
setUsername("Unknown [XBOX]");
}
} else {
setAvatar(null);
setUsername("BOT");
}
}
getData();
}, [station.DispatchedBy?.[0]]);
const { colorScheme } = useMantineColorScheme();
let botIcon = "/markers/icon-bot-simrail.jpg";
if (
colorScheme === "dark" ||
(colorScheme === "auto" &&
window.matchMedia("(prefers-color-scheme: dark)").matches)
)
botIcon = "/markers/icon-bot-simrail-dark.jpg";
const icon = L.icon({
iconUrl: station.DispatchedBy[0] && avatar ? avatar : botIcon,
iconSize: [32, 32],
popupAnchor: [0, -16],
className: "station-avatar",
});
if (!username) return null;
return (
<Marker
key={station.id}
icon={icon}
position={[station.Latititude, station.Longitude]}
zIndexOffset={50}
eventHandlers={{
mouseover: (event) => event.target.openPopup(),
mouseout: (event) => event.target.closePopup(),
click: () => {
// Find the corresponding station in the JSON file
const stationEntry = Object.values(stationsList).find(
(entry) => entry.srName === station.Name,
);
if (stationEntry) {
router.push(
`https://edr.simrail.app/${pathname.split("/")[2]}/station/${
stationEntry.id
}`,
);
} else {
// Fallback to old behavior if station not found
router.push(
`https://edr.simrail.app/${
pathname.split("/")[2]
}/station/${station.Prefix.toUpperCase()}`,
);
}
},
}}
>
<Popup>
<img
src={station.MainImageURL}
alt={station.Name}
width={200}
height={86}
style={{ borderRadius: "6px" }}
/>
<br />
<Space h="sm" />
Station: {station.Name}
<br />
User: {username}
<br />
Difficulty: {station.DifficultyLevel}
<br />
</Popup>
<Tooltip offset={[0, 20]} direction={"bottom"} permanent={true}>
{station.Name}
</Tooltip>
</Marker>
);
};