From f2c5871636d66005d24f6d5e03e5dd5650a1ec3d Mon Sep 17 00:00:00 2001 From: miles-grant-ibigroup Date: Wed, 13 Sep 2023 15:07:18 -0400 Subject: [PATCH] fix: un-de-duplicate headsigns in pattern viewer --- i18n/en-US.yml | 1 + lib/components/viewers/route-details.tsx | 29 +++++++++++++----------- lib/components/viewers/styled.ts | 1 - 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/i18n/en-US.yml b/i18n/en-US.yml index 9a1a8c9a7..9a4dab4e6 100644 --- a/i18n/en-US.yml +++ b/i18n/en-US.yml @@ -472,6 +472,7 @@ components: RouteDetails: moreDetails: More Details operatedBy: Operated by {agencyName} + headsignTo: "{headsign} ({lastStop})" selectADirection: Select a direction... stopsTo: Toward RouteViewer: diff --git a/lib/components/viewers/route-details.tsx b/lib/components/viewers/route-details.tsx index 579e376cc..48e6c6bc2 100644 --- a/lib/components/viewers/route-details.tsx +++ b/lib/components/viewers/route-details.tsx @@ -43,6 +43,7 @@ interface PatternSummary { geometryLength: number headsign: string id: string + lastStop?: string } interface Props { @@ -85,27 +86,29 @@ class RouteDetails extends Component { ([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) => { diff --git a/lib/components/viewers/styled.ts b/lib/components/viewers/styled.ts index 16bfbe173..278dbbd12 100644 --- a/lib/components/viewers/styled.ts +++ b/lib/components/viewers/styled.ts @@ -52,7 +52,6 @@ export const PatternContainer = styled.div` align-items: center; display: flex; justify-content: space-between; - max-width: 300px; width: 95%; span {