Skip to content

Commit e171640

Browse files
fix: restore route preview overlay
1 parent 5b60510 commit e171640

File tree

4 files changed

+115
-2
lines changed

4 files changed

+115
-2
lines changed

lib/components/map/default-map.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import ElevationPointMarker from './elevation-point-marker'
2222
import EndpointsOverlay from './connected-endpoints-overlay'
2323
import ParkAndRideOverlay from './connected-park-and-ride-overlay'
2424
import PointPopup from './point-popup'
25+
import RoutePreviewOverlay from './route-preview-overlay'
2526
import RouteViewerOverlay from './connected-route-viewer-overlay'
2627
import StopsOverlay from './connected-stops-overlay'
2728
import StopViewerOverlay from './connected-stop-viewer-overlay'
@@ -276,6 +277,7 @@ class DefaultMap extends Component {
276277
zoom={zoom}
277278
>
278279
<PointPopup />
280+
<RoutePreviewOverlay />
279281
{/* The default overlays */}
280282
<EndpointsOverlay />
281283
<RouteViewerOverlay />
@@ -284,8 +286,6 @@ class DefaultMap extends Component {
284286
<TransitiveOverlay
285287
getTransitiveRouteLabel={getTransitiveRouteLabel}
286288
/>
287-
{/* TODO: bring this back? or focus time on migrating transitive to webgl? */}
288-
{/* <RoutePreviewOverlay /> */}
289289
<TripViewerOverlay />
290290
<ElevationPointMarker />
291291

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { connect } from 'react-redux'
2+
import { Itinerary } from '@opentripplanner/types'
3+
import { Layer, Source } from 'react-map-gl'
4+
import polyline from '@mapbox/polyline'
5+
import React from 'react'
6+
7+
import {
8+
getActiveItinerary,
9+
getActiveSearch,
10+
getVisibleItineraryIndex
11+
} from '../../util/state'
12+
13+
type Props = {
14+
geometries: string[]
15+
visible?: boolean
16+
}
17+
/**
18+
* This overlay will display thin gray lines for a set of geometries. It's to be used
19+
* as a stopgap until we make full use of Transitive!
20+
*/
21+
const GeometryPreviewOverlay = ({ geometries, visible }: Props) => {
22+
if (!geometries || !visible) return <></>
23+
24+
const uniqueGeometries = Array.from(new Set(geometries))
25+
try {
26+
const geojson: GeoJSON.FeatureCollection = {
27+
features: uniqueGeometries
28+
.filter((s) => !!s)
29+
.map((segment) => {
30+
const pts = polyline.decode(segment)
31+
return {
32+
geometry: {
33+
coordinates: pts.map((pt: [number, number]) => [pt[1], pt[0]]),
34+
type: 'LineString'
35+
},
36+
properties: [],
37+
type: 'Feature'
38+
}
39+
}),
40+
type: 'FeatureCollection'
41+
}
42+
return (
43+
<>
44+
<Source data={geojson} id="route" type="geojson">
45+
<Layer
46+
id="route-preview"
47+
layout={{
48+
'line-cap': 'round',
49+
'line-join': 'round'
50+
}}
51+
paint={{
52+
'line-blur': 4,
53+
'line-color': '#333',
54+
'line-dasharray': [1, 2],
55+
'line-opacity': 0.6,
56+
'line-width': 4
57+
}}
58+
type="line"
59+
/>
60+
</Source>
61+
</>
62+
)
63+
} catch (error) {
64+
console.warn(`Can't create geojson from route ${geometries}: ${error}`)
65+
return <></>
66+
}
67+
}
68+
69+
// TODO: Typescript state
70+
const mapStateToProps = (state: any) => {
71+
const { activeSearchId, config } = state.otp
72+
// Only show this overlay if the metro UI is explicitly enabled
73+
if (config.itinerary?.showFirstResultByDefault !== false) {
74+
return {}
75+
}
76+
if (!activeSearchId) return {}
77+
78+
const visibleItinerary = getVisibleItineraryIndex(state)
79+
const activeItinerary = getActiveItinerary(state)
80+
81+
// @ts-expect-error state is not typed
82+
const geometries = getActiveSearch(state)?.response.flatMap(
83+
(serverResponse: { plan?: { itineraries?: Itinerary[] } }) =>
84+
serverResponse?.plan?.itineraries?.flatMap((itinerary) => {
85+
return itinerary.legs?.map((leg) => leg.legGeometry.points)
86+
})
87+
)
88+
89+
return {
90+
geometries,
91+
92+
visible:
93+
// We need an explicit check for undefined and null because 0
94+
// is for us true
95+
(visibleItinerary === undefined || visibleItinerary === null) &&
96+
(activeItinerary === undefined || activeItinerary === null)
97+
}
98+
}
99+
100+
const mapDispatchToProps = {}
101+
102+
export default connect(
103+
mapStateToProps,
104+
mapDispatchToProps
105+
)(GeometryPreviewOverlay)

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
"@percy/cli": "^1.14.0",
130130
"@percy/puppeteer": "^2.0.0",
131131
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.1",
132+
"@types/mapbox__polyline": "^1.0.2",
132133
"@types/qs": "^6.9.7",
133134
"@types/react-bootstrap": "^0.32.26",
134135
"@types/react-router": "^5.1.17",

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,6 +3331,13 @@
33313331
resolved "https://registry.yarnpkg.com/@types/mapbox__point-geometry/-/mapbox__point-geometry-0.1.2.tgz#488a9b76e8457d6792ea2504cdd4ecdd9860a27e"
33323332
integrity sha512-D0lgCq+3VWV85ey1MZVkE8ZveyuvW5VAfuahVTQRpXFQTxw03SuIf1/K4UQ87MMIXVKzpFjXFiFMZzLj2kU+iA==
33333333

3334+
"@types/mapbox__polyline@^1.0.2":
3335+
version "1.0.2"
3336+
resolved "https://registry.yarnpkg.com/@types/mapbox__polyline/-/mapbox__polyline-1.0.2.tgz#788cf4d72326961eacf44cb1abca435860ec4673"
3337+
integrity sha512-Kr/oznVL3e8uvWM0+VPlVu2rVP+jKyVW/994HE9YJFBfcAcKqB4wMvqCdgXLG0SSrxxlKc5Nqaj7zt+KRl9oCA==
3338+
dependencies:
3339+
"@types/geojson" "*"
3340+
33343341
"@types/mapbox__vector-tile@^1.3.0":
33353342
version "1.3.0"
33363343
resolved "https://registry.yarnpkg.com/@types/mapbox__vector-tile/-/mapbox__vector-tile-1.3.0.tgz#8fa1379dbaead1e1b639b8d96cfd174404c379d6"

0 commit comments

Comments
 (0)