Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern viewer configuration and styling #1336

Merged
merged 10 commits into from
Jan 23, 2025
2 changes: 2 additions & 0 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@ disableSingleItineraryDays: false
# # Whether to render routes within flex zones of a route's patterns. If set to true,
# # routes will not be rendered within flex zones.
# hideRouteShapesWithinFlexZones: true
# # Setting to sort routes by the number of vehicles on each pattern
# sortRoutePatternsByVehicleCount: true
# # Disable vehicle highlight if necessary (e.g. custom or inverted icons)
# vehicleIconHighlight: true
# # Customize vehicle icon padding (the default iconPadding is 2px in otp-ui)
Expand Down
64 changes: 46 additions & 18 deletions lib/components/viewers/route-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ const PatternSelectDropdown = styled(Dropdown)`
span.caret {
color: #333;
}

ul li button span {
width: 100%;
display: block;
}
`

interface Props {
Expand All @@ -57,6 +62,7 @@ interface Props {
setHoveredStop: (id: string | null) => void
setViewedRoute: SetViewedRouteHandler
setViewedStop: SetViewedStopHandler
sortPatternsByVehicleCount: boolean
}

class RouteDetails extends Component<Props> {
Expand Down Expand Up @@ -87,33 +93,44 @@ class RouteDetails extends Component<Props> {
}

render() {
const { intl, operator, patternId, route, setHoveredStop } = this.props
const {
intl,
operator,
patternId,
route,
setHoveredStop,
sortPatternsByVehicleCount
} = this.props
const { agency, patterns = {}, shortName, url } = route
const pattern = patterns[patternId]

const moreDetailsURL = url || route?.agency?.url

const routeColor = getRouteColorBasedOnSettings(operator, route)

const headsigns = extractMainHeadsigns(
const unsortedHeadsigns = extractMainHeadsigns(
patterns,
shortName,
this._editHeadsign
).sort((a, b) => {
// sort by number of vehicles on that pattern
const aVehicleCount =
route.vehicles?.filter((vehicle) => vehicle.patternId === a.id)
.length || 0
const bVehicleCount =
route.vehicles?.filter((vehicle) => vehicle.patternId === b.id)
.length || 0

// if both have the same count, sort by pattern geometry length
if (aVehicleCount === bVehicleCount) {
return b.geometryLength - a.geometryLength
}
return bVehicleCount - aVehicleCount
})
)

const headsigns = sortPatternsByVehicleCount
? unsortedHeadsigns.sort((a, b) => {
// sort by number of vehicles on that pattern
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid adding the new variable and the ternary if you instead add an if(!sortPatternsByVehicleCount) return 0 at the top of the original sort method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it more efficient to avoid iterating through the sort if we don't have to?

Copy link
Collaborator

@miles-grant-ibigroup miles-grant-ibigroup Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we always return 0 it's still O(n) and the code is much cleaner

const aVehicleCount =
route.vehicles?.filter((vehicle) => vehicle.patternId === a.id)
.length || 0
const bVehicleCount =
route.vehicles?.filter((vehicle) => vehicle.patternId === b.id)
.length || 0

// if both have the same count, sort by pattern geometry length
if (aVehicleCount === bVehicleCount) {
return b.geometryLength - a.geometryLength
}
return bVehicleCount - aVehicleCount
})
: unsortedHeadsigns

const patternSelectLabel = intl.formatMessage({
id: 'components.RouteDetails.selectADirection'
Expand Down Expand Up @@ -231,11 +248,22 @@ class RouteDetails extends Component<Props> {
}
}

const mapStateToProps = (state: any) => {
amy-corson-ibigroup marked this conversation as resolved.
Show resolved Hide resolved
const { sortRoutePatternsByVehicleCount } = state.otp.config.routeViewer
amy-corson-ibigroup marked this conversation as resolved.
Show resolved Hide resolved

return {
sortPatternsByVehicleCount: sortRoutePatternsByVehicleCount !== false
}
}

// connect to redux store
const mapDispatchToProps = {
setHoveredStop: uiActions.setHoveredStop,
setViewedRoute: uiActions.setViewedRoute,
setViewedStop: uiActions.setViewedStop
}

export default connect(null, mapDispatchToProps)(injectIntl(RouteDetails))
export default connect(
mapStateToProps,
mapDispatchToProps
)(injectIntl(RouteDetails))
5 changes: 2 additions & 3 deletions lib/components/viewers/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ export const PatternContainer = styled.div`
background-color: inherit;
color: inherit;
display: flex;
gap: 16px;
justify-content: flex-start;
justify-content: space-between;
margin: 0;
padding: 8px;

Expand All @@ -48,7 +47,7 @@ export const PatternContainer = styled.div`
// Styling for SortResultsDropdown

& > span {
width: 85%;
width: 80%;

button#headsign-selector-label {
align-items: center;
Expand Down
2 changes: 2 additions & 0 deletions lib/util/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ export interface RouteViewerConfig {
maxRealtimeVehicleAge?: number
/** Use OTP date limiting to only show current service week in list */
onlyShowCurrentServiceWeek?: boolean
/** Setting to sort routes by the number of vehicles on each pattern */
sortRoutePatternsByVehicleCount?: boolean
/** Disable vehicle highlight if necessary (e.g. custom or inverted icons) */
vehicleIconHighlight?: boolean
/** Customize vehicle icon padding (the default iconPadding is 2px in otp-ui) */
Expand Down
Loading