@@ -7,6 +7,7 @@ import { timeStr, epochToIso } from '../../../util/timeUtils';
7
7
import { getFaresFromLegs } from '../../../util/fareUtils' ;
8
8
import { ExtendedRouteTypes } from '../../../constants' ;
9
9
import { getItineraryPagePath } from '../../../util/path' ;
10
+ import { locationToUri } from '../../../util/otpStrings' ;
10
11
11
12
const TRANSFER_SLACK = 60000 ;
12
13
const DISPLAY_MESSAGE_THRESHOLD = 120 * 1000 ; // 2 minutes
@@ -279,7 +280,10 @@ export const getTransitLegState = (leg, intl, messages, time) => {
279
280
const departure = leg . trip . stoptimesForDate [ 0 ] ;
280
281
const departed =
281
282
1000 * ( departure . serviceDay + departure . scheduledDeparture ) ;
282
- if ( time - departed < DISPLAY_MESSAGE_THRESHOLD ) {
283
+ if (
284
+ time - departed < DISPLAY_MESSAGE_THRESHOLD &&
285
+ time + DISPLAY_MESSAGE_THRESHOLD > legTime ( leg . start )
286
+ ) {
283
287
// vehicle just departed, maybe no realtime yet
284
288
severity = 'INFO' ;
285
289
} else {
@@ -330,28 +334,60 @@ export const getTransitLegState = (leg, intl, messages, time) => {
330
334
return [ { severity, content, id : legId , expiresOn : legTime ( start ) } ] ;
331
335
} ;
332
336
337
+ export function itinerarySearchPath ( time , leg , nextLeg , position , to ) {
338
+ let from ;
339
+ if ( leg ?. transitLeg ) {
340
+ from = leg . intermediatePlaces . find (
341
+ p => legTime ( p . arrival ) > time + TRANSFER_SLACK ,
342
+ ) ;
343
+ if ( ! from ) {
344
+ from = leg . to ;
345
+ }
346
+ } else {
347
+ from = position || leg ?. to || nextLeg ?. from ;
348
+ }
349
+ const location = { ...from , ...from . stop } ;
350
+
351
+ return getItineraryPagePath ( locationToUri ( location ) , to ) ;
352
+ }
353
+
354
+ function withNewSearchBtn ( children , searchCallback ) {
355
+ return (
356
+ < div className = "navi-alert-content" >
357
+ { children }
358
+ < FormattedMessage id = "navigation-abort-trip" />
359
+ < button
360
+ className = "new-itinerary-search"
361
+ type = "button"
362
+ onClick = { searchCallback }
363
+ >
364
+ < FormattedMessage id = "settings-dropdown-open-label" />
365
+ </ button >
366
+ </ div >
367
+ ) ;
368
+ }
369
+
370
+ function alertId ( alert ) {
371
+ return `${ alert . effectiveStartDate } -${ alert . alertDescriptionText } ` ;
372
+ }
373
+
333
374
export const getItineraryAlerts = (
334
375
legs ,
335
376
time ,
336
377
position ,
337
378
origin ,
338
379
intl ,
339
380
messages ,
340
- location ,
341
- router ,
381
+ itinerarySearchCallback ,
342
382
) => {
343
- const canceled = legs . filter (
344
- leg => leg . realtimeState === 'CANCELED' && legTime ( leg . start ) > time ,
345
- ) ;
346
- let content ;
347
383
const alerts = legs . flatMap ( leg => {
348
384
return leg . alerts
349
385
. filter ( alert => {
350
- const { first } = getFirstLastLegs ( legs ) ;
351
- const startTime = legTime ( first . start ) / 1000 ;
352
- if ( messages . get ( alert . id ) ) {
386
+ if ( messages . get ( alertId ( alert ) ) ?. closed ) {
353
387
return false ;
354
388
}
389
+ const { first } = getFirstLastLegs ( legs ) ;
390
+ const startTime = legTime ( first . start ) / 1000 ;
355
391
// show only alerts that are active when
356
392
// the journey starts
357
393
if ( startTime < alert . effectiveStartDate ) {
@@ -372,47 +408,36 @@ export const getItineraryAlerts = (
372
408
< span className = "header" > { alert . alertHeaderText } </ span >
373
409
</ div >
374
410
) ,
375
- id : ` ${ alert . effectiveStartDate } - ${ alert . alertDescriptionText } ` ,
411
+ id : alertId ( alert ) ,
376
412
} ) ) ;
377
413
} ) ;
378
- const abortTrip = < FormattedMessage id = "navigation-abort-trip" /> ;
379
- const withShowRoutesBtn = children => (
380
- < div className = "alt-btn" >
381
- { children }
382
- < button
383
- className = "show-options"
384
- type = "button"
385
- onClick = { ( ) => router . push ( getItineraryPagePath ( 'POS' , location . to ) ) }
386
- >
387
- < FormattedMessage id = "settings-dropdown-open-label" />
388
- </ button >
389
- </ div >
414
+
415
+ const canceled = legs . filter (
416
+ leg => leg . realtimeState === 'CANCELED' && legTime ( leg . start ) > time ,
390
417
) ;
391
418
392
- if ( canceled ) {
419
+ if ( canceled . length ) {
393
420
// show routes button only for first canceled leg.
394
421
canceled . forEach ( ( leg , i ) => {
395
422
const { legId, mode, route } = leg ;
396
423
397
424
const lMode = getLocalizedMode ( mode , intl ) ;
398
425
const routeName = `${ lMode } ${ route . shortName } ` ;
426
+
399
427
const m = (
400
428
< FormattedMessage
401
429
id = "navigation-mode-canceled"
402
430
values = { { mode : routeName } }
403
431
/>
404
432
) ;
405
433
// we want to show the show routes button only for the first canceled leg.
406
- if ( i === 0 ) {
407
- content = withShowRoutesBtn (
408
- < div className = "navi-alert-content" >
409
- { m }
410
- { abortTrip }
411
- </ div > ,
434
+ const content =
435
+ i === 0 ? (
436
+ withNewSearchBtn ( { m } , itinerarySearchCallback )
437
+ ) : (
438
+ < div className = "navi-alert-content" > { m } </ div >
412
439
) ;
413
- } else {
414
- content = < div className = "navi-alert-content" > { m } </ div > ;
415
- }
440
+
416
441
if ( ! messages . get ( `canceled-${ legId } ` ) ) {
417
442
alerts . push ( {
418
443
severity : 'ALERT' ,
@@ -423,36 +448,33 @@ export const getItineraryAlerts = (
423
448
} ) ;
424
449
}
425
450
} ) ;
426
- }
427
-
428
- const transferProblems = findTransferProblems ( legs , time , position , origin ) ;
429
- if ( transferProblems . length ) {
430
- let prob = transferProblems . find ( p => p . severity === 'ALERT' ) ;
431
- if ( ! prob ) {
432
- // just take first
433
- [ prob ] = transferProblems ;
434
- }
435
- const transferId = `transfer-${ prob . fromLeg . legId } -${ prob . toLeg . legId } }` ;
436
- const alert = messages . get ( transferId ) ;
437
- if ( ! alert || alert . severity !== prob . severity ) {
438
- content = withShowRoutesBtn (
439
- < div className = "navi-alert-content" >
440
- < FormattedMessage
441
- id = "navigation-transfer-problem"
442
- values = { {
443
- route1 : prob . fromLeg . route . shortName ,
444
- route2 : prob . toLeg . route . shortName ,
445
- } }
446
- />
447
- { abortTrip }
448
- </ div > ,
449
- ) ;
450
- alerts . push ( {
451
- severity : prob . severity ,
452
- content,
453
- id : transferId ,
454
- hideClose : prob . severity === 'ALERT' ,
455
- } ) ;
451
+ } else {
452
+ const transferProblems = findTransferProblems ( legs , time , position , origin ) ;
453
+ if ( transferProblems . length ) {
454
+ let prob = transferProblems . find ( p => p . severity === 'ALERT' ) ;
455
+ if ( ! prob ) {
456
+ // just take first
457
+ [ prob ] = transferProblems ;
458
+ }
459
+ const transferId = `transfer-${ prob . fromLeg . legId } -${ prob . toLeg . legId } }` ;
460
+ const alert = messages . get ( transferId ) ;
461
+ if ( ! alert || alert . severity !== prob . severity ) {
462
+ alerts . push ( {
463
+ severity : prob . severity ,
464
+ content : withNewSearchBtn (
465
+ < FormattedMessage
466
+ id = "navigation-transfer-problem"
467
+ values = { {
468
+ route1 : prob . fromLeg . route . shortName ,
469
+ route2 : prob . toLeg . route . shortName ,
470
+ } }
471
+ /> ,
472
+ itinerarySearchCallback ,
473
+ ) ,
474
+ id : transferId ,
475
+ hideClose : prob . severity === 'ALERT' ,
476
+ } ) ;
477
+ }
456
478
}
457
479
}
458
480
return alerts ;
0 commit comments