All files / api/places/src neighborhoods.ts

94.59% Statements 35/37
86.66% Branches 52/60
100% Functions 17/17
100% Lines 27/27

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                        26x   5x 5x       6x         8x         6x         4x 6x   2x                   557x 557x 557x 78x 5x 5x   5x 5x 5x     4x 4x   5x 1x 1x 1x     4x   5x   557x    
import { FeatureCollection, MultiPolygon, Polygon } from "geojson";
import { useEffect, useState } from "react";
import { PlaceSnapshot, safePlaceUrl, validPlacePoint } from "./places";
 
export type NeighborhoodSnapshot = FeatureCollection<Polygon | MultiPolygon, { name: string; shared: boolean; areaId: number }> &
  Pick<PlaceSnapshot, "version" | "retrievedAt" | "source" | "sourceUrl" | "license" | "licenseUrl" | "coverage">;
export interface NeighborhoodState {
  snapshot?: NeighborhoodSnapshot;
  loading: boolean;
  error: boolean;
}
 
const record = (value: unknown): value is Record<string, unknown> => typeof value === "object" && value !== null;
function validPolygon(value: unknown): boolean {
  return Array.isArray(value) && value.length > 0 && value.every(ring =>
    Array.isArray(ring) && ring.length >= 4 && ring.every(validPlacePoint) &&
    ring[0][0] === ring[ring.length - 1][0] && ring[0][1] === ring[ring.length - 1][1]);
}
function validGeometry(value: unknown): boolean {
  return record(value) && (value.type === "Polygon" ? validPolygon(value.coordinates) :
    value.type === "MultiPolygon" && Array.isArray(value.coordinates) &&
      value.coordinates.length > 0 && value.coordinates.every(validPolygon));
}
export function parseNeighborhoodSnapshot(value: unknown): NeighborhoodSnapshot {
  if (!record(value) || value.type !== "FeatureCollection" || value.version !== 1 ||
      typeof value.retrievedAt !== "string" || !Number.isFinite(Date.parse(value.retrievedAt)) ||
      typeof value.source !== "string" || !safePlaceUrl(value.sourceUrl) ||
      typeof value.license !== "string" || !safePlaceUrl(value.licenseUrl) || typeof value.coverage !== "string" ||
      !Array.isArray(value.features) || !value.features.length ||
      !value.features.every(feature => record(feature) && feature.type === "Feature" &&
        record(feature.properties) && typeof feature.properties.name === "string" && feature.properties.name.trim() &&
        Number.isInteger(feature.properties.areaId) && Number(feature.properties.areaId) >= 0 &&
        feature.id === `portland:neighborhood:${feature.properties.areaId}` &&
        typeof feature.properties.shared === "boolean" && validGeometry(feature.geometry)) ||
      new Set(value.features.map(feature => feature.id)).size !== value.features.length) {
    throw new Error("Invalid Portland neighborhood snapshot");
  }
  return {
    type: "FeatureCollection", version: 1, retrievedAt: value.retrievedAt,
    source: value.source, sourceUrl: value.sourceUrl, license: value.license,
    licenseUrl: value.licenseUrl, coverage: value.coverage,
    features: value.features as NeighborhoodSnapshot["features"]
  };
}
 
let cache: NeighborhoodSnapshot | undefined;
export function useNeighborhoods(enabled: boolean) {
  const [state, setState] = useState<NeighborhoodState>({ loading: false, error: false });
  const [attempt, setAttempt] = useState(0);
  useEffect(() => {
    if (!enabled) return;
    const controller = new AbortController();
    setState(previous => ({ ...previous, loading: true, error: false }));
    async function load() {
      Iif (cache && attempt === 0) return cache;
      const base = (process.env.PUBLIC_URL || "").replace(/\/$/, "");
      const response = await fetch(`${base}/data/places/neighborhoods.json`, {
        signal: controller.signal, ...(attempt > 0 ? { cache: "reload" as const } : {})
      });
      Iif (!response.ok) throw new Error("Could not load Portland neighborhoods");
      return parseNeighborhoodSnapshot(await response.json());
    }
    load().then(snapshot => {
      Eif (!controller.signal.aborted) {
        cache = snapshot;
        setState({ snapshot, loading: false, error: false });
      }
    }).catch(() => {
      Eif (!controller.signal.aborted) setState(previous => ({ ...previous, loading: false, error: true }));
    });
    return () => controller.abort();
  }, [enabled, attempt]);
  return { ...state, retry: () => setAttempt(value => value + 1) };
}