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:
parent
02f8a8be44
commit
9e2ca5595e
8 changed files with 63 additions and 52 deletions
|
|
@ -3,6 +3,7 @@ import * as Y from "yjs";
|
|||
import { useTranslation } from "react-i18next";
|
||||
import { parseGpxAsync, extractWaypoints } from "@trails-cool/gpx";
|
||||
import type { YjsState } from "~/lib/use-yjs";
|
||||
import { waypointToYMap } from "~/lib/waypoint-ymap";
|
||||
|
||||
export function useGpxDrop(yjs: YjsState, onImportError?: (message: string) => void) {
|
||||
const { t } = useTranslation("planner");
|
||||
|
|
@ -48,13 +49,7 @@ export function useGpxDrop(yjs: YjsState, onImportError?: (message: string) => v
|
|||
yjs.doc.transact(() => {
|
||||
yjs.waypoints.delete(0, yjs.waypoints.length);
|
||||
for (const wp of newWaypoints) {
|
||||
const yMap = new Y.Map();
|
||||
yMap.set("lat", wp.lat);
|
||||
yMap.set("lon", wp.lon);
|
||||
if (wp.name) yMap.set("name", wp.name);
|
||||
if (wp.isDayBreak) yMap.set("overnight", true);
|
||||
if (wp.note) yMap.set("note", wp.note);
|
||||
yjs.waypoints.push([yMap]);
|
||||
yjs.waypoints.push([waypointToYMap(wp)]);
|
||||
}
|
||||
|
||||
yjs.noGoAreas.delete(0, yjs.noGoAreas.length);
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { usePois } from "~/lib/use-pois";
|
|||
import { snapToPoi } from "~/lib/poi-snap";
|
||||
import { isOvernight } from "~/lib/overnight";
|
||||
import { findSegmentForPoint } from "~/components/ColoredRoute";
|
||||
import { waypointFromYMap } from "~/lib/waypoint-ymap";
|
||||
|
||||
export interface WaypointData {
|
||||
lat: number;
|
||||
|
|
@ -16,13 +17,10 @@ export 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) };
|
||||
});
|
||||
}
|
||||
|
||||
function pointToSegmentDist(
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import { useEffect, useRef, useState } from "react";
|
||||
import * as Y from "yjs";
|
||||
import { WebsocketProvider } from "y-websocket";
|
||||
import type { Waypoint } from "@trails-cool/types";
|
||||
import { waypointToYMap } from "~/lib/waypoint-ymap";
|
||||
|
||||
const COLORS = [
|
||||
"#ef4444", "#f97316", "#eab308", "#22c55e",
|
||||
|
|
@ -42,7 +44,7 @@ export interface YjsState {
|
|||
|
||||
export function useYjs(
|
||||
sessionId: string,
|
||||
initialWaypoints?: Array<{ lat: number; lon: number; name?: string; isDayBreak?: boolean; note?: string; osmId?: number; poiTags?: Record<string, string> }>,
|
||||
initialWaypoints?: Waypoint[],
|
||||
initialNoGoAreas?: Array<{ points: Array<{ lat: number; lon: number }> }>,
|
||||
initialNotes?: string,
|
||||
): YjsState | null {
|
||||
|
|
@ -103,15 +105,7 @@ export function useYjs(
|
|||
initializedWaypoints.current = true;
|
||||
doc.transact(() => {
|
||||
for (const wp of initialWaypoints) {
|
||||
const yMap = new Y.Map();
|
||||
yMap.set("lat", wp.lat);
|
||||
yMap.set("lon", wp.lon);
|
||||
if (wp.name) yMap.set("name", wp.name);
|
||||
if (wp.isDayBreak) yMap.set("overnight", true);
|
||||
if (wp.note) yMap.set("note", wp.note);
|
||||
if (wp.osmId !== undefined) yMap.set("osmId", wp.osmId);
|
||||
if (wp.poiTags) yMap.set("poiTags", wp.poiTags);
|
||||
waypoints.push([yMap]);
|
||||
waypoints.push([waypointToYMap(wp)]);
|
||||
}
|
||||
if (initialNoGoAreas?.length && noGoAreas.length === 0) {
|
||||
for (const area of initialNoGoAreas) {
|
||||
|
|
|
|||
41
apps/planner/app/lib/waypoint-ymap.ts
Normal file
41
apps/planner/app/lib/waypoint-ymap.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import * as Y from "yjs";
|
||||
import type { Waypoint, WaypointPoiTags } from "@trails-cool/types";
|
||||
|
||||
/**
|
||||
* Reads all Waypoint fields from a Yjs map.
|
||||
* The "overnight" key maps to isDayBreak (legacy wire name).
|
||||
* Add new Waypoint fields here — one place for all consumers.
|
||||
*/
|
||||
export function waypointFromYMap(yMap: Y.Map<unknown>): Waypoint {
|
||||
return {
|
||||
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,
|
||||
isDayBreak: yMap.get("overnight") === true ? true : undefined,
|
||||
osmId: yMap.get("osmId") as number | undefined,
|
||||
poiTags: yMap.get("poiTags") as WaypointPoiTags | undefined,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes all Waypoint fields onto an existing Yjs map.
|
||||
* The isDayBreak field is stored as "overnight" (legacy wire name).
|
||||
* Add new Waypoint fields here — one place for all producers.
|
||||
*/
|
||||
export function applyWaypointToYMap(yMap: Y.Map<unknown>, wp: Waypoint): void {
|
||||
yMap.set("lat", wp.lat);
|
||||
yMap.set("lon", wp.lon);
|
||||
if (wp.name) yMap.set("name", wp.name);
|
||||
if (wp.note) yMap.set("note", wp.note);
|
||||
if (wp.isDayBreak) yMap.set("overnight", true);
|
||||
if (wp.osmId !== undefined) yMap.set("osmId", wp.osmId);
|
||||
if (wp.poiTags) yMap.set("poiTags", wp.poiTags);
|
||||
}
|
||||
|
||||
/** Convenience: creates a new Y.Map and populates it from a Waypoint. */
|
||||
export function waypointToYMap(wp: Waypoint): Y.Map<unknown> {
|
||||
const yMap = new Y.Map<unknown>();
|
||||
applyWaypointToYMap(yMap, wp);
|
||||
return yMap;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue