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

100% Statements 13/13
91.11% Branches 41/45
100% Functions 5/5
100% Lines 12/12

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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                  242x                                                           550x 70x 70x 70x 70x 70x 70x 70x       550x 550x                   550x                                                                                                                                
import React, { useEffect } from "react";
import { Button } from "@wintermuted/ui-react";
import FontAwesome from "react-fontawesome";
import { SearchRadiusSelection } from "./SearchRadiusSelection";
import { VehicleFeed } from "../util/useVehiclePositions";
import PlacesLoadStatus from "./PlacesLoadStatus";
 
export type LatLngCoords = number[];
 
interface Props {
  mapRef: any;
  mapContainerRef: any;
  initializeMap: () => void;
  onSearchArea: () => void;
  onDropPin: () => void;
  onFindMe: () => void;
  radiusSize: number;
  onRadiusChange: React.ChangeEventHandler<HTMLSelectElement>;
  isLocating: boolean;
  locationError?: boolean;
  isMapLoading: boolean;
  searchMode: "none" | "device" | "area" | "pin";
  vehiclesEnabled: boolean;
  onVehiclesToggle: () => void;
  vehicleFeed: VehicleFeed;
  vehicleMapError: boolean;
  onVehiclesRetry: () => void;
  children?: React.ReactNode;
  placesMode?: boolean;
  placesScope?: "map area" | "saved view" | "trip corridor";
}
 
export function NearbyMapSkeleton({ message }: { message: string }) {
  return (
    <div className="transit-nearby-map-skeleton" role="status">
      <span>{message}</span>
    </div>
  );
}
 
function NearbyMapV2({
  initializeMap,
  mapContainerRef,
  mapRef,
  onSearchArea,
  onDropPin,
  onFindMe,
  radiusSize,
  onRadiusChange,
  isLocating,
  locationError = false,
  isMapLoading,
  searchMode,
  vehiclesEnabled,
  onVehiclesToggle,
  vehicleFeed,
  vehicleMapError,
  onVehiclesRetry,
  children,
  placesMode = false,
  placesScope = "map area"
}: Props) {
  // Initialize Map
  useEffect(() => {
    initializeMap();
    const map = mapRef.current;
    const observer = new ResizeObserver(() => map.resize());
    observer.observe(mapContainerRef.current);
    return () => {
      observer.disconnect();
      map.remove();
    };
  }, []);
 
  const vehicleProblem = vehicleMapError || vehicleFeed.status === "error" || vehicleFeed.status === "delayed";
  const vehicleMessage = vehicleMapError ? "Vehicle symbols could not be displayed." :
    vehicleFeed.message || {
      loading: "Loading vehicle positions...",
      current: "Reported vehicle positions",
      empty: "No vehicles in this view.",
      delayed: "Waiting for fresh vehicle reports.",
      error: "Vehicle updates are unavailable.",
      paused: "Vehicle updates paused.",
    }[vehicleFeed.status];
 
  return (
    <div className="transit-nearby-map">
      <div className="transit-nearby-map-stage">
        <div className="transit-nearby-map-canvas" ref={mapContainerRef} />
        {isMapLoading && <NearbyMapSkeleton message="Loading map…" />}
      </div>
      <div className="sidebar transit-nearby-map-controls">
        <span className="transit-map-location-status" role="status">
          <FontAwesome
            name={searchMode === "pin" ? "map-marker" : searchMode === "device" ? "location-arrow" : "map"}
            aria-hidden="true"
          />
          {placesMode ? `Places in ${placesScope}` : searchMode === "pin" ? "Dropped pin" :
            searchMode === "area" ? "Map center" :
              searchMode === "device" ? "Near me" : "Choose area"}
        </span>
        <div className="transit-nearby-map-actions">
          <Button type="button" variant={vehiclesEnabled ? "primary" : "secondary"}
            className="transit-map-vehicles-toggle" aria-pressed={vehiclesEnabled}
            onClick={onVehiclesToggle}>
            <FontAwesome name="bus" aria-hidden="true" /> Vehicles
          </Button>
          {!placesMode && <SearchRadiusSelection
            compact
            radiusSize={radiusSize}
            handleRadiusSelectionChange={onRadiusChange}
          />}
          {!placesMode && <Button type="button" variant="secondary" className="transit-map-control-button" onClick={onSearchArea} title="Search this area" aria-label="Search this area">
            <FontAwesome name="search" aria-hidden="true" />
          </Button>}
          {!placesMode && <Button type="button" variant="secondary" className="transit-map-control-button" onClick={onDropPin} title="Drop a pin at the map center" aria-label="Drop a pin at the map center">
            <FontAwesome name="map-marker" aria-hidden="true" />
          </Button>}
          <Button type="button" variant="secondary" className="transit-map-control-button" onClick={onFindMe} disabled={isLocating} title="Find near me" aria-label={isLocating ? "Finding your location" : "Find near me"}>
            <FontAwesome name={isLocating ? "spinner" : "location-arrow"} spin={isLocating} aria-hidden="true" />
          </Button>
          <PlacesLoadStatus label="Current location" issues={locationError ? [{
            label: "Current location",
            message: "Current location is unavailable. Check browser permissions and retry, or keep browsing the map.",
            retry: onFindMe
          }] : []} />
        </div>
      </div>
      {vehiclesEnabled && (
        <div className="transit-vehicle-status">
            <span role="status">{vehicleMessage}</span>
            {vehicleFeed.updatedAt && !vehicleMapError && (
              <small>Updated <time dateTime={new Date(vehicleFeed.updatedAt).toISOString()}>
                {new Date(vehicleFeed.updatedAt).toLocaleTimeString([], { hour: "numeric", minute: "2-digit", second: "2-digit" })}
              </time></small>
            )}
            {vehicleProblem && <Button type="button" size="sm" variant="secondary" onClick={onVehiclesRetry}>Retry vehicles</Button>}
            {children}
            <span className="transit-vehicle-legend" aria-label="Vehicle symbols: bus and train">
              <FontAwesome name="bus" aria-hidden="true" /> Bus
              <FontAwesome name="train" aria-hidden="true" /> Train
            </span>
        </div>
      )}
    </div>
  );
}
 
export default NearbyMapV2;