@digitransit-component/digitransit-component-autosuggest 9.0.0 → 9.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -5533,6 +5533,15 @@ function hasFavourites(searchContext) {
5533
5533
  if (favouriteLocations?.length > 0) {
5534
5534
  return true;
5535
5535
  }
5536
+
5537
+ // Prefer the backend-validated signal when available: a stop/station/
5538
+ // bikeStation favourite may look locally available (e.g. a citybike
5539
+ // network whose season config is stale) while no longer being served by
5540
+ // OTP at all, which would otherwise offer a "favourites" suggestion that
5541
+ // opens to an empty list.
5542
+ if (typeof searchContext.getHasOtpLocationFavourites === 'function') {
5543
+ return !!searchContext.getHasOtpLocationFavourites(searchContext.context);
5544
+ }
5536
5545
  const favouriteVehicleRentalStations = searchContext.getFavouriteVehicleRentalStations(searchContext.context);
5537
5546
  if (favouriteVehicleRentalStations?.length > 0) {
5538
5547
  return true;
@@ -6391,7 +6400,7 @@ function getSuggestionContent(item, lng, t) {
6391
6400
  let [name, label] = getNameLabel(item.properties, true);
6392
6401
  let suggestionType;
6393
6402
  if (item.properties.layer.toLowerCase().includes('bikerental') || item.properties.layer.toLowerCase().includes('bikestation')) {
6394
- suggestionType = t('vehiclerentalstation', {
6403
+ suggestionType = t('bikerentalstation', {
6395
6404
  lng
6396
6405
  });
6397
6406
  const stopCode = item.properties.labelId;
@@ -7301,6 +7310,7 @@ const getNewTargets = ({
7301
7310
  * getFavouriteRoutesQuery: () => ({}), // Function that returns query for fetching favourite routes.
7302
7311
  * getFavouriteVehicleRentalStations: () => ({}), // Function that returns favourite bike rental station.
7303
7312
  * getFavouriteVehicleRentalStationsQuery: () => ({}), // Function that returns query for fetching favourite bike rental stations.
7313
+ * getHasOtpLocationFavourites: () => ({}), // Optional. Function that returns whether any OTP-backed favourite is currently available.
7304
7314
  * startLocationWatch: () => ({}), // Function that locates users geolocation.
7305
7315
  * saveSearch: () => ({}), // Function that saves search to old searches store.
7306
7316
  * clearOldSearches: () => ({}), // Function that clears old searches store.
@@ -7455,6 +7465,8 @@ function DTAutosuggest({
7455
7465
  // not something that should itself trigger a render or round-trip through
7456
7466
  // another dispatch.
7457
7467
  const pendingEnterRef = useRef(false);
7468
+ // Identifies the latest search so results of superseded searches are ignored.
7469
+ const searchIdRef = useRef(0);
7458
7470
  useEffect(() => {
7459
7471
  if (storeRef) {
7460
7472
  storeRef(inputRef.current);
@@ -7472,9 +7484,14 @@ function DTAutosuggest({
7472
7484
  });
7473
7485
  // remove location favourites in desktop search (collection item replaces it in target array)
7474
7486
  const newSources = state.sources ? state.sources.filter(s => !isLocationSearch || !s === 'Favourite' || state.showOwnPlaces || isMobile) : state.sources;
7487
+ searchIdRef.current += 1;
7488
+ const searchId = searchIdRef.current;
7475
7489
  executeSearch(newTargets, newSources, transportMode, searchContext, filterResults, geocodingSize, {
7476
7490
  input: state.value || ''
7477
7491
  }, searchResult => {
7492
+ if (searchId !== searchIdRef.current) {
7493
+ return;
7494
+ }
7478
7495
  if (searchResult == null) {
7479
7496
  dispatch({
7480
7497
  type: 'FETCH_SUGGESTIONS',