All files / app/features/search/utils mapUtils.ts

60.46% Statements 26/43
20% Branches 2/10
54.54% Functions 6/11
63.15% Lines 24/38

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        4x     4x   4x                             4x             4x       4x                       4x     4x 28x 448x   28x 28x     4x 16x   4x 3x 3x 3x 3x 3x 3x         12x 16x 3x          
import { FeatureCollection } from "geojson";
import { SearchUser } from "proto/search_pb";
import { MapRef } from "react-map-gl/maplibre";
 
import userPin from "../resources/userPin.png";
import { MapSearchState } from "../state/mapSearchReducers";
import { Coordinates } from "./constants";
import { USERS_SOURCE_ID } from "./mapLayers";
 
const usersToGeoJSON = (pins: SearchUser.AsObject[]): FeatureCollection => ({
  type: "FeatureCollection",
  features: pins.map((pin) => ({
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [pin.lng, pin.lat], // GeoJSON expects [lng, lat]
    },
    properties: {
      id: pin.userId,
      hasCompletedProfile: pin.hasCompletedProfile,
    },
  })),
});
 
const clearMapFeatureState = (mapRef: React.RefObject<MapRef | null>) => {
  const map = mapRef.current?.getMap();
  if (map) {
    map.removeFeatureState({ source: USERS_SOURCE_ID });
  }
};
 
const setMapFeatureState = (mapRef: React.RefObject<MapRef | null>, id: string, selected: boolean) => {
  mapRef.current?.setFeatureState({ source: USERS_SOURCE_ID, id }, { selected });
};
 
const loadMapUserPins = async (mapRef: React.RefObject<MapRef | null>) => {
  const image = await mapRef.current?.loadImage(userPin.src);
 
  if (mapRef.current?.hasImage("user-pin")) return;
 
  if (image) {
    mapRef.current?.addImage("user-pin", image.data, { sdf: true });
  }
  return;
};
 
// age has a min and a max, but it's one filter to the member
const AGE_FILTER_KEYS: string[] = ["ageMin", "ageMax"];
 
/** How many filters differ from their defaults, counting age (min and max) as one. */
const countActiveFilters = (filters: MapSearchState["filters"], initialFilters: MapSearchState["filters"]): number => {
  const changed = (Object.keys(initialFilters) as (keyof MapSearchState["filters"])[]).filter(
    (key) => filters[key] !== initialFilters[key],
  );
  const ageChanged = changed.some((key) => AGE_FILTER_KEYS.includes(key));
  return changed.filter((key) => !AGE_FILTER_KEYS.includes(key)).length + (ageChanged ? 1 : 0);
};
 
const getHasActiveFilters = (state: MapSearchState, initialState: MapSearchState) =>
  countActiveFilters(state.filters, initialState.filters) > 0;
 
const getMapBounds = (mapRef: React.RefObject<MapRef | null>) => {
  const mapBounds = mapRef.current?.getMap().getBounds();
  Iif (!mapBounds) return;
  const ne = mapBounds.getNorthEast();
  const sw = mapBounds.getSouthWest();
  const bbox: Coordinates = [sw.lng, sw.lat, ne.lng, ne.lat];
  return bbox;
};
 
export {
  clearMapFeatureState,
  countActiveFilters,
  getHasActiveFilters,
  getMapBounds,
  loadMapUserPins,
  setMapFeatureState,
  usersToGeoJSON,
};