Port Angular's closest-flight auto-select + scroll-into-view behavior
Angular's route/departure/arrival search result list picks a 'current flight' on load and auto-expands + scrolls it into view — the flight whose dep/arr time is closest to 'now' for today's searches, or the first/last flight when the search is for a future/past day. React was always rendering the list scrolled to the top, so on today's route search the user sees flights from 00:30 onwards instead of landing on whatever is departing right now. - Add features/online-board/closestFlight.ts with a React-flavored port of find-closest-flight.ts (plus a today-guard that reuses the same 'yyyymmdd' shape the URL parser produces). - FlightList takes an optional initialCurrentFlightId, attaches a ref to each card, and scrollIntoView's it on mount / list change. - FlightCard takes an initialExpanded prop and seeds its useState so the selected flight lands expanded, matching the Angular 'expanded: true' assignment after setCurrentFlight. - OnlineBoardSearchPage computes the id via findClosestFlightId using the current params (type + date) and forwards it to FlightList.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* Ported from Angular's find-closest-flight utility. Picks the flight
|
||||
* whose departure (or arrival, for arrival-view) time is closest to
|
||||
* "now" on today's searches, or the first/last flight for future/past
|
||||
* days. The result gets auto-expanded and scrolled into view on the
|
||||
* search results card, matching Angular behavior.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
import type { ISimpleFlight, IFlightLeg } from "./types.js";
|
||||
|
||||
function firstLeg(flight: ISimpleFlight): IFlightLeg {
|
||||
return flight.routeType === "Direct" ? flight.leg : flight.legs[0]!;
|
||||
}
|
||||
|
||||
function lastLeg(flight: ISimpleFlight): IFlightLeg {
|
||||
return flight.routeType === "Direct"
|
||||
? flight.leg
|
||||
: flight.legs[flight.legs.length - 1]!;
|
||||
}
|
||||
|
||||
function pickTime(flight: ISimpleFlight, isArrival: boolean): string | null {
|
||||
const leg = isArrival ? lastLeg(flight) : firstLeg(flight);
|
||||
const times = isArrival ? leg.arrival.times : leg.departure.times;
|
||||
const dep = (times as { scheduledDeparture?: { local?: string } }).scheduledDeparture;
|
||||
const arr = (times as { scheduledArrival?: { local?: string } }).scheduledArrival;
|
||||
const actualOff = (times as { actualBlockOff?: { local?: string } }).actualBlockOff;
|
||||
const actualOn = (times as { actualBlockOn?: { local?: string } }).actualBlockOn;
|
||||
const latest =
|
||||
(times as { latestDeparture?: { local?: string }; latestArrival?: { local?: string } })
|
||||
[isArrival ? "latestArrival" : "latestDeparture"];
|
||||
if (isArrival) return actualOn?.local ?? latest?.local ?? arr?.local ?? null;
|
||||
return actualOff?.local ?? latest?.local ?? dep?.local ?? null;
|
||||
}
|
||||
|
||||
function parseTimeMs(iso: string | null): number | null {
|
||||
if (!iso) return null;
|
||||
const ms = Date.parse(iso);
|
||||
return Number.isNaN(ms) ? null : ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` when `yyyymmdd` (e.g. '20260419') refers to today's
|
||||
* calendar day in the browser's local time.
|
||||
*/
|
||||
function isTodayYyyymmdd(yyyymmdd: string, now: Date = new Date()): boolean {
|
||||
if (yyyymmdd.length !== 8) return false;
|
||||
const todayYyyymmdd =
|
||||
String(now.getFullYear()) +
|
||||
String(now.getMonth() + 1).padStart(2, "0") +
|
||||
String(now.getDate()).padStart(2, "0");
|
||||
return yyyymmdd === todayYyyymmdd;
|
||||
}
|
||||
|
||||
export interface ClosestOptions {
|
||||
/** 'yyyyMMdd' — the search date */
|
||||
searchDate: string;
|
||||
/** `true` when the list is an arrival view (use arrival times) */
|
||||
isArrival: boolean;
|
||||
/** Override current time for deterministic tests. */
|
||||
now?: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns the id of the flight whose time is closest to `now`, or the
|
||||
* boundary flight when the search is not for today. Returns `null`
|
||||
* when the list is empty.
|
||||
*/
|
||||
export function findClosestFlightId(
|
||||
flights: ISimpleFlight[],
|
||||
opts: ClosestOptions,
|
||||
): string | null {
|
||||
if (!flights.length) return null;
|
||||
const now = opts.now ?? new Date();
|
||||
const nowMs = now.getTime();
|
||||
|
||||
if (!isTodayYyyymmdd(opts.searchDate, now)) {
|
||||
// For future days, show the first flight; for past days, the last.
|
||||
const firstMs = parseTimeMs(pickTime(flights[0]!, opts.isArrival));
|
||||
if (firstMs !== null && nowMs < firstMs) return flights[0]!.id;
|
||||
return flights[flights.length - 1]!.id;
|
||||
}
|
||||
|
||||
let best: string | null = flights[0]?.id ?? null;
|
||||
let bestAbsDiff = Number.POSITIVE_INFINITY;
|
||||
for (const f of flights) {
|
||||
const ms = parseTimeMs(pickTime(f, opts.isArrival));
|
||||
if (ms === null) continue;
|
||||
const abs = Math.abs(ms - nowMs);
|
||||
if (abs < bestAbsDiff) {
|
||||
bestAbsDiff = abs;
|
||||
best = f.id;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import { useCallback, useEffect } from "react";
|
||||
import { useNavigate, useParams } from "@modern-js/runtime/router";
|
||||
import { useTranslation } from "@/i18n/provider.js";
|
||||
import { FlightList } from "@/ui/flights/FlightList.js";
|
||||
import { findClosestFlightId } from "../closestFlight.js";
|
||||
import { PageLayout } from "@/ui/layout/PageLayout.js";
|
||||
import { PageTabs } from "@/ui/layout/PageTabs.js";
|
||||
import { SearchHistory } from "@/ui/layout/SearchHistory.js";
|
||||
@@ -331,6 +332,18 @@ export const OnlineBoardSearchPage: FC<OnlineBoardSearchPageProps> = ({
|
||||
// Use live flights when connected, otherwise fetched flights
|
||||
const displayFlights = connectionStatus === "live" ? liveFlights : flights;
|
||||
|
||||
// Port of Angular's findClosestFlight — on today's search, picks the
|
||||
// flight with the smallest abs time-diff from 'now' (expands + scrolls
|
||||
// it on mount). For future/past days, picks the first/last.
|
||||
const initialCurrentFlightId = (() => {
|
||||
if (!displayFlights.length || !params.date) return null;
|
||||
const isArrival = params.type === "arrival";
|
||||
return findClosestFlightId(displayFlights, {
|
||||
searchDate: params.date,
|
||||
isArrival,
|
||||
});
|
||||
})();
|
||||
|
||||
// JSON-LD for search results (rendered once we have flights)
|
||||
const searchDescription = `Online board ${params.type} search results`;
|
||||
const jsonLd = displayFlights.length > 0
|
||||
@@ -442,6 +455,7 @@ export const OnlineBoardSearchPage: FC<OnlineBoardSearchPageProps> = ({
|
||||
flights={displayFlights}
|
||||
loading={loading}
|
||||
onFlightClick={handleFlightClick}
|
||||
initialCurrentFlightId={initialCurrentFlightId}
|
||||
/>
|
||||
</section>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user