All files / app/components Map.tsx

0% Statements 0/49
0% Branches 0/14
0% Functions 0/9
0% Lines 0/45

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178                                                                                                                                                                                                                                                                                                                                                                   
import "maplibre-gl/dist/maplibre-gl.css";
 
import { useMapSearchState } from "features/search/state/mapSearchContext";
import {
  clusterCountLayer,
  clusterLayer,
  SOURCE_CLUSTERED_USERS_ID,
  UNCLUSTERED_LAYER_ID,
  unclusteredPointLayer,
} from "features/search/utils/mapLayers";
import ZoomControl from "features/search/ZoomControl";
import { MapLayerMouseEvent, RequestParameters } from "maplibre-gl";
import React, { useRef } from "react";
import {
  Layer,
  Map as MaplibreMap,
  MapRef,
  Source,
  ViewStateChangeEvent,
} from "react-map-gl/maplibre";
 
export const API_BASE_URL = process.env.NEXT_PUBLIC_API_BASE_URL;
 
interface MapProps {
  grow?: boolean;
  hash?: boolean;
  mapRef: React.RefObject<MapRef>;
  onClick: (ev: MapLayerMouseEvent) => void;
  onLoad: () => void;
  onMapMove: () => void;
  onZoomIn: (newZoom: number) => void;
  onZoomOut: (newZoom: number) => void;
  onZoomControlInClick: (newZoom: number) => void;
  onZoomControlOutClick: (newZoom: number) => void;
  pins: string | GeoJSON.FeatureCollection;
}
 
const Map = ({
  grow,
  hash,
  mapRef,
  onClick,
  onLoad,
  onMapMove,
  onZoomIn,
  onZoomOut,
  onZoomControlInClick,
  onZoomControlOutClick,
  pins,
}: MapProps) => {
  const isZoomFromControlRef = useRef(false);
 
  const {
    uiOnly: { zoom },
  } = useMapSearchState();
 
  const handleMapLoad = () => {
    Iif (mapRef.current) {
      onLoad();
    }
  };
 
  const handleMapClick = async (event: MapLayerMouseEvent) => {
    onClick(event);
  };
 
  const handleDragEnd = () => {
    onMapMove();
  };
 
  const handleMouseMove = (event: MapLayerMouseEvent) => {
    const map = mapRef.current;
    Iif (!map) return;
 
    // Query the features (pins) under the mouse pointer
    const features = map.queryRenderedFeatures(event.point, {
      layers: [UNCLUSTERED_LAYER_ID], // Make sure pins are in this layer
    });
 
    // If there are any pins under the mouse, change cursor to pointer
    Iif (features.length > 0) {
      map.getCanvas().style.cursor = "pointer";
    }
  };
 
  const handleSetZoom = (viewState: ViewStateChangeEvent) => {
    Iif (isZoomFromControlRef.current) {
      isZoomFromControlRef.current = false; // reset the flag
      return; // skip regular zoom logic since already handled
    }
 
    Iif (viewState.viewState.zoom === zoom) return;
 
    const isZoomIn = viewState.viewState.zoom > zoom;
    const isZoomOut = viewState.viewState.zoom < zoom;
 
    Iif (isZoomIn) {
      onZoomIn(viewState.viewState.zoom);
    }
 
    Iif (isZoomOut) {
      onZoomOut(viewState.viewState.zoom);
    }
  };
 
  /*
    Allows sending cookies (counted as sensitive "credentials") on cross-origin requests when we grab GeoJSON/other data from the API.
    Those APIs will return an error if the session cookie is not set as these APIs are secure and not public.
    */
  const transformRequest = (url: string): RequestParameters => {
    Iif (url.startsWith(API_BASE_URL)) {
      return {
        credentials: "include",
        url,
      };
    }
    return { url };
  };
 
  const handleZoomControlInClick = (newZoom: number) => {
    isZoomFromControlRef.current = true;
    onZoomControlInClick(newZoom);
  };
 
  const handleZoomControlOutClick = (newZoom: number) => {
    isZoomFromControlRef.current = true;
    onZoomControlOutClick(newZoom);
  };
 
  return (
    <>
      <MaplibreMap
        id="map"
        style={{
          height: grow ? "100%" : "200px",
          width: grow ? "100%" : "400px",
        }}
        interactive={true}
        mapStyle="https://cdn.couchers.org/maps/couchers-basemap-style-v1.json"
        interactiveLayerIds={clusterLayer.id ? [clusterLayer.id] : []}
        onClick={handleMapClick}
        onLoad={handleMapLoad}
        onDragEnd={handleDragEnd}
        onMouseMove={handleMouseMove}
        onZoomEnd={handleSetZoom}
        hash={hash}
        ref={mapRef}
        transformRequest={transformRequest}
      >
        <Source
          id={SOURCE_CLUSTERED_USERS_ID}
          cluster={true}
          clusterMaxZoom={14}
          clusterRadius={50}
          data={pins}
          promoteId="id"
          type={"geojson"}
        >
          <Layer {...clusterLayer} />
          <Layer {...clusterCountLayer} />
          <Layer {...unclusteredPointLayer} />
        </Source>
        {/** WHY BUILD OUR OWN ZOOM CONTROL? The built in NavigationControl component from react-map-gl doesn't offer a
         * click event, nor does the underlying map-libre. We need a click event to control the api queries for the pins
         * and user cards on the map. */}
        <ZoomControl
          mapRef={mapRef}
          onZoomIn={handleZoomControlInClick}
          onZoomOut={handleZoomControlOutClick}
          isZoomFromControlRef={isZoomFromControlRef}
        />
      </MaplibreMap>
    </>
  );
};
 
export default Map;