Skip to content

Commit

Permalink
fix: incorrect footer trip status, remove unused getTripStatus helper
Browse files Browse the repository at this point in the history
  • Loading branch information
david-abell committed Apr 10, 2024
1 parent 4c58a7e commit 444ce39
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 129 deletions.
96 changes: 33 additions & 63 deletions src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
formatReadableDelay,
getDelayStatus,
getDelayedTime,
getTripStatus,
isPastArrivalTime,
} from "@/lib/timeHelpers";
import { TripUpdate } from "@/types/realtime";
Expand Down Expand Up @@ -79,48 +78,40 @@ function Footer({

const lastStopTimeUpdate = stopTimeUpdates?.at(-1);

const closestPickupStopUpdate =
const pickupStopUpdate =
(pickupStopSequence &&
stopTimeUpdates?.find(
({ stopId: stopTimeStopId, stopSequence }) =>
stop?.stopId === stopTimeStopId ||
(destination && stopSequence >= pickupStopSequence),
({ stopSequence }) => stopSequence >= pickupStopSequence,
)) ||
lastStopTimeUpdate;

const realtimePickupArrivalTime = getDelayedTime(
pickupDepartureTime,
closestPickupStopUpdate?.arrival?.delay ||
closestPickupStopUpdate?.departure?.delay,
pickupStopUpdate?.arrival?.delay || pickupStopUpdate?.departure?.delay,
);

const pickupDelayStatus = getDelayStatus(closestPickupStopUpdate);
const pickupDelayStatus = getDelayStatus(pickupStopUpdate);

const pickupDelay = formatReadableDelay(
closestPickupStopUpdate?.arrival?.delay ||
closestPickupStopUpdate?.departure?.delay,
pickupStopUpdate?.arrival?.delay || pickupStopUpdate?.departure?.delay,
);

const closestDropOffStopUpdate =
const dropOffStopUpdate =
(dropOffStopSequence &&
stopTimeUpdates?.find(
({ stopId: stopTimeStopId, stopSequence }) =>
destination?.stopId === stopTimeStopId ||
(pickupStopSequence && stopSequence >= dropOffStopSequence),
({ stopSequence }) => stopSequence >= dropOffStopSequence,
)) ||
lastStopTimeUpdate;

const dropOffDelayStatus = getDelayStatus(closestDropOffStopUpdate);
const dropOffDelayStatus = getDelayStatus(dropOffStopUpdate);

const dropOffDelay = formatReadableDelay(
closestDropOffStopUpdate?.arrival?.delay ||
closestDropOffStopUpdate?.departure?.delay,
dropOffStopUpdate?.arrival?.delay || dropOffStopUpdate?.departure?.delay,
);

const realtimeDropOffArrivalTime = getDelayedTime(
dropOffArrivalTime,
closestDropOffStopUpdate?.arrival?.delay ||
closestDropOffStopUpdate?.departure?.delay,
dropOffStopUpdate?.arrival?.delay || dropOffStopUpdate?.departure?.delay,
);

const isPastPickup =
Expand All @@ -131,14 +122,6 @@ function Footer({
!!dropOffArrivalTime &&
isPastArrivalTime(realtimeDropOffArrivalTime ?? dropOffArrivalTime);

const tripStatus = getTripStatus(
trip,
stopTimes,
stopTimeUpdates,
stop?.stopId,
destination?.stopId,
);

return (
<div className="absolute bottom-0 z-[1000] mx-auto min-h-[6rem] w-full overflow-x-auto p-4 lg:max-w-7xl lg:px-10">
<div className="rounded-lg bg-background/90 p-4">
Expand All @@ -148,53 +131,40 @@ function Footer({
{
<div className="flex w-full flex-row content-center justify-between gap-2 md:gap-4 overflow-hidden px-2 text-left font-normal">
<p>
{!!route ? (
{
<>
<span>Route </span>
<b>{route.routeShortName ?? ""}</b>
<b>{route?.routeShortName ?? ""}</b>
<span className="max-lg:hidden">
{" "}
&#9830; {route?.routeLongName ?? ""}
&#9830; {route?.routeLongName ?? "No route selected"}
</span>
</>
) : (
"No route selected"
)}
}

{!!trip && (
<> &#9830; heading towards {trip.tripHeadsign}</>
)}
</p>

{!!tripStatus && (
<p
className={
tripStatus === "early"
? "text-green-700 dark:text-green-500"
: tripStatus === "delayed"
? "text-red-700 dark:text-red-500"
: ""
}
>
{tripStatus === "completed" ? (
"Completed"
) : isPastPickup ? (
<>
<span className="whitespace-nowrap">
{dropOffDelay ?? ""}
</span>{" "}
{dropOffDelayStatus || tripStatus}
</>
) : (
<>
<span className="whitespace-nowrap">
{pickupDelay ?? ""}
</span>{" "}
{pickupDelayStatus || tripStatus}
</>
)}
</p>
)}
<p
className={
dropOffDelayStatus === "early"
? "text-green-700 dark:text-green-500"
: dropOffDelayStatus === "late"
? "text-red-700 dark:text-red-500"
: ""
}
>
<span className="whitespace-nowrap">
{dropOffDelay ?? ""}
</span>{" "}
{isPastDropOff
? "Completed"
: isPastPickup
? dropOffDelayStatus
: pickupDelayStatus}
</p>
</div>
}
</AccordionTrigger>
Expand Down Expand Up @@ -229,7 +199,7 @@ function Footer({
<span>
{!!realtimePickupArrivalTime && (
<>
{isPastPickup && "Departed @ "}
{isPastPickup && "Arrived @ "}
<time dateTime={realtimePickupArrivalTime}>
{realtimePickupArrivalTime}
</time>
Expand Down
66 changes: 0 additions & 66 deletions src/lib/timeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,72 +207,6 @@ export function getDelayStatus(
}
}

export const tripStatus = {
canceled: "canceled",
future: "scheduled",
early: "early",
delayed: "delayed",
"on-time": "on time",
finished: "completed",
not: "not in service",
} as const;

// returns empty string on invalid input
export function getTripStatus(
trip: Trip | undefined,
stopTimes: StopTime[] | undefined,
stopTimeUpdate: StopTimeUpdate[] | undefined,
start: Stop["stopId"] | undefined,
end: Stop["stopId"] | undefined,
) {
if (!trip || !stopTimes) return "";
if (!stopTimes.length) return tripStatus.not;

if (stopTimeUpdate?.at(-1)?.scheduleRelationship === "CANCELED") {
return tripStatus.canceled;
}

const firstStop = start
? stopTimes.find((stop) => stop.stopId === start) ?? stopTimes[0]
: stopTimes[0];

const lastStop =
stopTimes.find((stop) => stop.stopId === end) ?? stopTimes.at(-1);

const startTime = firstStop.departureTime ?? firstStop.arrivalTime;

const finishTime = lastStop?.arrivalTime;

const realtime = stopTimeUpdate?.at(-1);
const delay = (realtime?.arrival?.delay || realtime?.departure?.delay) ?? 0;

const adjustedStartTime = getDelayedTime(startTime, delay, true);
const adjustedFinishTime = getDelayedTime(finishTime, delay, true);

if (!adjustedStartTime || !adjustedFinishTime) {
return "";
}

// if trip has finished
if (isPastArrivalTime(adjustedFinishTime)) {
return tripStatus.finished;
}
// if delayed
if (delay > 0) {
return tripStatus.delayed;
}
// if early
if (delay < 0) {
return tripStatus.early;
}
// if trip hasn't started yet and no delay
if (!isPastArrivalTime(adjustedStartTime)) {
return tripStatus.future;
}

return tripStatus["on-time"];
}

export function getDifferenceInSeconds(
stopTimeOne: string,
stopTimeTwo?: string,
Expand Down

0 comments on commit 444ce39

Please sign in to comment.