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

80.76% Statements 21/26
41.66% Branches 5/12
66.66% Functions 4/6
89.47% Lines 17/19

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        8x       3x 1x   1x 1x     1x       1x     1x 1x 1x 1x 1x 1x 1x 1x 1x     3x    
import { Position } from "geojson";
import { Map as MapboxMap } from "mapbox-gl";
import { useEffect } from "react";
 
const SOURCE = "nearby-trip-corridor";
export default function NearbyTripCorridorMap({ map, lines, onError }: {
  map?: MapboxMap; lines: Position[][]; onError: (error: boolean) => void;
}) {
  useEffect(() => {
    Iif (!map) return;
    function install() {
      try {
        Eif (!map.getSource(SOURCE)) map.addSource(SOURCE, { type: "geojson", data: {
          type: "Feature", properties: {}, geometry: { type: "MultiLineString", coordinates: lines }
        } });
        Eif (!map.getLayer(SOURCE)) map.addLayer({
          id: SOURCE, source: SOURCE, type: "line",
          paint: { "line-color": "#1559d1", "line-width": 4, "line-opacity": 0.85, "line-emissive-strength": 1 }
        }, map.getStyle()?.layers?.find(layer => layer.id.startsWith("nearby-places-"))?.id);
        onError(!map.getLayer(SOURCE));
      } catch { onError(true); }
    }
    const sourceError = (event: { error: Error; sourceId?: string }) => { if (event.sourceId === SOURCE) onError(true); };
    map.on("style.load", install);
    map.on("error", sourceError);
    install();
    return () => {
      map.off("style.load", install);
      map.off("error", sourceError);
      Eif (map.getLayer(SOURCE)) map.removeLayer(SOURCE);
      Eif (map.getSource(SOURCE)) map.removeSource(SOURCE);
    };
  }, [map, lines, onError]);
  return null;
}