Skip to content

Commit 206f558

Browse files
committed
fix: route crash
1 parent 275f6f4 commit 206f558

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/features/route/useRouteStore.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ const fallbackKey = (lat, lon, prefix) =>
3737

3838
export const getRouteCoordKey = formatCoordKey
3939

40+
/**
41+
* @param {import('@rm/types').Route | undefined} route
42+
*/
43+
const isLoopRoute = (route) => {
44+
if (!route) return false
45+
if (route.start_fort_id && route.start_fort_id === route.end_fort_id) {
46+
return true
47+
}
48+
if (
49+
typeof route.start_lat === 'number' &&
50+
typeof route.start_lon === 'number' &&
51+
typeof route.end_lat === 'number' &&
52+
typeof route.end_lon === 'number'
53+
) {
54+
return (
55+
Math.abs(route.start_lat - route.end_lat) <= ROUTE_COORD_EPSILON &&
56+
Math.abs(route.start_lon - route.end_lon) <= ROUTE_COORD_EPSILON
57+
)
58+
}
59+
return false
60+
}
61+
4062
export const getRoutePoiKey = (route, position) => {
4163
const lat = position === 'start' ? route.start_lat : route.end_lat
4264
const lon = position === 'start' ? route.start_lon : route.end_lon
@@ -53,6 +75,7 @@ export const getRoutePoiKey = (route, position) => {
5375
const collectNearbyRoutes = (poiIndex, entry, routeCache) => {
5476
if (!entry) return []
5577
const seen = new Set()
78+
const loopedRoutes = new Set()
5679
/** @type {RouteSelection[]} */
5780
const combined = []
5881
Object.values(poiIndex).forEach((candidate) => {
@@ -61,8 +84,21 @@ const collectNearbyRoutes = (poiIndex, entry, routeCache) => {
6184
Math.abs(candidate.lon - entry.lon) <= ROUTE_COORD_EPSILON
6285
) {
6386
candidate.routes.forEach((ref) => {
87+
const route = routeCache[ref.routeId]
88+
if (!route) {
89+
return
90+
}
91+
if (isLoopRoute(route)) {
92+
if (ref.orientation !== 'forward') {
93+
return
94+
}
95+
if (loopedRoutes.has(route.id)) {
96+
return
97+
}
98+
loopedRoutes.add(route.id)
99+
}
64100
const id = `${ref.routeId}-${ref.orientation}`
65-
if (!seen.has(id) && routeCache[ref.routeId]) {
101+
if (!seen.has(id)) {
66102
seen.add(id)
67103
combined.push(ref)
68104
}

0 commit comments

Comments
 (0)