Files
TREK/client/src/components/Tours/planner/tourPlannerDraft.ts
T
Maurice 73237ffedb feat(tours): bike tours in the planner, CyclOSM, and the planned cards' last points (BIK-01 to BIK-06, RTE-01, IO-01, LIB-02)
The tour type is a dropdown; a bike tour picks its profile, routes for it,
says when a point lies off every way, and shows what it runs on: surfaces,
kinds of way and, for a mountain bike, the trail grades, each pointed at on
the map. A type's own map (OpenHikingMap for a hike, CyclOSM for a bike tour)
is offered while a tour of that type is open, with an admin switch each.

From the planned cards: a hike avoids steep climbs, steps or ferries, nothing
else; a saved tour can be shared where the platform takes GPX files; and the
tours rail adds a tour from one menu: plan new, import GPX, or take one over
from another trip.
2026-10-19 21:33:49 +02:00

106 lines
3.8 KiB
TypeScript

import {
bikeProfileSchema,
hikingPreferencesSchema,
MAX_PLANNED_TOUR_DURATION_MINUTES,
paddleLockSchema,
plannableTourTypeSchema,
type BikeProfile,
type HikingPreferences,
type PlannableTourType,
type TourMaxHikingDifficulty,
} from '@trek/shared';
import type { TourPlannerWaypoint } from './tourPlannerWaypoints';
import type { PaddleFieldValues } from './usePaddleFields';
/**
* The tour planner's draft as it is kept in local storage between visits, and
* the check a stored draft has to pass before the planner takes it back: a
* draft from an older version, or one edited by hand, is dropped rather than
* half read.
*/
export interface DraftPayload {
version: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
name: string;
description?: string;
website?: string;
plannedDurationMinutes?: number | null;
breakAdditionalMinutes?: number | null;
waypoints: TourPlannerWaypoint[];
maxHikingDifficulty?: TourMaxHikingDifficulty;
editingPlaceId?: number | null;
/** The import a new tour is being planned from; carried until it is saved. */
derivedFromPlaceId?: number | null;
tourType?: PlannableTourType;
paddle?: PaddleFieldValues;
/** P2C-03: the revision of the tour the draft edits, as it was loaded. */
baseRevision?: number | null;
/** MF-08: what the hike asks the router for besides its limit. */
hikingPreferences?: HikingPreferences | null;
/** BIK-01: the profile a bike tour is routed for. */
bikeProfile?: BikeProfile;
}
const DRAFT_VERSIONS: ReadonlyArray<DraftPayload['version']> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function optionalString(value: unknown): boolean {
return value === undefined || typeof value === 'string';
}
function optionalMinutes(value: number | null | undefined): boolean {
return (
value === undefined ||
value === null ||
(Number.isInteger(value) && value >= 0 && value <= MAX_PLANNED_TOUR_DURATION_MINUTES)
);
}
function optionalPlaceId(value: number | null | undefined): boolean {
return value == null || (Number.isInteger(value) && value > 0);
}
function optionalDifficulty(value: number | null | undefined): boolean {
return value == null || (Number.isInteger(value) && value >= 1 && value <= 6);
}
function validDraftWaypoint(point: TourPlannerWaypoint): boolean {
return (
!!point &&
typeof point.id === 'string' &&
Number.isFinite(point.lat) &&
Math.abs(point.lat) <= 90 &&
Number.isFinite(point.lng) &&
Math.abs(point.lng) <= 180 &&
(point.lock == null || paddleLockSchema.safeParse(point.lock).success)
);
}
/** The type of a draft and what its route is planned by, each one absent in a draft older than it. */
function validRouteChoices(draft: Partial<DraftPayload>): boolean {
return (
(draft.tourType === undefined || plannableTourTypeSchema.safeParse(draft.tourType).success) &&
(draft.bikeProfile === undefined || bikeProfileSchema.safeParse(draft.bikeProfile).success) &&
(draft.hikingPreferences == null || hikingPreferencesSchema.safeParse(draft.hikingPreferences).success) &&
optionalDifficulty(draft.maxHikingDifficulty)
);
}
export function validDraft(value: unknown): value is DraftPayload {
if (!value || typeof value !== 'object') return false;
const draft = value as Partial<DraftPayload>;
return (
draft.version !== undefined &&
DRAFT_VERSIONS.includes(draft.version) &&
typeof draft.name === 'string' &&
optionalString(draft.description) &&
optionalString(draft.website) &&
optionalMinutes(draft.plannedDurationMinutes) &&
optionalMinutes(draft.breakAdditionalMinutes) &&
optionalPlaceId(draft.editingPlaceId) &&
optionalPlaceId(draft.derivedFromPlaceId) &&
optionalPlaceId(draft.baseRevision) &&
validRouteChoices(draft) &&
Array.isArray(draft.waypoints) &&
draft.waypoints.every(validDraftWaypoint)
);
}