From 2786f23525608b0e0060258d76d864f6cf66910a Mon Sep 17 00:00:00 2001 From: miles-grant-ibigroup Date: Wed, 10 Apr 2024 10:39:48 -0400 Subject: [PATCH 1/5] support route viewer sorting in nearby view --- example-config.yml | 4 +++- lib/components/viewers/nearby/stop.tsx | 27 ++++++++++++++++++++------ lib/util/config-types.ts | 6 +++++- package.json | 2 +- yarn.lock | 11 ++++++++++- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/example-config.yml b/example-config.yml index 2ab5da22e..9346f909f 100644 --- a/example-config.yml +++ b/example-config.yml @@ -660,4 +660,6 @@ itinerary: ### Setting to hide stops with no depatures in the nearby view. # hideEmptyStops: true ### What radius should the nearby query get results within? (in meters) -# radius: 600 \ No newline at end of file +# radius: 600 +### Setting this to true will use the route viewer sort algorithm on routes +# useRouteViewSort: false \ No newline at end of file diff --git a/lib/components/viewers/nearby/stop.tsx b/lib/components/viewers/nearby/stop.tsx index 18b71905d..15f4a9362 100644 --- a/lib/components/viewers/nearby/stop.tsx +++ b/lib/components/viewers/nearby/stop.tsx @@ -1,6 +1,7 @@ import { Calendar } from '@styled-icons/fa-solid' import { connect } from 'react-redux' import { FormattedMessage } from 'react-intl' +import { TransitOperator } from '@opentripplanner/types' import coreUtils from '@opentripplanner/core-utils' import React from 'react' @@ -24,14 +25,30 @@ type Props = { homeTimezone: string nearbyViewConfig?: NearbyViewConfig stopData: StopData + transitOperators: TransitOperator[] } const Stop = ({ fromToSlot, homeTimezone, nearbyViewConfig, - stopData + stopData, + transitOperators }: Props): JSX.Element => { + const generateRouteSortComparator = () => { + if (nearbyViewConfig?.useRouteViewSort) { + return (a: PatternStopTime, b: PatternStopTime) => + coreUtils.route.makeRouteComparator(transitOperators)( + // @ts-expect-error core-utils types are wrong! + a.pattern.route, + b.pattern.route + ) + } + + // Default sort: departure time + return (a: PatternStopTime, b: PatternStopTime) => + fullTimestamp(a.stoptimes?.[0]) - fullTimestamp(b.stoptimes?.[0]) + } const patternRows = (stopData.stoptimesForPatterns || []) ?.reduce((acc, cur) => { const currentHeadsign = extractHeadsignFromPattern(cur.pattern) @@ -52,10 +69,7 @@ const Stop = ({ } return acc }, []) - .sort( - (a: PatternStopTime, b: PatternStopTime) => - fullTimestamp(a.stoptimes?.[0]) - fullTimestamp(b.stoptimes?.[0]) - ) + .sort(generateRouteSortComparator()) .map((st: any, index: number) => { const sortedStopTimes = st.stoptimes.sort( (a: StopTime, b: StopTime) => fullTimestamp(a) - fullTimestamp(b) @@ -105,7 +119,8 @@ const mapStateToProps = (state: AppReduxState) => { const { config } = state.otp return { homeTimezone: config.homeTimezone, - nearbyViewConfig: config?.nearbyView + nearbyViewConfig: config?.nearbyView, + transitOperators: config?.transitOperators || [] } } diff --git a/lib/util/config-types.ts b/lib/util/config-types.ts index 377213cda..2d84fad21 100644 --- a/lib/util/config-types.ts +++ b/lib/util/config-types.ts @@ -68,7 +68,11 @@ interface ApiKeyConfig { export type BugsnagConfig = ApiKeyConfig export type MapillaryConfig = ApiKeyConfig -export type NearbyViewConfig = { hideEmptyStops?: boolean; radius?: number } +export type NearbyViewConfig = { + hideEmptyStops?: boolean + radius?: number + useRouteViewSort?: boolean +} /** TODO: Language settings */ export type LanguageConfig = Record diff --git a/package.json b/package.json index b17006b81..65211ef16 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,7 @@ "@bugsnag/js": "^7.17.0", "@bugsnag/plugin-react": "^7.17.0", "@floating-ui/react": "^0.19.2", - "@opentripplanner/base-map": "^3.0.17", + "@opentripplanner/base-map": "^3.1.0", "@opentripplanner/core-utils": "^11.3.1", "@opentripplanner/endpoints-overlay": "^2.0.12", "@opentripplanner/from-to-location-picker": "^2.1.12", diff --git a/yarn.lock b/yarn.lock index c7380eae9..9ecf65e8a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2383,7 +2383,7 @@ dependencies: "@octokit/openapi-types" "^10.0.0" -"@opentripplanner/base-map@^3.0.16", "@opentripplanner/base-map@^3.0.17": +"@opentripplanner/base-map@^3.0.16": version "3.0.17" resolved "https://registry.yarnpkg.com/@opentripplanner/base-map/-/base-map-3.0.17.tgz#01862a7925b59be355e3229edac954ef876343cb" integrity sha512-cWfhquu7PrLnqte03Mj5XQJW3OSrRtwq10/s2Az523X9yeheZsiO4sm/Bfmizr9xhqec/UTl882nOVKjESHzHA== @@ -2392,6 +2392,15 @@ maplibre-gl "^2.1.9" react-map-gl "^7.0.15" +"@opentripplanner/base-map@^3.1.0": + version "3.1.0" + resolved "https://registry.yarnpkg.com/@opentripplanner/base-map/-/base-map-3.1.0.tgz#3181d4ec17029db8a40ca1342e20d1a0d449931f" + integrity sha512-feOSOWmiBFvIFWyG+OllCMxOcTmRKzxIoKKeoEgUFjNdVnrkZM6HH5r0m5IaQA9wO7T0N0K64hn/zdRQZBv0rQ== + dependencies: + mapbox-gl "npm:empty-npm-package@1.0.0" + maplibre-gl "^2.1.9" + react-map-gl "^7.0.15" + "@opentripplanner/core-utils@^11.2.3", "@opentripplanner/core-utils@^11.3.1": version "11.3.1" resolved "https://registry.yarnpkg.com/@opentripplanner/core-utils/-/core-utils-11.3.1.tgz#e42d95aa3d6b6cd167e8c63222c726d2ae6ebdd6" From dc9f3ec7b1ffbe8c4f4d9f28355f2a23c2abb79a Mon Sep 17 00:00:00 2001 From: miles-grant-ibigroup Date: Wed, 10 Apr 2024 10:40:09 -0400 Subject: [PATCH 2/5] update snapshots --- .../components/viewers/__snapshots__/nearby-view.js.snap | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/__tests__/components/viewers/__snapshots__/nearby-view.js.snap b/__tests__/components/viewers/__snapshots__/nearby-view.js.snap index cdb1cbc46..f3d46d8f9 100644 --- a/__tests__/components/viewers/__snapshots__/nearby-view.js.snap +++ b/__tests__/components/viewers/__snapshots__/nearby-view.js.snap @@ -7598,6 +7598,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } + transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` ], } } + transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` ], } } + transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` ], } } + transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` ], } } + transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` ], } } + transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` ], } } + transitOperators={Array []} >
Date: Wed, 10 Apr 2024 10:52:53 -0400 Subject: [PATCH 3/5] dont regenerate route sort comparator on every render --- lib/components/viewers/nearby/stop.tsx | 42 ++++++++++++++------------ 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/components/viewers/nearby/stop.tsx b/lib/components/viewers/nearby/stop.tsx index 15f4a9362..0fdc0a694 100644 --- a/lib/components/viewers/nearby/stop.tsx +++ b/lib/components/viewers/nearby/stop.tsx @@ -24,31 +24,17 @@ type Props = { fromToSlot: JSX.Element homeTimezone: string nearbyViewConfig?: NearbyViewConfig + routeSortComparator: (a: PatternStopTime, b: PatternStopTime) => number stopData: StopData - transitOperators: TransitOperator[] } const Stop = ({ fromToSlot, homeTimezone, nearbyViewConfig, - stopData, - transitOperators + routeSortComparator, + stopData }: Props): JSX.Element => { - const generateRouteSortComparator = () => { - if (nearbyViewConfig?.useRouteViewSort) { - return (a: PatternStopTime, b: PatternStopTime) => - coreUtils.route.makeRouteComparator(transitOperators)( - // @ts-expect-error core-utils types are wrong! - a.pattern.route, - b.pattern.route - ) - } - - // Default sort: departure time - return (a: PatternStopTime, b: PatternStopTime) => - fullTimestamp(a.stoptimes?.[0]) - fullTimestamp(b.stoptimes?.[0]) - } const patternRows = (stopData.stoptimesForPatterns || []) ?.reduce((acc, cur) => { const currentHeadsign = extractHeadsignFromPattern(cur.pattern) @@ -69,7 +55,7 @@ const Stop = ({ } return acc }, []) - .sort(generateRouteSortComparator()) + .sort(routeSortComparator) .map((st: any, index: number) => { const sortedStopTimes = st.stoptimes.sort( (a: StopTime, b: StopTime) => fullTimestamp(a) - fullTimestamp(b) @@ -117,10 +103,26 @@ const Stop = ({ const mapStateToProps = (state: AppReduxState) => { const { config } = state.otp + const nearbyViewConfig = config?.nearbyView + const transitOperators = config?.transitOperators || [] + + // Default sort: departure time + let routeSortComparator = (a: PatternStopTime, b: PatternStopTime) => + fullTimestamp(a.stoptimes?.[0]) - fullTimestamp(b.stoptimes?.[0]) + + if (nearbyViewConfig?.useRouteViewSort) { + routeSortComparator = (a: PatternStopTime, b: PatternStopTime) => + coreUtils.route.makeRouteComparator(transitOperators)( + // @ts-expect-error core-utils types are wrong! + a.pattern.route, + b.pattern.route + ) + } + return { homeTimezone: config.homeTimezone, - nearbyViewConfig: config?.nearbyView, - transitOperators: config?.transitOperators || [] + nearbyViewConfig, + routeSortComparator } } From 873bbcb67cd421e992a38c887ccf4bae00fbc3ee Mon Sep 17 00:00:00 2001 From: miles-grant-ibigroup Date: Wed, 10 Apr 2024 10:58:13 -0400 Subject: [PATCH 4/5] update snapshots --- .../viewers/__snapshots__/nearby-view.js.snap | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/__tests__/components/viewers/__snapshots__/nearby-view.js.snap b/__tests__/components/viewers/__snapshots__/nearby-view.js.snap index f3d46d8f9..fa879ac48 100644 --- a/__tests__/components/viewers/__snapshots__/nearby-view.js.snap +++ b/__tests__/components/viewers/__snapshots__/nearby-view.js.snap @@ -7035,6 +7035,7 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` /> } homeTimezone="America/Los_Angeles" + routeSortComparator={[Function]} stopData={ Object { "__typename": "Stop", @@ -7598,7 +7599,6 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } - transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` /> } homeTimezone="America/Los_Angeles" + routeSortComparator={[Function]} stopData={ Object { "__typename": "Stop", @@ -15191,7 +15192,6 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } - transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` /> } homeTimezone="America/Los_Angeles" + routeSortComparator={[Function]} stopData={ Object { "__typename": "Stop", @@ -23452,7 +23453,6 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } - transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` /> } homeTimezone="America/Los_Angeles" + routeSortComparator={[Function]} stopData={ Object { "__typename": "Stop", @@ -33908,7 +33909,6 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } - transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` /> } homeTimezone="America/Los_Angeles" + routeSortComparator={[Function]} stopData={ Object { "__typename": "Stop", @@ -40956,7 +40957,6 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } - transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` /> } homeTimezone="America/Los_Angeles" + routeSortComparator={[Function]} stopData={ Object { "__typename": "Stop", @@ -48056,7 +48057,6 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } - transitOperators={Array []} >
viewers > nearby view renders proper scooter dates 1`] = ` /> } homeTimezone="America/Los_Angeles" + routeSortComparator={[Function]} stopData={ Object { "__typename": "Stop", @@ -59417,7 +59418,6 @@ exports[`components > viewers > nearby view renders proper scooter dates 1`] = ` ], } } - transitOperators={Array []} >
Date: Wed, 10 Apr 2024 12:03:28 -0400 Subject: [PATCH 5/5] update otp-ui --- package.json | 4 ++-- yarn.lock | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 65211ef16..2f1602660 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "@opentripplanner/route-viewer-overlay": "^2.0.15", "@opentripplanner/stop-viewer-overlay": "^2.0.8", "@opentripplanner/stops-overlay": "^5.2.2", - "@opentripplanner/transit-vehicle-overlay": "^4.0.9", + "@opentripplanner/transit-vehicle-overlay": "^4.0.10", "@opentripplanner/transitive-overlay": "^3.0.18", "@opentripplanner/trip-details": "^5.0.11", "@opentripplanner/trip-form": "^3.5.2", @@ -140,7 +140,7 @@ "@graphql-tools/schema": "^10.0.0", "@jackwilsdon/craco-use-babelrc": "^1.0.0", "@opentripplanner/scripts": "^1.2.0", - "@opentripplanner/types": "^6.4.0", + "@opentripplanner/types": "^6.4.1", "@percy/cli": "^1.20.3", "@percy/puppeteer": "^2.0.2", "@pmmmwh/react-refresh-webpack-plugin": "^0.5.1", diff --git a/yarn.lock b/yarn.lock index 9ecf65e8a..7f1c479a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2573,10 +2573,10 @@ "@opentripplanner/map-popup" "^3.0.2" flat "^5.0.2" -"@opentripplanner/transit-vehicle-overlay@^4.0.9": - version "4.0.9" - resolved "https://registry.yarnpkg.com/@opentripplanner/transit-vehicle-overlay/-/transit-vehicle-overlay-4.0.9.tgz#f193de3444445fefe1bf8e40e312d53ea2a70dc7" - integrity sha512-VajxLn+qIOQ17Z7vdjWFmImDz4aKJ4mzQsztZH0zSZepmEFD5HLqdlvm1RGRbpLbnAU2XWGVgAAT8NTT5YxVyw== +"@opentripplanner/transit-vehicle-overlay@^4.0.10": + version "4.0.10" + resolved "https://registry.yarnpkg.com/@opentripplanner/transit-vehicle-overlay/-/transit-vehicle-overlay-4.0.10.tgz#a63e86a2dc5108ddf47ba8d8c0c9f5017c65c86a" + integrity sha512-j8+KCyeCD7WLQgAIbtzX7ckwqLU1fwKjXCMJ5E5/t75YqNuWB8hAiUonerIanuA20TUinm4sPwSL3dr9OnGNlA== dependencies: "@opentripplanner/base-map" "^3.0.16" "@opentripplanner/core-utils" "^11.2.3" @@ -2635,10 +2635,10 @@ "@opentripplanner/base-map" "^3.0.16" "@opentripplanner/core-utils" "^11.2.3" -"@opentripplanner/types@^6.4.0": - version "6.4.0" - resolved "https://registry.yarnpkg.com/@opentripplanner/types/-/types-6.4.0.tgz#6f7a3475ea982c7b7d8b2f1383a6d775dfabe62a" - integrity sha512-PS+CUwETLf0WzAUZg3qiey+SBigaf0CfknKF1XMOM+zJVc2c8nN34hgnwV7sS+RKs030KZFAgIn8p035ErcBuQ== +"@opentripplanner/types@^6.4.1": + version "6.4.1" + resolved "https://registry.yarnpkg.com/@opentripplanner/types/-/types-6.4.1.tgz#a01e612e258d873840a3222bbc7dd446e7c502c4" + integrity sha512-gSaEIxdXJjOoUcOKi+JNVkcT58SyqU5RnU3zoBVKrH8DYjXOTqX058NmmzPpptL3yEZRXysjlIyRL/xo2x5Wlw== "@opentripplanner/vehicle-rental-overlay@^2.1.7": version "2.1.7"