Skip to content

Commit

Permalink
Merge pull request #997 from opentripplanner/disable-headsign-dedupli…
Browse files Browse the repository at this point in the history
…cation

Un-de-duplicate headsigns in pattern viewer
  • Loading branch information
miles-grant-ibigroup authored Sep 25, 2023
2 parents cfec6bb + 26528e0 commit 6aa90fb
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions i18n/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ components:
tripsFound: We Found {count, plural, one {# Option} other {# Options}}
waiting: Waiting...
RouteDetails:
headsignTo: "{headsign} ({lastStop})"
moreDetails: More Details
operatedBy: Operated by {agencyName}
selectADirection: Select a direction...
Expand Down
1 change: 1 addition & 0 deletions i18n/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ components:
tripsFound: "{count, plural, one {# trajet trouvé} other {# trajets trouvés}}"
waiting: Patientez...
RouteDetails:
headsignTo: "{headsign} ({lastStop})"
moreDetails: Plus d'infos
operatedBy: Exploité par {agencyName}
selectADirection: Choisissez une direction...
Expand Down
29 changes: 16 additions & 13 deletions lib/components/viewers/route-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ interface PatternSummary {
geometryLength: number
headsign: string
id: string
lastStop?: string
}

interface Props {
Expand Down Expand Up @@ -85,27 +86,29 @@ class RouteDetails extends Component<Props> {
([id, pat]): PatternSummary => ({
geometryLength: pat.patternGeometry?.length || 0,
headsign: extractHeadsignFromPattern(pat, shortName),
id
id,
lastStop: pat.stops?.[pat.stops?.length - 1]?.name
})
)
// Remove duplicate headsigns. Using a reducer means that the first pattern
// with a specific headsign is the accepted one. TODO: is this good behavior?
// Address duplicate headsigns. Replaces duplicate headsigns with the last stop name
.reduce((prev: PatternSummary[], cur) => {
const amended = prev
const alreadyExistingIndex = prev.findIndex(
(h) => h.headsign === cur.headsign
)
// If the item we're replacing has less geometry, replace it!
if (alreadyExistingIndex >= 0) {
// Only replace if new pattern has greater geometry
if (
amended[alreadyExistingIndex].geometryLength < cur.geometryLength
) {
amended[alreadyExistingIndex] = cur
}
} else {
amended.push(cur)
// If the item we're replacing has less geometry, amend the headsign to be more helpful
if (
alreadyExistingIndex >= 0 &&
cur.lastStop &&
cur.headsign !== cur.lastStop
) {
cur.headsign = intl.formatMessage(
{ id: 'components.RouteDetails.headsignTo' },
{ ...cur }
)
}

amended.push(cur)
return amended
}, [])
.sort((a, b) => {
Expand Down
1 change: 0 additions & 1 deletion lib/components/viewers/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export const PatternContainer = styled.div`
align-items: center;
display: flex;
justify-content: space-between;
max-width: 300px;
width: 95%;
span {
Expand Down

0 comments on commit 6aa90fb

Please sign in to comment.