The routeData Y.Map's ~15 string keys (geojson, coordinates, segmentBoundaries, road metadata, profile, colorMode, baseLayer, overlays, poiCategories) were read and written raw at ~30 call sites, each with its own JSON parsing and casts; parseJsonArray existed twice and waypoint extraction four times. GPX assembly was duplicated between SaveToJournalButton and ExportButton, so the saved plan and the exported file could silently diverge. - new lib/route-data.ts owns the routeData (+ noGoAreas) schema: typed read/write, JSON encoding internal, ColorMode moves here (re-exported from ColoredRoute for existing importers) - new lib/gpx-export.ts owns GPX assembly: buildRouteGpx / buildPlanGpx / buildDayGpxFiles / hasDayBreaks; multi-day splitting becomes a pure, tested function - waypoint-ymap.ts gains extractWaypoints / extractWaypointData; the four hand-rolled copies (use-routing, use-waypoint-manager, WaypointSidebar, use-days) now share it, and WaypointSidebar's moveWaypoint reuses the round-trip helpers instead of re-listing every waypoint field - all hooks/components consume the seam; no raw routeData key strings remain outside route-data.ts Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import { useEffect, useRef } from "react";
|
|
import type { YjsState } from "./use-yjs.ts";
|
|
import type { PoiState } from "./use-pois.ts";
|
|
import { getCategoriesForProfile } from "@trails-cool/map-core";
|
|
import { getProfile } from "./route-data.ts";
|
|
|
|
/**
|
|
* Auto-enable relevant POI categories when the routing profile changes.
|
|
* Only triggers on explicit profile changes (not initial load).
|
|
*/
|
|
export function useProfileDefaults(yjs: YjsState | null, poiState: PoiState): void {
|
|
const initializedRef = useRef(false);
|
|
const prevProfileRef = useRef<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!yjs) return;
|
|
|
|
const handleChange = () => {
|
|
const profile = getProfile(yjs.routeData);
|
|
if (!profile) return;
|
|
|
|
// Skip initial load — respect existing state
|
|
if (!initializedRef.current) {
|
|
initializedRef.current = true;
|
|
prevProfileRef.current = profile;
|
|
return;
|
|
}
|
|
|
|
// Only act on actual profile changes
|
|
if (profile === prevProfileRef.current) return;
|
|
prevProfileRef.current = profile;
|
|
|
|
// Auto-enable POI categories for this profile
|
|
const defaultCategories = getCategoriesForProfile(profile);
|
|
if (defaultCategories.length > 0) {
|
|
poiState.setEnabledCategories((prev: string[]) => {
|
|
const merged = new Set([...prev, ...defaultCategories]);
|
|
return [...merged];
|
|
});
|
|
}
|
|
};
|
|
|
|
yjs.routeData.observe(handleChange);
|
|
return () => yjs.routeData.unobserve(handleChange);
|
|
}, [yjs, poiState.setEnabledCategories]);
|
|
}
|