Skip to content

Commit 6d41fcd

Browse files
committed
fix: various improvements
1 parent 9b37e2e commit 6d41fcd

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

src/features/route/RouteLayer.jsx

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import { useStorage } from '@store/useStorage'
77

88
import { RouteTile } from './RouteTile'
99
import { routeMarker } from './routeMarker'
10-
import { useRouteStore, ROUTE_COORD_EPSILON } from './useRouteStore'
10+
import {
11+
useRouteStore,
12+
ROUTE_COORD_EPSILON,
13+
getRouteCoordKey,
14+
} from './useRouteStore'
1115

1216
const ACTIVE_Z_INDEX = 1800
1317
const INACTIVE_Z_INDEX = 900
@@ -97,27 +101,42 @@ export function RouteLayer({ routes }) {
97101
},
98102
})
99103

104+
const destinationCoords = React.useMemo(() => {
105+
if (!compactView) return new Set()
106+
const keys = new Set()
107+
activeRoutes.forEach((selection) => {
108+
const route = routeCache[selection.routeId]
109+
if (!route) return
110+
const isForward = selection.orientation === 'forward'
111+
const lat = isForward ? route.end_lat : route.start_lat
112+
const lon = isForward ? route.end_lon : route.start_lon
113+
keys.add(getRouteCoordKey(lat, lon))
114+
})
115+
return keys
116+
}, [activeRoutes, routeCache, compactView])
117+
100118
const anchors = React.useMemo(() => {
101119
if (!compactView) return []
102120
const values = Object.values(poiIndex)
103121
return values.map((entry) => {
104-
const seen = new Set()
105-
let count = 0
122+
const uniqueRoutes = new Set()
106123
values.forEach((candidate) => {
107124
if (
108125
Math.abs(candidate.lat - entry.lat) <= ROUTE_COORD_EPSILON &&
109126
Math.abs(candidate.lon - entry.lon) <= ROUTE_COORD_EPSILON
110127
) {
111128
candidate.routes.forEach((ref) => {
112-
const id = `${ref.routeId}-${ref.orientation}`
113-
if (!seen.has(id) && routeCache[ref.routeId]) {
114-
seen.add(id)
115-
count += 1
129+
if (routeCache[ref.routeId]) {
130+
uniqueRoutes.add(ref.routeId)
116131
}
117132
})
118133
}
119134
})
120-
return { entry, routeCount: count || entry.routes.length || 1 }
135+
return {
136+
entry,
137+
routeCount:
138+
uniqueRoutes.size || new Set(entry.routes.map((r) => r.routeId)).size,
139+
}
121140
})
122141
}, [compactView, poiIndex, routeCache])
123142

@@ -137,15 +156,21 @@ export function RouteLayer({ routes }) {
137156

138157
return (
139158
<>
140-
{anchors.map(({ entry, routeCount }) => (
141-
<RouteAnchor
142-
key={entry.key}
143-
entry={entry}
144-
selected={entry.key === activePoiId}
145-
onSelect={selectPoi}
146-
routeCount={routeCount}
147-
/>
148-
))}
159+
{anchors.map(({ entry, routeCount }) => {
160+
const entryCoordKey = getRouteCoordKey(entry.lat, entry.lon)
161+
if (destinationCoords.has(entryCoordKey) && entry.key !== activePoiId) {
162+
return null
163+
}
164+
return (
165+
<RouteAnchor
166+
key={entry.key}
167+
entry={entry}
168+
selected={entry.key === activePoiId}
169+
onSelect={selectPoi}
170+
routeCount={routeCount}
171+
/>
172+
)
173+
})}
149174
{activeRoutes.map((selection) => (
150175
<ActiveRoute
151176
key={`${selection.routeId}-${selection.orientation}`}

src/features/route/useRouteStore.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@ export const ROUTE_COORD_EPSILON = 1 / 10 ** PRECISION
2929
* @param {number} lon
3030
* @param {'start' | 'end'} prefix
3131
*/
32+
const formatCoordKey = (lat, lon) =>
33+
`${lat.toFixed(PRECISION)}:${lon.toFixed(PRECISION)}`
34+
3235
const fallbackKey = (lat, lon, prefix) =>
33-
`${prefix}:${lat.toFixed(PRECISION)}:${lon.toFixed(PRECISION)}`
36+
`${prefix}:${formatCoordKey(lat, lon)}`
37+
38+
export const getRouteCoordKey = formatCoordKey
39+
40+
export const getRoutePoiKey = (route, position) => {
41+
const lat = position === 'start' ? route.start_lat : route.end_lat
42+
const lon = position === 'start' ? route.start_lon : route.end_lon
43+
const fortId = position === 'start' ? route.start_fort_id : route.end_fort_id
44+
return fortId || fallbackKey(lat, lon, position)
45+
}
3446

3547
/**
3648
* @param {Record<string, RoutePoiIndex>} poiIndex

0 commit comments

Comments
 (0)