Fix flights map transfer toggle fallback

This commit is contained in:
2026-05-21 19:07:13 +03:00
parent 99562c2218
commit 19dbdc5127
4 changed files with 167 additions and 6 deletions
@@ -10,6 +10,7 @@ import { transformDictionaries } from "@/shared/dictionaries/index.js";
import type { IDictionaries, IRawDictionaries } from "@/shared/dictionaries/index.js";
import type {
FlightsMapCalendarParams,
IFlightsMapFilterState,
FlightsMapSearchParams,
} from "../types.js";
import {
@@ -383,6 +384,7 @@ describe("TZ §4.1.4 Table 7 breadcrumbs — Flight-Map pages (rows 1-3)", () =>
describe("FlightsMapStartPage — C.4 integration", () => {
beforeEach(() => {
lastMapCanvasProps = null;
lastMapFilterProps = null;
searchCalls.length = 0;
dictState.dictionaries = buildDictionaries({
regions: [],
@@ -513,6 +515,33 @@ describe("FlightsMapStartPage — C.4 integration", () => {
const last = [...searchCalls].reverse().find((p) => p?.departure && p?.arrival);
expect(last?.connections).toBe(1);
});
it("keeps transfer-only toggle off after the user disables auto-fallback", () => {
searchState.routes = [];
render(<FlightsMapStartPage />);
act(() => {
(lastMapCanvasProps!["onMarkerClick"] as (id: string) => void)("MOW");
});
act(() => {
(lastMapCanvasProps!["onMarkerClick"] as (id: string) => void)("LED");
});
const autoFallbackValue = lastMapFilterProps!["value"] as IFlightsMapFilterState;
expect(autoFallbackValue.connections).toBe(true);
act(() => {
(lastMapFilterProps!["onChange"] as (state: IFlightsMapFilterState) => void)({
...autoFallbackValue,
connections: false,
});
});
const userValue = lastMapFilterProps!["value"] as IFlightsMapFilterState;
expect(userValue.connections).toBe(false);
const last = [...searchCalls].reverse().find((p) => p?.departure && p?.arrival);
expect(last?.connections).toBe(0);
});
});
// ---------------------------------------------------------------------------
@@ -161,6 +161,8 @@ export const FlightsMapStartPage: FC<FlightsMapStartPageProps> = ({
const [effectiveConnections, setEffectiveConnections] = useState<0 | 1>(
filterState.connections ? 1 : 0,
);
const [connectionsFallbackSuppressed, setConnectionsFallbackSuppressed] =
useState(false);
const persistFilterState = useCallback((newState: IFlightsMapFilterState) => {
setFilterState(newState);
@@ -225,6 +227,7 @@ export const FlightsMapStartPage: FC<FlightsMapStartPageProps> = ({
useEffect(() => {
if (loading || error) return;
if (effectiveConnections !== 0) return;
if (connectionsFallbackSuppressed) return;
if (!filterState.departure || !filterState.arrival) return;
if (routes.length > 0) return;
setEffectiveConnections(1);
@@ -232,6 +235,7 @@ export const FlightsMapStartPage: FC<FlightsMapStartPageProps> = ({
loading,
error,
effectiveConnections,
connectionsFallbackSuppressed,
filterState.departure,
filterState.arrival,
routes,
@@ -239,14 +243,43 @@ export const FlightsMapStartPage: FC<FlightsMapStartPageProps> = ({
// Reflect fallback in the UI toggle once.
useEffect(() => {
if (filterState.arrival && effectiveConnections === 1 && !filterState.connections) {
if (
filterState.arrival &&
effectiveConnections === 1 &&
!filterState.connections &&
!connectionsFallbackSuppressed
) {
setFilterState((prev) => ({ ...prev, connections: true }));
}
}, [effectiveConnections, filterState.arrival, filterState.connections]);
}, [
effectiveConnections,
filterState.arrival,
filterState.connections,
connectionsFallbackSuppressed,
]);
const handleFilterChange = useCallback((newState: IFlightsMapFilterState) => {
const sameRoute =
newState.departure === filterState.departure &&
newState.arrival === filterState.arrival;
const turnedConnectionsOff =
sameRoute && filterState.connections && !newState.connections;
setConnectionsFallbackSuppressed(
turnedConnectionsOff
? true
: sameRoute && !newState.connections
? connectionsFallbackSuppressed
: false,
);
persistFilterState(newState);
}, [persistFilterState]);
}, [
connectionsFallbackSuppressed,
filterState.arrival,
filterState.connections,
filterState.departure,
persistFilterState,
]);
const handleMarkerClick = useCallback(
(markerId: string) => {