All files / app/components Map.tsx

69.23% Statements 27/39
40% Branches 4/10
66.66% Functions 4/6
73.52% Lines 25/34

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 1429x   9x 9x 9x 9x 9x 9x   9x   9x   9x                                                                       9x                     5x 5x 5x           5x                   5x   5x 3x     3x   3x 3x                                                 3x 3x                     5x   5x                              
import "maplibre-gl/dist/maplibre-gl.css";
 
import { Typography } from "@material-ui/core";
import classNames from "classnames";
import { NO_MAP_SUPPORT } from "components/constants";
import maplibregl, { LngLat, RequestParameters } from "maplibre-gl";
import { useEffect, useRef, useState } from "react";
import makeStyles from "utils/makeStyles";
 
const URL = process.env.NEXT_PUBLIC_API_BASE_URL;
 
maplibregl.accessToken = process.env.NEXT_PUBLIC_MAPBOX_KEY!;
 
const useStyles = makeStyles({
  root: {
    position: "relative",
    height: 200,
    width: 400,
  },
  grow: {
    height: "100%",
    width: "100%",
  },
  map: {
    position: "absolute",
    bottom: 0,
    top: 0,
    width: "100%",
  },
  noMapText: {
    display: "flex",
    alignItems: "center",
    justifyContent: "center",
    width: "100%",
    height: "100%",
  },
});
 
export interface MapProps {
  initialCenter: LngLat | undefined;
  initialZoom: number;
  postMapInitialize?: (map: maplibregl.Map) => 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);
  const classes = useStyles();
 
  /*
  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<maplibregl.Map>();
 
  useEffect(() => {
    Iif (!containerRef.current) return;
 
    //don't create a new map if it exists already
    Iif (mapRef.current) return;
 
    try {
      const map = new maplibregl.Map({
        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 maplibregl.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);
    }
  }, [
    initialCenter,
    initialZoom,
    interactive,
    onUpdate,
    postMapInitialize,
    hash,
  ]);
 
  useEffect(() => () => mapRef?.current?.remove(), []);
 
  return (
    <div
      className={classNames(classes.root, { [classes.grow]: grow }, className)}
      {...otherProps}
    >
      <div className={classes.map} ref={containerRef}>
        {noMap && (
          <div className={classes.noMapText}>
            <Typography variant="body1">{NO_MAP_SUPPORT}</Typography>
          </div>
        )}
      </div>
    </div>
  );
}