|
| 1 | +import { isTransit } from '@opentripplanner/core-utils/lib/itinerary' |
| 2 | +import { |
| 3 | + legType, |
| 4 | + timeOptionsType |
| 5 | +} from '@opentripplanner/core-utils/lib/types' |
| 6 | +import { formatTime } from '@opentripplanner/core-utils/lib/time' |
| 7 | +import PropTypes from 'prop-types' |
| 8 | +import React from 'react' |
| 9 | +import styled from 'styled-components' |
| 10 | + |
| 11 | +const TimeText = styled.div`` |
| 12 | + |
| 13 | +const TimeStruck = styled.div` |
| 14 | + text-decoration: line-through; |
| 15 | +` |
| 16 | + |
| 17 | +const TimeBlock = styled.div` |
| 18 | + line-height: 1em; |
| 19 | + margin-bottom: 4px; |
| 20 | +` |
| 21 | + |
| 22 | +const TimeColumnBase = styled.div`` |
| 23 | + |
| 24 | +const StatusText = styled.div` |
| 25 | + color: #bbb; |
| 26 | + font-size: 80%; |
| 27 | + line-height: 1em; |
| 28 | +` |
| 29 | + |
| 30 | +const DelayText = styled.span` |
| 31 | + display: block; |
| 32 | + white-space: nowrap; |
| 33 | +` |
| 34 | + |
| 35 | +// Reusing stop viewer colors. |
| 36 | +const TimeColumnOnTime = styled(TimeColumnBase)` |
| 37 | + ${TimeText}, ${StatusText} { |
| 38 | + color: #5cb85c; |
| 39 | + } |
| 40 | +` |
| 41 | +const TimeColumnEarly = styled(TimeColumnBase)` |
| 42 | + ${TimeText}, ${StatusText} { |
| 43 | + color: #337ab7; |
| 44 | + } |
| 45 | +` |
| 46 | +const TimeColumnLate = styled(TimeColumnBase)` |
| 47 | + ${TimeText}, ${StatusText} { |
| 48 | + color: #d9534f; |
| 49 | + } |
| 50 | +` |
| 51 | + |
| 52 | +/** |
| 53 | + * This component displays the scheduled departure/arrival time for a leg, |
| 54 | + * and, for transit legs, displays any delays or earliness where applicable. |
| 55 | + */ |
| 56 | +export default function RealtimeTimeColumn ({ |
| 57 | + isDestination, |
| 58 | + leg, |
| 59 | + timeOptions |
| 60 | +}) { |
| 61 | + const time = isDestination ? leg.endTime : leg.startTime |
| 62 | + const formattedTime = time && formatTime(time, timeOptions) |
| 63 | + const isTransitLeg = isTransit(leg.mode) |
| 64 | + |
| 65 | + // For non-real-time legs, show only the scheduled time, |
| 66 | + // except for transit legs where we add the "scheduled" text underneath. |
| 67 | + if (!leg.realTime) { |
| 68 | + return ( |
| 69 | + <> |
| 70 | + <TimeText>{formattedTime}</TimeText> |
| 71 | + {isTransitLeg && <StatusText>scheduled</StatusText>} |
| 72 | + </> |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + // Delay in seconds. |
| 77 | + const delay = isDestination ? leg.arrivalDelay : leg.departureDelay |
| 78 | + // Time is in milliseconds. |
| 79 | + const originalTime = time - delay * 1000 |
| 80 | + const originalFormattedTime = |
| 81 | + originalTime && formatTime(originalTime, timeOptions) |
| 82 | + |
| 83 | + // TODO: refine on-time thresholds. |
| 84 | + // const isOnTime = delay >= -60 && delay <= 120; |
| 85 | + const isOnTime = delay === 0 |
| 86 | + |
| 87 | + let statusText |
| 88 | + let TimeColumn = TimeColumnBase |
| 89 | + if (isOnTime) { |
| 90 | + statusText = 'on time' |
| 91 | + TimeColumn = TimeColumnOnTime |
| 92 | + } else if (delay < 0) { |
| 93 | + statusText = 'early' |
| 94 | + TimeColumn = TimeColumnEarly |
| 95 | + } else if (delay > 0) { |
| 96 | + statusText = 'late' |
| 97 | + TimeColumn = TimeColumnLate |
| 98 | + } |
| 99 | + |
| 100 | + // Absolute delay in rounded minutes, for display purposes. |
| 101 | + const delayInMinutes = Math.abs( |
| 102 | + Math.round((isDestination ? leg.arrivalDelay : leg.departureDelay) / 60) |
| 103 | + ) |
| 104 | + |
| 105 | + let renderedTime |
| 106 | + if (!isOnTime) { |
| 107 | + // If the transit vehicle is not on time, strike the original scheduled time |
| 108 | + // and display the updated time underneath. |
| 109 | + renderedTime = ( |
| 110 | + <TimeBlock> |
| 111 | + {!isOnTime && <TimeStruck>{originalFormattedTime}</TimeStruck>} |
| 112 | + <TimeText>{formattedTime}</TimeText> |
| 113 | + </TimeBlock> |
| 114 | + ) |
| 115 | + } else { |
| 116 | + renderedTime = <TimeText>{formattedTime}</TimeText> |
| 117 | + } |
| 118 | + |
| 119 | + return ( |
| 120 | + <TimeColumn> |
| 121 | + {renderedTime} |
| 122 | + <StatusText> |
| 123 | + {/* Keep the '5 min' string on the same line. */} |
| 124 | + {!isOnTime && <DelayText>{delayInMinutes} min</DelayText>} |
| 125 | + {statusText} |
| 126 | + </StatusText> |
| 127 | + </TimeColumn> |
| 128 | + ) |
| 129 | +} |
| 130 | + |
| 131 | +RealtimeTimeColumn.propTypes = { |
| 132 | + isDestination: PropTypes.bool.isRequired, |
| 133 | + leg: legType.isRequired, |
| 134 | + timeOptions: timeOptionsType |
| 135 | +} |
| 136 | + |
| 137 | +RealtimeTimeColumn.defaultProps = { |
| 138 | + timeOptions: null |
| 139 | +} |
0 commit comments