All files / app/components Map.tsx

67.56% Statements 25/37
38.46% Branches 5/13
66.66% Functions 4/6
72.72% Lines 24/33

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 1318x   8x 8x           8x 8x   8x   8x           8x               8x                                     4x                     5x 5x           5x                   5x   5x 2x     2x   2x 2x                                             2x 2x     2x 2x 2x                                  
import "maplibre-gl/dist/maplibre-gl.css";
 
import { styled, Typography } from "@mui/material";
import { NO_MAP_SUPPORT } from "components/constants";
import {
  LngLat,
  Map as MaplibreMap,
  NavigationControl,
  RequestParameters,
} from "maplibre-gl";
import { useEffect, useRef, useState } from "react";
 
const URL = process.env.NEXT_PUBLIC_API_BASE_URL;
 
const StyledWrapper = styled("div")<{ grow?: boolean }>(({ grow }) => ({
  position: "relative",
  height: grow ? "100%" : "200px",
  width: grow ? "100%" : "400px",
}));
 
const StyledMap = styled("div")({
  position: "absolute",
  bottom: 0,
  top: 0,
  width: "100%",
  height: "100%", // Add this to ensure the child takes the parent's height
});
 
const StyledNoMapText = styled("div")({
  display: "flex",
  alignItems: "center",
  justifyContent: "center",
  width: "100%",
  height: "100%",
});
 
export interface MapProps {
  initialCenter: LngLat | undefined;
  initialZoom: number;
  postMapInitialize?: (map: MaplibreMap) => void;
  className?: string;
  onUpdate?: (center: LngLat, zoom: number) => void;
  grow?: boolean;
  interactive?: boolean;
  hash?: boolean;
}
 
export default function Map({
  initialCenter,
  initialZoom,
  grow,
  postMapInitialize,
  onUpdate,
  hash,
  interactive = true,
  className,
  ...otherProps
}: MapProps) {
  const containerRef = useRef<HTMLDivElement>(null);
  const [noMap, setNoMap] = useState(false);
 
  /*
  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(URL)) {
      return {
        credentials: "include",
        url,
      };
    }
    return { url };
  };
 
  const mapRef = useRef<MaplibreMap>();
 
  useEffect(() => {
    Iif (!containerRef.current) return;
 
    // don't create a new map if it exists already
    Iif (mapRef.current) return;
 
    try {
      const map = new MaplibreMap({
        center: initialCenter,
        container: containerRef.current,
        hash: hash ? "loc" : false,
        interactive: interactive,
        style: "https://cdn.couchers.org/maps/couchers-basemap-style-v1.json",
        transformRequest,
        zoom: initialZoom,
      });
 
      mapRef.current = map;
 
      Iif (interactive) {
        map.addControl(new NavigationControl({ showCompass: false }));
      }
 
      Iif (onUpdate) {
        map.on("moveend", () => onUpdate(map.getCenter(), map.getZoom()));
      }
 
      postMapInitialize?.(map);
    } catch {
      //probably no webgl
      console.warn("Couldn't initialize maplibre gl");
      setNoMap(true);
    }
 
    return () => {
      mapRef.current?.remove();
      mapRef.current = undefined;
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);
 
  return (
    <StyledWrapper className={className} grow={grow} {...otherProps}>
      <StyledMap ref={containerRef}>
        {noMap && (
          <StyledNoMapText>
            <Typography variant="body1">{NO_MAP_SUPPORT}</Typography>
          </StyledNoMapText>
        )}
      </StyledMap>
    </StyledWrapper>
  );
}