Centralize waypoint Yjs serialization in waypointFromYMap/waypointToYMap

Introduce waypoint-ymap.ts with typed helpers so all Yjs↔Waypoint
conversions go through one place. New Waypoint fields now only need
to be added once rather than in every consumer.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ullrich Schäfer 2026-05-18 21:02:27 +02:00
parent 02f8a8be44
commit 9e2ca5595e
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
8 changed files with 63 additions and 52 deletions

View file

@ -4,7 +4,7 @@ import * as Y from "yjs";
import type { YjsState } from "~/lib/use-yjs";
import { generateGpx, computeDays } from "@trails-cool/gpx";
import type { TrackPoint, NoGoArea } from "@trails-cool/gpx";
import type { WaypointPoiTags } from "@trails-cool/types";
import { waypointFromYMap } from "~/lib/waypoint-ymap";
function getTracks(yjs: YjsState): TrackPoint[][] {
const geojsonStr = yjs.routeData.get("geojson") as string | undefined;
@ -20,15 +20,7 @@ function getTracks(yjs: YjsState): TrackPoint[][] {
}
function getWaypoints(yjs: YjsState) {
return yjs.waypoints.toArray().map((yMap: Y.Map<unknown>) => ({
lat: yMap.get("lat") as number,
lon: yMap.get("lon") as number,
name: yMap.get("name") as string | undefined,
isDayBreak: yMap.get("overnight") === true ? true : undefined,
note: yMap.get("note") as string | undefined,
osmId: yMap.get("osmId") as number | undefined,
poiTags: yMap.get("poiTags") as WaypointPoiTags | undefined,
}));
return yjs.waypoints.toArray().map(waypointFromYMap);
}
function getNoGoAreas(yjs: YjsState): NoGoArea[] {

View file

@ -4,7 +4,7 @@ import * as Y from "yjs";
import type { YjsState } from "~/lib/use-yjs";
import { generateGpx } from "@trails-cool/gpx";
import type { TrackPoint, NoGoArea } from "@trails-cool/gpx";
import type { WaypointPoiTags } from "@trails-cool/types";
import { waypointFromYMap } from "~/lib/waypoint-ymap";
interface SaveToJournalButtonProps {
yjs: YjsState;
@ -42,15 +42,7 @@ export function SaveToJournalButton({ yjs, callbackUrl, callbackToken, returnUrl
points: (yMap.get("points") as Array<{ lat: number; lon: number }>) ?? [],
})).filter((a) => a.points.length >= 3);
const waypoints = yjs.waypoints.toArray().map((yMap: Y.Map<unknown>) => ({
lat: yMap.get("lat") as number,
lon: yMap.get("lon") as number,
name: yMap.get("name") as string | undefined,
isDayBreak: yMap.get("overnight") === true ? true : undefined,
note: yMap.get("note") as string | undefined,
osmId: yMap.get("osmId") as number | undefined,
poiTags: yMap.get("poiTags") as WaypointPoiTags | undefined,
}));
const waypoints = yjs.waypoints.toArray().map(waypointFromYMap);
const notes = yjs.notes.toString() || undefined;
const gpx = generateGpx({ name: "trails.cool route", description: notes, waypoints, tracks, noGoAreas });

View file

@ -8,6 +8,7 @@ import { DayBreakdown } from "./DayBreakdown";
import { useNearbyPois } from "~/lib/use-nearby-pois";
import { poiCategories } from "@trails-cool/map-core";
import type { Poi } from "~/lib/overpass";
import { waypointFromYMap } from "~/lib/waypoint-ymap";
const NOTE_MAX = 500;
@ -20,13 +21,10 @@ interface WaypointData {
}
function getWaypointsFromYjs(waypoints: Y.Array<Y.Map<unknown>>): WaypointData[] {
return waypoints.toArray().map((yMap) => ({
lat: yMap.get("lat") as number,
lon: yMap.get("lon") as number,
name: yMap.get("name") as string | undefined,
note: yMap.get("note") as string | undefined,
overnight: isOvernight(yMap),
}));
return waypoints.toArray().map((yMap) => {
const wp = waypointFromYMap(yMap);
return { lat: wp.lat, lon: wp.lon, name: wp.name, note: wp.note, overnight: isOvernight(yMap) };
});
}
interface WaypointSidebarProps {