Skip to content

Commit 38765e4

Browse files
committed
fix(stop-viewer): fix npe if no stop times exist for pattern
1 parent 4d432b0 commit 38765e4

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

lib/components/viewers/stop-viewer.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,26 @@ class PatternRow extends Component {
285285
timeFormat
286286
} = this.props
287287
// sort stop times by next departure
288-
let sortedStopTimes = null
289-
if (stopTimes) {
288+
let sortedStopTimes = []
289+
const hasStopTimes = stopTimes && stopTimes.length > 0
290+
if (hasStopTimes) {
290291
sortedStopTimes = stopTimes.sort((a, b) => {
291292
const aTime = a.serviceDay + a.realtimeDeparture
292293
const bTime = b.serviceDay + b.realtimeDeparture
293294
return aTime - bTime
294295
})
295296
// Cap the number of times shown for any Route at 5. TODO: make configurable
296297
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-
}
298+
// Do not show any patterns with no departures happening soon or departures
299+
// that have already occurred (there appear to be cases where tomorrow's
300+
// times will show up when they should not).
301+
const secondsUntilFirstTime = sortedStopTimes[0].realtimeDeparture - getHomeTime(homeTimezone)
302+
const firstTimeIsOverThreshold = secondsUntilFirstTime > ONE_HOUR_IN_SECONDS * 3
303+
const firstTimeIsInThePast = secondsUntilFirstTime < 0
304+
if (firstTimeIsInThePast || firstTimeIsOverThreshold) return null
305+
} else {
306+
// Do not include pattern row if it has no stop times.
307+
return null
302308
}
303309
const routeName = route.shortName ? route.shortName : route.longName
304310

@@ -314,7 +320,7 @@ class PatternRow extends Component {
314320
</div>
315321

316322
{/* next departure preview */}
317-
{stopTimes && stopTimes.length > 0 && (
323+
{hasStopTimes && (
318324
<div className='next-trip-preview'>
319325
{getFormattedStopTime(sortedStopTimes[0], homeTimezone, stopViewerArriving, timeFormat)}
320326
</div>
@@ -343,7 +349,7 @@ class PatternRow extends Component {
343349
</div>
344350

345351
{/* list of upcoming trips */}
346-
{stopTimes && (
352+
{hasStopTimes && (
347353
sortedStopTimes.map((stopTime, i) => {
348354
return (
349355
<div

0 commit comments

Comments
 (0)