Skip to content

Commit

Permalink
fix: un-de-duplicate headsigns in pattern viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
miles-grant-ibigroup committed Sep 13, 2023
1 parent 3f01238 commit f2c5871
Show file tree
Hide file tree
Showing 3 changed files with 17 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:
RouteDetails:
moreDetails: More Details
operatedBy: Operated by {agencyName}
headsignTo: "{headsign} ({lastStop})"
selectADirection: Select a direction...
stopsTo: Toward
RouteViewer:
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?
// Remove 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 f2c5871

Please sign in to comment.