All files / presentation/ui/src/vehicles VehicleTripProgress.tsx

100% Statements 19/19
83.54% Branches 66/79
100% Functions 8/8
100% Lines 17/17

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                                                              40x 40x 40x 40x 40x   40x 41x 40x 40x 40x     110x 110x             43x 55x                           40x           2x   1x                                                          
import { isVehicleTripStale, vehicleTripLegs } from "@overworld/trimet-client";
import type { VehiclePosition, VehicleTripSnapshot, VehicleTripStop, VehicleTripProgressMode } from "@overworld/trimet-client";
import React from "react";
import { Button } from "@wintermuted/ui-react";
import { useTimeDisplay } from "../TimeDisplay";
import "./VehicleTripProgress.scss";
 
export interface VehicleTripProgressState {
  data?: VehicleTripSnapshot;
  receivedAt?: number;
  loading: boolean;
  error?: string;
  retry: () => void;
}
 
export interface VehicleTripProgressProps {
  selection: VehiclePosition;
  vehicle?: VehiclePosition;
  tracking: boolean;
  ended: boolean;
  paused: boolean;
  result: VehicleTripProgressState;
  view: "north-up" | "chase";
  onViewChange: (view: "north-up" | "chase") => void;
  header: React.ReactNode;
  sectionRef?: React.Ref<HTMLElement>;
  timeZone?: string;
}
 
export default function VehicleTripProgress({ selection, vehicle, tracking, ended, paused, result, view,
  onViewChange, header, sectionRef, timeZone }: VehicleTripProgressProps) {
  const { formatTime } = useTimeDisplay();
  const data = result.data;
  const trip = data?.trip;
  const stale = isVehicleTripStale(result.receivedAt, Date.now());
  const mode: VehicleTripProgressMode = trip?.progressAvailable
    ? tracking && !result.error && !stale && !paused ? "live" : "last-known" : "scheduled";
  const legs = trip ? vehicleTripLegs(trip, mode) : [];
  const next = trip?.stops.find(stop => !stop.passed && stop.status !== "canceled");
  const firstUpcoming = next ? Math.max(0, legs.findIndex(leg => leg.to.sequence >= next.sequence)) : legs.length;
  const earlier = legs.slice(0, firstUpcoming);
  const upcoming = legs.slice(firstUpcoming);
 
  function stopTime(stop: VehicleTripStop) {
    const time = stop.estimated ?? stop.scheduled;
    return time ? <>
      <time dateTime={new Date(time).toISOString()}>{formatTime(time, timeZone)}</time>
      <small>{stop.estimated ? mode === "live" ? "Estimated" : "Last estimate" : "Scheduled"}</small>
    </> : <small>Time not reported</small>;
  }
 
  function legList(entries: typeof legs, start: number) {
    return <ol className="transit-follow-legs" start={start}>
      {entries.map(leg => <li key={`${leg.from.sequence}-${leg.to.sequence}`}
        className={leg.current ? "transit-follow-leg-current" : undefined}
        aria-current={leg.current ? "step" : undefined}>
        <strong className="transit-follow-leg-status">{leg.status}</strong>
        <div className="transit-follow-stop-pair">
          <div><span>{leg.from.name}</span><small>Stop {leg.from.id}</small>{stopTime(leg.from)}</div>
          <div><span>{leg.to.name}</span><small>Stop {leg.to.id}</small>{stopTime(leg.to)}</div>
        </div>
        {leg.to.status === "dropOffOnly" && <small>Drop-off only</small>}
        {!["normal", "canceled", "dropOffOnly"].includes(leg.to.status) && <small>Stop status: {leg.to.status}</small>}
      </li>)}
    </ol>;
  }
 
  return <section ref={sectionRef} className="transit-follow-trip" aria-label="Following vehicle trip">
    {header}
    <p>{selection.type === "bus" ? "Bus" : "Train"} {selection.id} ยท Trip {selection.tripId || "not reported"}</p>
    <p>{trip?.destination ? `To ${trip.destination}` : selection.destination}</p>
    <div role="group" aria-label="Following map view" className="transit-follow-view-controls">
      <Button type="button" size="sm" variant={view === "north-up" ? "primary" : "secondary"}
        aria-pressed={view === "north-up"} onClick={() => onViewChange("north-up")}>North-up</Button>
      <Button type="button" size="sm" variant={view === "chase" ? "primary" : "secondary"}
        aria-pressed={view === "chase"} onClick={() => onViewChange("chase")}>Chase view</Button>
    </div>
    {view === "chase" && <p className="transit-trip-note">
      3D view follows reported headings, not continuous GPS.
      {tracking && vehicle?.bearing === undefined && " Heading unavailable; holding the last orientation."}
    </p>}
    <p role="status">{ended ? "This vehicle is now serving another trip. Tracking this trip has ended." :
      !tracking ? "Waiting for a fresh vehicle position. Map following is paused." :
        mode === "scheduled" ? "Stop progress is schedule-based; live stop progress is unavailable." :
          mode === "last-known" ? "Showing the last reported trip progress." :
            next ? `Next stop: ${next.name}` : "All reported stops have been passed."}</p>
    {result.loading && !data && tracking && <p role="status">Loading trip legs...</p>}
    {result.error && <div role="alert">
      <p>{result.error} Any displayed legs are from the last successful update.</p>
      <Button type="button" size="sm" disabled={!tracking || ended} onClick={result.retry}>Retry trip progress</Button>
    </div>}
    {data && !trip && <p role="status">This trip is no longer reported for the selected vehicle block.</p>}
    {data?.partial && <p role="status">Some stop names are unavailable; stop IDs are shown instead.</p>}
    {result.receivedAt && <p className="transit-trip-note">
      Trip progress updated {formatTime(result.receivedAt, timeZone, true)}
    </p>}
    {earlier.length > 0 && <details>
      <summary>{earlier.length} earlier {earlier.length === 1 ? "leg" : "legs"}{mode === "scheduled" ? " (schedule-based)" : ""}</summary>
      {legList(earlier, 1)}
    </details>}
    {legList(upcoming, earlier.length + 1)}
    {trip && legs.length === 0 && <p>No public stop-to-stop legs are available for this trip.</p>}
  </section>;
}