Files
TREK/client/src/pages/tripPlanner/useOneCornerCard.ts
T
Maurice f745ca13c9 feat(tours): suggest round hikes of about a wished length from a stop (RTE-03)
A hike can now be planned as a round out of one of its stops without
building the loop point by point. "Round hike from here" in a stop's menu,
or the round button in the head of the waypoints for the start, opens a
card over the planner map: the start (any stop of the tour, picked from a
list), the wished length in the traveller's unit, the window of a fifth
either way it allows, and the tour's hiking limit, which every question
to the router keeps to together with the hike's preferences.

TREK looks for up to three different rounds itself on the hiking router it
already uses. It steers loops round circles in several directions from the
start, asks a loop that missed the length once more at a corrected size,
and learns from each answer how much longer the paths are than straight
lines. A loop that walks most of its ground twice (a way there and back), a
spot too far from any path, and a round that repeats one found are left
out, and nothing outside the window is offered. The rounds show up in
their colours on the map and in 3D as they are found, the map frames the
area they can lie in, and each lists its length, how far it lies off the
wish, climb, descent and walking time once the heights are in. Fewer
rounds than three, none, or a router that does not answer are said so.

Showing, choosing and closing change nothing. "Take this round" puts the
start, the steering spots as shaping points and the way back in place of
the plan as one step of the history, with a "Round taken" note and its
undo; a tour that held more than its start asks first. The round is then
an ordinary tour to edit and save. A start moved or a limit changed while
rounds are shown lets them go and the card says why.

The card shares its frame, buttons and rows with the comparison of a
section's ways, and the map corner still holds one card at a time.
2026-10-20 15:19:03 +02:00

66 lines
2.3 KiB
TypeScript

import { useLayoutEffect, useRef } from 'react';
import { useSelectedPoi } from '../../components/Tours/poi/useTourPoiSearch';
import { selectPaddleObstacle, usePaddleObstacleStore } from '../../store/paddleObstacleStore';
import { useTourPoiStore } from '../../store/tourPoiStore';
/** One of the planner's own cards: whether it is up, and how it is closed. */
interface StickyCard {
open: boolean;
close: () => void;
}
/**
* The corner over the planner map holds one card at a time. A via ferrata's,
* a paddle object's and a place's card close on any press outside them, so
* the press that opens the next one has closed the last already. The
* planner's own cards (the ways of a section compared, RTE-02, and the round
* hikes out of a stop, RTE-03) stay up through presses on the map, so they
* are kept apart here: whichever card opened last stays, and every other one
* closes. Laid out before paint, so two cards never show stacked in the
* corner, not even for a frame.
*/
export function useOneCornerCard({
planned,
ferrataOpen,
closeFerrata,
}: {
planned: readonly StickyCard[];
ferrataOpen: boolean;
closeFerrata: () => void;
}): void {
const paddleOpen = usePaddleObstacleStore((state) => state.selectedRef !== null);
const poiOpen = useSelectedPoi() !== null;
const selectPoi = useTourPoiStore((state) => state.select);
const otherOpen = ferrataOpen || paddleOpen || poiOpen;
const cards: StickyCard[] = [
...planned,
{
open: otherOpen,
close: () => {
closeFerrata();
selectPaddleObstacle(null);
selectPoi(null);
},
},
];
// The cards as this render has them, read by the layout pass that follows.
const latest = useRef(cards);
useLayoutEffect(() => {
latest.current = cards;
});
const was = useRef(cards.map((card) => card.open));
const openKey = cards.map((card) => card.open).join();
// Runs when what is open changes: the cards that just opened take the corner, those up already make way.
useLayoutEffect(() => {
const now = latest.current;
const before = was.current;
was.current = now.map((card) => card.open);
if (now.filter((card) => card.open).length < 2) return;
if (!now.some((card, index) => card.open && !before[index])) return;
now.forEach((card, index) => {
if (card.open && before[index]) card.close();
});
}, [openKey]);
}