@@ -13,7 +13,7 @@ import { setMainPanelContent, toggleAutoRefresh } from '../../actions/ui'
1313import { findStop , findStopTimesForStop } from '../../actions/api'
1414import { forgetStop , rememberStop , setLocation } from '../../actions/map'
1515import { routeComparator } from '../../util/itinerary'
16- import { getShowUserSettings } from '../../util/state'
16+ import { getShowUserSettings , getStopViewerConfig } from '../../util/state'
1717import { formatDuration , formatStopTime , getTimeFormat } from '../../util/time'
1818
1919class StopViewer extends Component {
@@ -116,6 +116,7 @@ class StopViewer extends Component {
116116 showUserSettings,
117117 stopData,
118118 stopViewerArriving,
119+ stopViewerConfig,
119120 timeFormat
120121 } = this . props
121122 const { spin } = this . state
@@ -237,6 +238,7 @@ class StopViewer extends Component {
237238 pattern = { patternTimes . pattern }
238239 route = { patternTimes . route }
239240 stopTimes = { patternTimes . times }
241+ stopViewerConfig = { stopViewerConfig }
240242 key = { patternTimes . id }
241243 stopViewerArriving = { stopViewerArriving }
242244 homeTimezone = { homeTimezone }
@@ -282,23 +284,28 @@ class PatternRow extends Component {
282284 stopTimes,
283285 homeTimezone,
284286 stopViewerArriving,
287+ stopViewerConfig,
285288 timeFormat
286289 } = this . props
287290 // sort stop times by next departure
288- let sortedStopTimes = null
289- if ( stopTimes ) {
290- sortedStopTimes = stopTimes . sort ( ( a , b ) => {
291- const aTime = a . serviceDay + a . realtimeDeparture
292- const bTime = b . serviceDay + b . realtimeDeparture
293- return aTime - bTime
294- } )
295- // Cap the number of times shown for any Route at 5. TODO: make configurable
296- if ( sortedStopTimes . length > 0 ) sortedStopTimes = sortedStopTimes . slice ( 0 , 5 )
297- // Do not show any patterns with no departures happening soon.
298- const timeIsOverThreshold = sortedStopTimes [ 0 ] . realtimeDeparture - getHomeTime ( homeTimezone ) > ONE_HOUR_IN_SECONDS * 3
299- if ( sortedStopTimes [ 0 ] && timeIsOverThreshold ) {
300- return null
301- }
291+ let sortedStopTimes = [ ]
292+ const hasStopTimes = stopTimes && stopTimes . length > 0
293+ if ( hasStopTimes ) {
294+ sortedStopTimes = stopTimes
295+ . concat ( )
296+ . sort ( ( a , b ) => {
297+ const aTime = a . serviceDay + a . realtimeDeparture
298+ const bTime = b . serviceDay + b . realtimeDeparture
299+ return aTime - bTime
300+ } )
301+ // We request only x departures per pattern, but the patterns are merged
302+ // according to shared headsigns, so we need to slice the stop times
303+ // here as well to ensure only x times are shown per route/headsign combo.
304+ // This is applied after the sort, so we're keeping the soonest departures.
305+ . slice ( 0 , stopViewerConfig . numberOfDepartures )
306+ } else {
307+ // Do not include pattern row if it has no stop times.
308+ return null
302309 }
303310 const routeName = route . shortName ? route . shortName : route . longName
304311
@@ -314,7 +321,7 @@ class PatternRow extends Component {
314321 </ div >
315322
316323 { /* next departure preview */ }
317- { stopTimes && stopTimes . length > 0 && (
324+ { hasStopTimes && (
318325 < div className = 'next-trip-preview' >
319326 { getFormattedStopTime ( sortedStopTimes [ 0 ] , homeTimezone , stopViewerArriving , timeFormat ) }
320327 </ div >
@@ -343,7 +350,7 @@ class PatternRow extends Component {
343350 </ div >
344351
345352 { /* list of upcoming trips */ }
346- { stopTimes && (
353+ { hasStopTimes && (
347354 sortedStopTimes . map ( ( stopTime , i ) => {
348355 return (
349356 < div
@@ -485,6 +492,7 @@ function getStatusLabel (delay) {
485492
486493const mapStateToProps = ( state , ownProps ) => {
487494 const showUserSettings = getShowUserSettings ( state . otp )
495+ const stopViewerConfig = getStopViewerConfig ( state . otp )
488496 return {
489497 autoRefreshStopTimes : state . otp . user . autoRefreshStopTimes ,
490498 favoriteStops : state . otp . user . favoriteStops ,
@@ -493,6 +501,7 @@ const mapStateToProps = (state, ownProps) => {
493501 showUserSettings,
494502 stopData : state . otp . transitIndex . stops [ state . otp . ui . viewedStop . stopId ] ,
495503 stopViewerArriving : state . otp . config . language . stopViewerArriving ,
504+ stopViewerConfig,
496505 timeFormat : getTimeFormat ( state . otp . config )
497506 }
498507}
0 commit comments