All files / applications/go-by-transit/src/view/nearby/components NearbyPlacesDiscovery.tsx

31.42% Statements 11/35
73.52% Branches 25/34
30.43% Functions 7/23
40% Lines 10/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                              333x 333x                       333x 333x                           1x                                                                               2x                               209x 209x           627x       836x                        
import React from "react";
import { Button } from "@wintermuted/ui-react";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { Map } from "mapbox-gl";
import { placeDetailLink } from "@overworld/places/catalog";
import { LINE_OVERLAY_CATEGORIES } from "@overworld/places/lineOverlays";
import { CONTEXT_CATEGORIES } from "@overworld/places/contextOverlays";
import { placesPreset } from "@overworld/places/discovery";
import { NearbyPlacesDiscovery } from "./useNearbyPlacesDiscovery";
import NearbyPlaces from "./NearbyPlaces";
import NearbyNeighborhoodsMap from "./NearbyNeighborhoodsMap";
import NearbyLineOverlayMap from "./NearbyLineOverlayMap";
import NearbyContextMap from "./NearbyContextMap";
import NearbyPlacesMap from "./NearbyPlacesMap";
import NearbyDiscoveryControls from "./NearbyDiscoveryControls";
import NearbyTripCorridorMap from "./NearbyTripCorridorMap";
 
export function NearbyPlacesSidebar({ discovery, selectedPlaceId, currentLocation, searchMode }: {
  discovery: NearbyPlacesDiscovery;
  selectedPlaceId?: string;
  currentLocation: number[];
  searchMode: string;
}) {
  const location = useLocation();
  const navigate = useNavigate();
  const {
    coincidentIds, mappedPlaces, retainedViewSearch, setCoincidentIds, selectedPlace,
    selectedSource, selectedCategory, placeCatalog, placeFilters, setPlaceFilters,
    visiblePlaces, selectPlace, savedPlaces, placesScope, currentPlacesView, applyPlacesView,
    setSavedOnly, setAlongTrip, sharedPlacesView, shareSearch, savedOnly, unavailableSaved,
    tripCorridor, alongTrip, tripDistance, setTripDistance, tripMapError, setTripMapRevision,
    contextShown, contextSources, contextErrors, setContextShown, setContextRevisions,
    lineOverlaysShown, lineOverlaySources, lineOverlayErrors, setLineOverlaysShown,
    setLineOverlayRevisions, neighborhoods, neighborhoodsShown, setNeighborhoodsShown,
    neighborhoodPolygonsShown, setNeighborhoodPolygonsShown, neighborhoodMapError,
    setNeighborhoodLayerRevision, placeMapError, setPlaceMapRevision, placeBounds
  } = discovery;
  return (
    <>
      {coincidentIds.length > 1 && <section aria-label="Locations at this point">
        <h3>Locations at this point</h3>
        {mappedPlaces.filter(place => coincidentIds.includes(place.id)).map(place =>
          <Link key={place.id} className="wm-btn wm-btn-secondary" to={placeDetailLink(place.id, retainedViewSearch)}>{place.name}</Link>)}
        <Button variant="ghost" onClick={() => setCoincidentIds([])}>Dismiss locations</Button>
      </section>}
      {selectedPlaceId && !selectedPlace ? <section className="transit-places" aria-label="Place details">
        {selectedSource && !selectedSource.snapshot && !selectedSource.error ? (
          <p role="status">Loading place details...</p>
        ) : selectedSource?.error && selectedCategory ? (
          <div role="alert">
            <p>This place could not be loaded. Other places remain available.</p>
            <Button variant="secondary" onClick={() => placeCatalog.retry(selectedCategory)}>Retry place</Button>
          </div>
        ) : <p role="alert">Place not found. This location may no longer be in the current catalog.</p>}
      </section> : <NearbyPlaces sources={placeCatalog.sources} filters={placeFilters} onFilters={setPlaceFilters}
        visiblePlaces={visiblePlaces} selected={selectedPlace} onSelect={selectPlace}
        savedPlaces={savedPlaces} viewSearch={retainedViewSearch}
        areaLabel={placesScope}
        discovery={<>
          <NearbyDiscoveryControls view={currentPlacesView} onPreset={preset => {
            applyPlacesView(placesPreset(preset));
            if (preset === "default") { setSavedOnly(false); setAlongTrip(false); }
            if (sharedPlacesView.error) navigate(location.pathname, { replace: true });
          }} viewError={sharedPlacesView.error} shareHref={`/nearby/places${shareSearch}`}
            savedPlaces={savedPlaces} savedOnly={savedOnly} onSavedOnly={setSavedOnly}
            unavailable={unavailableSaved}
            trip={tripCorridor} alongTrip={alongTrip} onAlongTrip={setAlongTrip}
            distance={tripDistance} onDistance={setTripDistance} />
          {alongTrip && tripMapError && <p role="alert">The mapped trip could not be drawn. Distance filtering still uses its geometry.{" "}
            <Button size="sm" variant="secondary" onClick={() => setTripMapRevision(value => value + 1)}>Retry trip line</Button>
          </p>}
        </>}
        contextOverlays={{ shown: contextShown, sources: contextSources, mapErrors: contextErrors,
          onShown: (category, shown) => setContextShown(previous => ({ ...previous, [category]: shown })),
          onRetry: category => {
            contextSources[category].retry();
            setContextRevisions(previous => ({ ...previous, [category]: previous[category] + 1 }));
          } }}
        lineOverlays={{ shown: lineOverlaysShown, sources: lineOverlaySources, mapErrors: lineOverlayErrors,
          onShown: (category, shown) => setLineOverlaysShown(previous => ({ ...previous, [category]: shown })),
          onRetry: category => {
            lineOverlaySources[category].retry();
            setLineOverlayRevisions(previous => ({ ...previous, [category]: previous[category] + 1 }));
          } }}
        neighborhoods={{ ...neighborhoods, shown: neighborhoodsShown, onShown: setNeighborhoodsShown,
          polygonsShown: neighborhoodPolygonsShown, onPolygonsShown: setNeighborhoodPolygonsShown,
          mapError: neighborhoodMapError, onRetry: () => {
            neighborhoods.retry();
            setNeighborhoodLayerRevision(value => value + 1);
          } }}
        origin={currentLocation} originMode={searchMode} onRetry={placeCatalog.retry} mapError={placeMapError}
        onRetryMap={() => setPlaceMapRevision(value => value + 1)} mapReady={Boolean(placeBounds)} />}
    </>
  );
}
 
export function NearbyPlacesLayers({ discovery, map, selectedPlaceId }: {
  discovery: NearbyPlacesDiscovery;
  map: Map;
  selectedPlaceId?: string;
}) {
  const {
    neighborhoodsShown, neighborhoods, neighborhoodLayerRevision, neighborhoodPolygonsShown,
    setNeighborhoodMapError, lineOverlaysShown, lineOverlaySources, lineOverlayRevisions,
    handleLineOverlayError, contextShown, contextSources, contextRevisions, handleContextError,
    placeMapRevision, mappedPlaces, selectPlace, setCoincidentIds, updatePlaceBounds,
    setPlaceMapError, alongTrip, tripCorridor, tripMapRevision, setTripMapError
  } = discovery;
  return (
    <>
      {neighborhoodsShown && neighborhoods.snapshot &&
        <NearbyNeighborhoodsMap key={neighborhoodLayerRevision} map={map}
          snapshot={neighborhoods.snapshot} polygonsShown={neighborhoodPolygonsShown} onError={setNeighborhoodMapError} />}
      {LINE_OVERLAY_CATEGORIES.map(category =>
        lineOverlaysShown[category] && lineOverlaySources[category].snapshot &&
        <NearbyLineOverlayMap key={`${category}-${lineOverlayRevisions[category]}`} map={map}
          snapshot={lineOverlaySources[category].snapshot} onError={handleLineOverlayError} />)}
      {CONTEXT_CATEGORIES.map(category =>
        contextShown[category] && contextSources[category].snapshot &&
        <NearbyContextMap key={`${category}-${contextRevisions[category]}`} map={map}
          snapshot={contextSources[category].snapshot} onError={handleContextError} />)}
      <NearbyPlacesMap key={`places-${placeMapRevision}`} map={map} places={mappedPlaces}
        selectedId={selectedPlaceId} onSelect={selectPlace} onCoincident={setCoincidentIds}
        onBounds={updatePlaceBounds} onError={setPlaceMapError} />
      {alongTrip && tripCorridor &&
        <NearbyTripCorridorMap key={tripMapRevision} map={map} lines={tripCorridor.lines} onError={setTripMapError} />}
 
    </>
  );
}