mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
2203ff3a2e
A city walk now shows, at each stop, when the walk gets there, until when the stay lasts, and what the stop's opening hours say about that visit. The same lines appear under each stop row in the planner and under each stop in the walk's detail, in the user's 12 or 24 hour format. A conflict names the stop, the arrival, the end of the stay and the hours it was checked against, for instance "Lenbachhaus closes at 18:00, during the stay from 17:20 to 18:20 (open 10:00 to 18:00)", in the warning colour. Nothing is changed by these hints: no stop, order or route moves. The times belong to one day the walk is on. Opened from a day of the plan, the detail keeps that day, and checking stops off stays bound to it. Opened otherwise, and in the planner, a small pill above the stops names the day and its start, and the walk's other days can be picked from it; it starts on the first day with a start time. The pick is only what is looked at and is never saved, and another day never takes over the times quietly. Without a day, a date or a start time the line says what is missing and no arrival is shown. TREK keeps no time zone on a trip, so start, arrivals and opening hours are all read as the local time of the plan, and an arrival after midnight is said to be on the next day without being checked. Opening hours come from the trip place a stop stands for, as the travellers entered them, and otherwise from OpenStreetMap; a pill at each stop names the source, and a notice says the hours are a hint to check. A stop without hours stays unknown and is never shown as open, hours that are not in a plain form are shown as written, and an arrival that is only a lower bound, because a stay before it is not entered yet, is not judged unless no later arrival could escape the conflict. A trip place that a stop stands for also gives the stop its name before "Stop N". The arrival at a stop is found where the route passes it in the walk's order, so the end of a loop and a stop passed on the way out and back get the right time. The rules live once in the shared package, for the server to give the same answers. The walk's detail reads its stops again after a save, its own or a teammate's.
95 lines
4.2 KiB
TypeScript
95 lines
4.2 KiB
TypeScript
import { nearnessToLine } from '@trek/shared';
|
|
import { useCallback, useMemo } from 'react';
|
|
import { useTripStore } from '../../../store/tripStore';
|
|
import type { Category, Place } from '../../../types';
|
|
import { randomId } from '../../../utils/randomId';
|
|
import { useLinkedPlaceOf } from '../walk/useWalkSchedule';
|
|
import { unlinkedPoint, type TourPlannerWaypoint } from './tourPlannerWaypoints';
|
|
import { withPointAppended } from './tourRouteActions';
|
|
import type { TourPlannerController } from './useTourPlanner';
|
|
|
|
/** A place of the trip as the picker offers it. */
|
|
export interface TripPlaceCandidate {
|
|
place: Place;
|
|
category: Category | null;
|
|
/** Metres from the tour as it stands (its route, else its points); null while the tour has no point. */
|
|
distanceM: number | null;
|
|
/** A point of the tour stands for it already. */
|
|
used: boolean;
|
|
}
|
|
|
|
const NO_PLACES: Place[] = [];
|
|
const NO_CATEGORIES: Category[] = [];
|
|
|
|
/** Within this distance of the tour a place counts as near it, and the picker says so. */
|
|
export const NEAR_TOUR_M = 2_000;
|
|
|
|
/** The line a place is measured against: the route while there is one, else the points as set. */
|
|
function tourLine(planner: Pick<TourPlannerController, 'route' | 'waypoints'>): Array<[number, number]> {
|
|
if (planner.route && planner.route.length >= 2) return planner.route;
|
|
return planner.waypoints.map((point): [number, number] => [point.lat, point.lng]);
|
|
}
|
|
|
|
function distanceFrom(place: Place, line: Array<[number, number]>): number | null {
|
|
if (line.length === 0 || place.lat == null || place.lng == null) return null;
|
|
if (line.length === 1)
|
|
return nearnessToLine(place as { lat: number; lng: number }, [line[0], line[0]])?.distanceM ?? null;
|
|
return nearnessToLine(place as { lat: number; lng: number }, line)?.distanceM ?? null;
|
|
}
|
|
|
|
/**
|
|
* PR1-10: the trip's places as stops of a tour, chosen on purpose. A place is
|
|
* offered once it has a spot and is no tour itself, nearest to the tour first;
|
|
* picking one adds a point on its spot that stands for it, as one step of the
|
|
* history. The point keeps its own coordinates from then on, the place stays a
|
|
* place of its own, and its days are left alone. Removing or unlinking the
|
|
* point never touches the place.
|
|
*/
|
|
export function useTripPlaceWaypoints(planner: TourPlannerController, tourPlaceIds: ReadonlySet<number>) {
|
|
const places = useTripStore((state) => state.places) ?? NO_PLACES;
|
|
const categories = useTripStore((state) => state.categories) ?? NO_CATEGORIES;
|
|
const { editWaypoints, setSelectedWaypointId, waypoints, route } = planner;
|
|
|
|
const linkedPlaceOf = useLinkedPlaceOf();
|
|
|
|
const candidates = useMemo((): TripPlaceCandidate[] => {
|
|
const line = tourLine({ route, waypoints });
|
|
const used = new Set(waypoints.flatMap((point) => (point.linkedPlaceId ? [point.linkedPlaceId] : [])));
|
|
const categoryOf = new Map(categories.map((category) => [category.id, category]));
|
|
return places
|
|
.filter((place) => place.lat != null && place.lng != null && !tourPlaceIds.has(place.id))
|
|
.map((place) => ({
|
|
place,
|
|
category: place.category_id != null ? (categoryOf.get(place.category_id) ?? null) : null,
|
|
distanceM: distanceFrom(place, line),
|
|
used: used.has(place.id),
|
|
}))
|
|
.sort(
|
|
(a, b) =>
|
|
(a.distanceM ?? Number.POSITIVE_INFINITY) - (b.distanceM ?? Number.POSITIVE_INFINITY) ||
|
|
a.place.name.localeCompare(b.place.name)
|
|
);
|
|
}, [categories, places, route, tourPlaceIds, waypoints]);
|
|
|
|
const add = useCallback(
|
|
(place: Place) => {
|
|
if (place.lat == null || place.lng == null) return;
|
|
const id = randomId();
|
|
const point: TourPlannerWaypoint = { id, lat: place.lat, lng: place.lng, role: 'end', linkedPlaceId: place.id };
|
|
editWaypoints((current) => withPointAppended(current, point));
|
|
setSelectedWaypointId(id);
|
|
},
|
|
[editWaypoints, setSelectedWaypointId]
|
|
);
|
|
|
|
const unlink = useCallback(
|
|
(pointId: string) =>
|
|
editWaypoints((current) => current.map((point) => (point.id === pointId ? unlinkedPoint(point) : point))),
|
|
[editWaypoints]
|
|
);
|
|
|
|
return { candidates, add, unlink, linkedPlaceOf };
|
|
}
|
|
|
|
export type TripPlaceWaypoints = ReturnType<typeof useTripPlaceWaypoints>;
|