All files / src/components/map BasemapLayer.tsx

100% Statements 14/14
100% Branches 4/4
100% Functions 4/4
100% Lines 13/13

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                    24x               24x 3x 3x 2x                       24x 1x 1x 1x 1x 1x 1x     1x        
import React, { useEffect } from "react";
import { TileLayer, useMap } from "react-leaflet";
import {
  BASEMAP_ATTRIBUTION,
  BASEMAP_MAX_ZOOM,
  basemapKey,
  basemapUrl,
} from "./tiles";
 
/** Shown in the attribution corner when there is no key to draw tiles with. */
export const MISSING_KEY_NOTE = "Map tiles not configured";
 
/** The tile layer every map draws on, in the app's theme.
 *
 *  Without a key it requests nothing — CARTO would only answer with
 *  watermarks, and each request would still tell it where the user is
 *  looking — and says so in the attribution corner, so markers and shapes
 *  still read on the map's plain background. */
const BasemapLayer: React.FC<{ dark: boolean }> = ({ dark }) => {
  const key = basemapKey();
  if (!key) return <MissingKeyNote />;
  return (
    <TileLayer
      url={basemapUrl(dark ? "dark" : "light", key)}
      attribution={BASEMAP_ATTRIBUTION}
      maxZoom={BASEMAP_MAX_ZOOM}
      // A domain-locked key is checked against the Referer, so send the
      // origin whatever the page's own policy becomes — and only the origin.
      referrerPolicy="strict-origin"
    />
  );
};
 
const MissingKeyNote: React.FC = () => {
  const map = useMap();
  useEffect(() => {
    const control = map.attributionControl;
    control?.addAttribution(MISSING_KEY_NOTE);
    return () => {
      control?.removeAttribution(MISSING_KEY_NOTE);
    };
  }, [map]);
  return null;
};
 
export default BasemapLayer;