All files / applications/go-by-transit/src/view/tripPlanner useTripPlannerState.ts

91.91% Statements 250/272
85.23% Branches 179/210
91.17% Functions 31/34
93.46% Lines 229/245

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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396                              37x 37x   2x         346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 20758x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x 346x   346x 385x 346x   346x 53x 53x 53x 53x 53x 53x 53x     346x 94x 45x     49x 49x 49x 49x     346x 99x 99x 99x               99x 44x 44x     55x 99x 99x   99x   99x 2x 2x   53x 21x 21x   32x 32x 32x 2x 2x   30x 1x       1x     29x 29x 29x           27x     27x 1x           1x   26x 4x 4x   22x 22x 22x 22x 22x 22x 22x 22x 16x 16x 16x   2x         2x 2x             99x       346x 92x 92x 1x 1x       28x 28x       13x 13x 13x 13x 13x 13x 13x 13x       3x 3x 3x 3x 3x 3x 2x 1x   1x 1x 1x 2x 2x 2x 1x 1x       1x 1x 1x         1x 1x     3x         10x 10x 10x 10x 10x       10x       10x       10x 10x   10x 10x   10x 2x   8x               5x 3x 3x 3x   2x 2x 2x   5x       1x 1x 1x 1x 1x 1x 1x     1x 1x             346x 346x 346x 346x 346x 346x 346x     40x 40x     346x 73x 9x 9x   73x 6x 6x       346x 74x 27x     346x 58x 58x 2x 2x 56x 2x 2x 2x         2x 2x 2x 2x     346x 62x 62x 62x     346x                                      
import { endpointLabelForRequest, resolveStopReference, sameEndpoint, endpointParams, locationEndpoint, sameEndpointMessage } from "@overworld/journeys";
import type { EndpointField } from "@overworld/journeys";
import { useStopCatalog } from "@overworld/trimet-client/react";
import distance from "@turf/distance";
import { type FormEvent, useEffect, useLayoutEffect, useRef, useState } from "react";
import { useSearchParams } from "react-router-dom";
import geoLocateCurrentPosition from "../../api/geolocation/geoLocateCurrentPosition";
import { readRecentJourneys, recordRecentJourney, type RecentJourney } from "../../api/localstorage/recentJourneys";
import { plannerTransitColor } from "../../api/trimet/plannerRouteColors";
import { getStopCatalog, type StopCatalogStop } from "../../api/trimet/stopCatalog";
import { planTrip, parsePlannerCoordinates, type TripPlannerItinerary, type TripPlannerLeg } from "../../api/trimet/tripplanner";
import { PLACE_CATEGORIES, placeCategoryForId, usePlaceCatalog, type InterestPlace } from "@overworld/places/catalog";
import { EMPTY_STOPS, coordinateHelp, type PlannedSearch, type TripState, type TripPlannerProps } from "./TripPlannerModel";
 
function loadRecentTrips() {
  try {
    return { journeys: readRecentJourneys(), unavailable: false };
  } catch {
    return { journeys: [], unavailable: true };
  }
}
 
export default function useTripPlannerState({ inSidebar = false, directionsFooter }: Pick<TripPlannerProps, "inSidebar" | "directionsFooter">) {
  const [searchParams, setSearchParams] = useSearchParams();
  const fromParam = searchParams.get("from") || "";
  const toParam = searchParams.get("to") || "";
  const fromCoord = searchParams.get("fromCoord");
  const toCoord = searchParams.get("toCoord");
  const fromPlace = searchParams.get("fromPlace");
  const toPlace = searchParams.get("toPlace");
  const fromPlaceId = searchParams.get("fromPlaceId");
  const toPlaceId = searchParams.get("toPlaceId");
  const confirmPlace = searchParams.get("confirmPlace") === "1";
  const placeCatalog = usePlaceCatalog(Boolean(fromPlaceId || toPlaceId));
  const fromNamedPlace = placeCatalog.places.find(place => place.id === fromPlaceId && place.coordinates.join(",") === fromCoord);
  const toNamedPlace = placeCatalog.places.find(place => place.id === toPlaceId && place.coordinates.join(",") === toCoord);
  const requiredPlaceCategories = PLACE_CATEGORIES.filter(category =>
    [fromPlaceId, toPlaceId].some(id => placeCategoryForId(id) === category));
  const placesPending = requiredPlaceCategories.some(category => !placeCatalog.sources[category].snapshot && !placeCatalog.sources[category].error);
  const placesFailed = requiredPlaceCategories.some(category => placeCatalog.sources[category].error);
  const [fromInterestPlace, setFromInterestPlace] = useState<InterestPlace>();
  const [toInterestPlace, setToInterestPlace] = useState<InterestPlace>();
  const fromLinkedLocation = fromCoord ?? fromPlace;
  const toLinkedLocation = toCoord ?? toPlace;
  const catalogState = useStopCatalog(getStopCatalog);
  const [fromStop, setFromStop] = useState<StopCatalogStop>();
  const [toStop, setToStop] = useState<StopCatalogStop>();
  const [fromLocation, setFromLocation] = useState<string>();
  const [toLocation, setToLocation] = useState<string>();
  const [locating, setLocating] = useState<EndpointField>();
  const locationRequest = useRef(0);
  const pendingEndpointFocus = useRef<EndpointField>(undefined);
  const [selectedItinerary, setSelectedItinerary] = useState(-1);
  const [editingTrip, setEditingTrip] = useState(false);
  const [hoveredTrip, setHoveredTrip] = useState<number>();
  const [focusedTrip, setFocusedTrip] = useState<number>();
  const [journeyItinerary, setJourneyItinerary] = useState<TripPlannerItinerary>();
  const planningContent = useRef<HTMLElement>(null);
  const journeyHeading = useRef<HTMLHeadingElement>(null);
  const planningScrollTop = useRef<number>(undefined);
  const editTripButton = useRef<HTMLButtonElement>(null);
  const currentTripOption = useRef<HTMLButtonElement>(null);
  const [hoveredLeg, setHoveredLeg] = useState<number>();
  const [focusedLeg, setFocusedLeg] = useState<number>();
  const [mapLeg, setMapLeg] = useState<number>();
  const [tripRetry, setTripRetry] = useState(0);
  const [tripState, setTripState] = useState<TripState>({ status: "idle" });
  const [plannedSearch, setPlannedSearch] = useState<PlannedSearch>();
  const [editingStops, setEditingStops] = useState(false);
  const editStopsButton = useRef<HTMLButtonElement>(null);
  const searchFields = useRef<HTMLFieldSetElement>(null);
  const focusStopSummary = useRef(false);
  const focusBoardingStop = useRef(false);
  const [validationError, setValidationError] = useState("");
  const [historyUnavailable, setHistoryUnavailable] = useState(false);
  const [recentTrips, setRecentTrips] = useState(() => inSidebar ? loadRecentTrips() : undefined);
 
  const catalog = catalogState.catalog;
  const transitColor = (leg: TripPlannerLeg) => plannerTransitColor(leg, catalog?.plannerRouteColors);
  const lookupCatalog = fromLinkedLocation === null || toLinkedLocation === null ? catalog : undefined;
 
  useEffect(() => {
    setFromLocation(fromLinkedLocation ?? undefined);
    setToLocation(toLinkedLocation ?? undefined);
    setFromInterestPlace(fromNamedPlace);
    setToInterestPlace(toNamedPlace);
    setValidationError("");
    setLocating(undefined);
    return () => { locationRequest.current += 1; };
  }, [fromParam, toParam, fromLinkedLocation, toLinkedLocation, fromNamedPlace, toNamedPlace]);
 
  useEffect(() => {
    if (!catalog) {
      return;
    }
 
    const from = resolveStopReference(catalog.stops, fromParam);
    const to = resolveStopReference(catalog.stops, toParam);
    setFromStop(from.stop);
    setToStop(to.stop);
  }, [catalog, fromParam, toParam]);
 
  useEffect(() => {
    setHistoryUnavailable(false);
    setEditingStops(true);
    Iif ((fromPlaceId && !fromNamedPlace) || (toPlaceId && !toNamedPlace)) {
      if (placesPending) {
        setTripState({ status: "loading" });
      } else {
        setTripState({ status: "error", message: "The linked place could not be verified against its location snapshot. Retry loading places or enter your destination manually." });
      }
      return;
    }
    if (!(fromLinkedLocation ?? fromParam) || !(toLinkedLocation ?? toParam)) {
      setTripState({ status: "idle" });
      return;
    }
 
    const from = resolveStopReference(lookupCatalog?.stops ?? EMPTY_STOPS, fromParam).stop;
    const to = resolveStopReference(lookupCatalog?.stops ?? EMPTY_STOPS, toParam).stop;
    const origin = fromLinkedLocation !== null ? locationEndpoint(fromLinkedLocation, fromCoord !== null) :
      from && { kind: "stop" as const, stop: from };
    const destination = toLinkedLocation !== null ? locationEndpoint(toLinkedLocation, toCoord !== null) :
      to && { kind: "stop" as const, stop: to };
    if ((fromLinkedLocation !== null && !origin) || (toLinkedLocation !== null && !destination)) {
      setTripState({ status: "error", message: `The linked location is invalid. ${coordinateHelp}` });
      return;
    }
    if (!origin || !destination) {
      setTripState({ status: "idle" });
      return;
    }
    if (origin.kind !== "stop") origin.interestPlace = fromNamedPlace;
    if (destination.kind !== "stop") destination.interestPlace = toNamedPlace;
    if (confirmPlace && (fromNamedPlace || toNamedPlace)) {
      setTripState({ status: "idle" });
      return;
    }
    if (sameEndpoint(origin, destination)) {
      setTripState({
        status: "error",
        message: sameEndpointMessage(origin, destination),
      });
      return;
    }
 
    const controller = new AbortController();
    setTripState({ status: "loading" });
    planTrip({
      ...(origin.kind === "coord" ? { fromCoord: origin.value, fromPlace: origin.value } : { fromPlace: endpointLabelForRequest(origin) }),
      ...(destination.kind === "coord" ? { toCoord: destination.value, toPlace: destination.value } : { toPlace: endpointLabelForRequest(destination) }),
      signal: controller.signal,
    })
      .then((response) => {
        Iif (controller.signal.aborted) {
          return;
        }
        if (!response.success) {
          setTripState({
            status: "error",
            message: response.errorMessage?.startsWith("Uncertain Location")
              ? `TriMet could not identify the location. ${response.errorMessage}. Try a more specific name, a street address without city or state, or coordinates.`
              : response.errorMessage || "TriMet could not find a trip for these locations.",
          });
          return;
        }
        if (response.itineraries.length === 0) {
          setTripState({ status: "empty" });
          return;
        }
        setPlannedSearch({ from: origin, to: destination, response });
        setTripState({ status: "ready" });
        setSelectedItinerary(-1);
        setEditingTrip(false);
        clearTripPreview();
        setJourneyItinerary(undefined);
        setEditingStops(false);
        if (origin.kind === "stop" && destination.kind === "stop") {
          try {
            recordRecentJourney(origin.stop, destination.stop);
            if (inSidebar) setRecentTrips(loadRecentTrips());
          } catch {
            setHistoryUnavailable(true);
          }
        }
      })
      .catch(() => {
        Eif (!controller.signal.aborted) {
          setTripState({
            status: "error",
            message: "Could not load trip options from TriMet.",
          });
        }
      });
 
    return () => controller.abort();
  }, [lookupCatalog, fromParam, toParam, fromLinkedLocation, toLinkedLocation, fromCoord, toCoord, tripRetry, inSidebar,
    fromPlaceId, toPlaceId, fromNamedPlace, toNamedPlace, confirmPlace, placesPending, placesFailed]);
 
  useLayoutEffect(() => {
    const field = pendingEndpointFocus.current;
    if (!field) return;
    pendingEndpointFocus.current = undefined;
    searchFields.current?.querySelector<HTMLInputElement>(`#planner-${field}`)?.focus();
  }, [fromLocation, toLocation, locating]);
 
  function cancelLocationLookup() {
    locationRequest.current += 1;
    setLocating(undefined);
  }
 
  function swapEndpoints() {
    cancelLocationLookup();
    setFromStop(toStop);
    setToStop(fromStop);
    setFromLocation(toLocation);
    setToLocation(fromLocation);
    setFromInterestPlace(toInterestPlace);
    setToInterestPlace(fromInterestPlace);
    setValidationError("");
  }
 
  async function useNearestStop(field: EndpointField) {
    Iif (!catalog) return;
    const request = ++locationRequest.current;
    setLocating(field);
    setValidationError("");
    try {
      const { coords } = await geoLocateCurrentPosition();
      if (request !== locationRequest.current) return;
      const point = parsePlannerCoordinates(`${coords.longitude},${coords.latitude}`);
      let nearest: StopCatalogStop | undefined;
      let bestDistance = 1;
      Eif (point) {
        for (const stop of catalog.stops) {
          Iif (!Number.isFinite(stop.lng) || !Number.isFinite(stop.lat)) continue;
          const miles = distance(point, [stop.lng, stop.lat], { units: "miles" });
          if (miles < bestDistance) {
            nearest = stop;
            bestDistance = miles;
          }
        }
      }
      if (nearest) {
        pendingEndpointFocus.current = field;
        chooseStop(nearest, field);
      } else E{
        setValidationError("No TriMet stop was found within one mile. Enter a place, address, or coordinates instead.");
      }
    } catch {
      Eif (request === locationRequest.current) {
        setValidationError("Could not use your location. Check browser permissions or enter a location manually.");
      }
    } finally {
      if (request === locationRequest.current) setLocating(undefined);
    }
  }
 
  function submit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setValidationError("");
    const from = fromLocation !== undefined ? locationEndpoint(fromLocation) : fromStop && { kind: "stop" as const, stop: fromStop };
    const to = toLocation !== undefined ? locationEndpoint(toLocation) : toStop && { kind: "stop" as const, stop: toStop };
    Iif ((fromLocation?.trim() && !from) || (toLocation?.trim() && !to)) {
      setValidationError(`Check the ${!from ? "origin" : "destination"} coordinates. ${coordinateHelp}`);
      return;
    }
    Iif (!from || !to) {
      setValidationError("Choose an origin and a destination.");
      return;
    }
    Iif (sameEndpoint(from, to)) {
      setValidationError(sameEndpointMessage(from, to));
      return;
    }
    if (from.kind !== "stop" && fromInterestPlace?.coordinates.join(",") === from.value) from.interestPlace = fromInterestPlace;
    if (to.kind !== "stop" && toInterestPlace?.coordinates.join(",") === to.value) to.interestPlace = toInterestPlace;
 
    focusStopSummary.current = true;
    const params = { ...endpointParams(from, "from"), ...endpointParams(to, "to"),
      ...(searchParams.get("placesView") ? { placesView: searchParams.get("placesView") } : {}) };
    if (new URLSearchParams(params).toString() === searchParams.toString()) {
      setTripRetry(value => value + 1);
    } else {
      setSearchParams(params);
    }
  }
 
  function chooseStop(
    stop: StopCatalogStop | null,
    field: EndpointField,
  ) {
    if (field === "from") {
      setFromStop(stop ?? undefined);
      setFromLocation(undefined);
      setFromInterestPlace(undefined);
    } else {
      setToStop(stop ?? undefined);
      setToLocation(undefined);
      setToInterestPlace(undefined);
    }
    setValidationError("");
  }
 
  function chooseRecentTrip(journey: RecentJourney | null) {
    Iif (!journey) return;
    focusStopSummary.current = true;
    const { from, to } = journey;
    const params = { from: String(from.locid), to: String(to.locid) };
    Eif (params.from !== fromParam || params.to !== toParam || fromLinkedLocation !== null || toLinkedLocation !== null) {
      setSearchParams(params);
      return;
    }
    const origin = catalog?.stops.find(stop => stop.locid === from.locid);
    const destination = catalog?.stops.find(stop => stop.locid === to.locid);
    Iif (origin && destination) {
      chooseStop(origin, "from");
      chooseStop(destination, "to");
    }
    setTripRetry(value => value + 1);
  }
 
  const response = plannedSearch?.response;
  const stopSummary = editingStops ? undefined : plannedSearch;
  const itineraries = response?.itineraries || [];
  const activeItinerary = itineraries[selectedItinerary];
  const inJourney = Boolean(journeyItinerary && journeyItinerary === activeItinerary);
  const previewTrip = !inJourney && (!activeItinerary || editingTrip) ? hoveredTrip ?? focusedTrip : undefined;
  const highlightedLeg = hoveredLeg ?? mapLeg ?? focusedLeg;
 
  function clearTripPreview() {
    setHoveredTrip(undefined);
    setFocusedTrip(undefined);
  }
 
  useEffect(() => {
    if (stopSummary && focusStopSummary.current) {
      editStopsButton.current?.focus();
      focusStopSummary.current = false;
    }
    if (!stopSummary && focusBoardingStop.current) {
      searchFields.current?.querySelector<HTMLInputElement>("#planner-from")?.focus();
      focusBoardingStop.current = false;
    }
  }, [stopSummary]);
 
  useEffect(() => {
    if (selectedItinerary < 0) return;
    (editingTrip ? currentTripOption : editTripButton).current?.focus();
  }, [selectedItinerary, editingTrip]);
 
  useLayoutEffect(() => {
    const scrollRegion = planningContent.current?.closest(".transit-nearby-results");
    if (inJourney) {
      Eif (scrollRegion) scrollRegion.scrollTop = 0;
      journeyHeading.current?.focus({ preventScroll: true });
    } else if (planningScrollTop.current !== undefined) {
      Eif (scrollRegion) scrollRegion.scrollTop = planningScrollTop.current;
      planningScrollTop.current = undefined;
      directionsFooter?.querySelector("button")?.focus({ preventScroll: true });
    }
  }, [inJourney, directionsFooter]);
 
  function openJourney() {
    planningScrollTop.current = planningContent.current?.closest(".transit-nearby-results")?.scrollTop ?? 0;
    setHoveredLeg(undefined);
    setFocusedLeg(undefined);
    setJourneyItinerary(activeItinerary);
  }
 
  useEffect(() => {
    setHoveredLeg(undefined);
    setFocusedLeg(undefined);
    setMapLeg(undefined);
  }, [activeItinerary, selectedItinerary]);
 
  return {
    searchParams, fromParam, toParam, fromPlaceId, toPlaceId, confirmPlace,
    fromNamedPlace, toNamedPlace, placeCatalog, fromLinkedLocation, toLinkedLocation,
    fromInterestPlace, setFromInterestPlace, toInterestPlace, setToInterestPlace,
    fromStop, toStop, fromLocation, setFromLocation, toLocation, setToLocation,
    locating, pendingEndpointFocus, catalogState, catalog, transitColor,
    selectedItinerary, setSelectedItinerary, editingTrip, setEditingTrip,
    setHoveredTrip, setFocusedTrip, journeyItinerary, setJourneyItinerary,
    planningContent, journeyHeading, editTripButton, currentTripOption,
    setHoveredLeg, setFocusedLeg, setMapLeg, setTripRetry, tripState,
    plannedSearch, editingStops, setEditingStops, editStopsButton, searchFields,
    focusBoardingStop, validationError, setValidationError, historyUnavailable,
    recentTrips, cancelLocationLookup, swapEndpoints, useNearestStop, submit,
    chooseStop, chooseRecentTrip, response, stopSummary, itineraries,
    activeItinerary, inJourney, previewTrip, highlightedLeg, clearTripPreview, openJourney,
  };
}
 
export type TripPlannerState = ReturnType<typeof useTripPlannerState>;