diff --git a/apps/planner/app/components/PlannerMap.tsx b/apps/planner/app/components/PlannerMap.tsx index cc9789c..b194a0d 100644 --- a/apps/planner/app/components/PlannerMap.tsx +++ b/apps/planner/app/components/PlannerMap.tsx @@ -10,6 +10,7 @@ import { parseGpxAsync, extractWaypoints } from "@trails-cool/gpx"; import { isOvernight } from "~/lib/overnight"; import { setOvernight } from "~/lib/overnight"; import { usePois } from "~/lib/use-pois"; +import { useProfileDefaults } from "~/lib/use-profile-defaults"; import { snapToPoi } from "~/lib/poi-snap"; import { Z_CURSOR, Z_WAYPOINT, Z_WAYPOINT_HIGHLIGHTED, Z_HIGHLIGHT } from "~/lib/z-index"; import { NoGoAreaLayer } from "./NoGoAreaLayer"; @@ -278,6 +279,7 @@ export function PlannerMap({ yjs, onRouteRequest, highlightPosition, highlighted const { t } = useTranslation("planner"); const [waypoints, setWaypoints] = useState([]); const poiState = usePois(); + useProfileDefaults(yjs, poiState); const [draggingOver, setDraggingOver] = useState(false); const dragCounterRef = useRef(0); const [routeCoordinates, setRouteCoordinates] = useState<[number, number, number][] | null>(null); diff --git a/apps/planner/app/lib/use-pois.ts b/apps/planner/app/lib/use-pois.ts index 5aacb56..926b498 100644 --- a/apps/planner/app/lib/use-pois.ts +++ b/apps/planner/app/lib/use-pois.ts @@ -15,7 +15,7 @@ export interface PoiState { pois: Poi[]; status: PoiStatus; enabledCategories: string[]; - setEnabledCategories: (ids: string[]) => void; + setEnabledCategories: React.Dispatch>; toggleCategory: (id: string) => void; refresh: (bbox: BBox, zoom: number) => void; } diff --git a/apps/planner/app/lib/use-profile-defaults.ts b/apps/planner/app/lib/use-profile-defaults.ts new file mode 100644 index 0000000..f591bb1 --- /dev/null +++ b/apps/planner/app/lib/use-profile-defaults.ts @@ -0,0 +1,45 @@ +import { useEffect, useRef } from "react"; +import type { YjsState } from "./use-yjs.ts"; +import type { PoiState } from "./use-pois.ts"; +import { getCategoriesForProfile } from "./poi-categories.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(null); + + useEffect(() => { + if (!yjs) return; + + const handleChange = () => { + const profile = yjs.routeData.get("profile") as string | undefined; + 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]); +} diff --git a/openspec/changes/osm-overlays/tasks.md b/openspec/changes/osm-overlays/tasks.md index 8bb689c..81df7ea 100644 --- a/openspec/changes/osm-overlays/tasks.md +++ b/openspec/changes/osm-overlays/tasks.md @@ -47,9 +47,9 @@ ## 8. Profile-Aware Defaults -- [ ] 8.1 Define profile-to-overlay mapping (cycling → waymarked-cycling + bike POIs, hiking → waymarked-hiking + shelter + viewpoints, MTB → waymarked-mtb + bike POIs) -- [ ] 8.2 Auto-enable mapped overlays on routing profile change (update Yjs arrays) -- [ ] 8.3 Only auto-enable on explicit profile change, not on initial page load (respect existing Yjs state) +- [x] 8.1 Define profile-to-overlay mapping (cycling → waymarked-cycling + bike POIs, hiking → waymarked-hiking + shelter + viewpoints, MTB → waymarked-mtb + bike POIs) +- [x] 8.2 Auto-enable mapped overlays on routing profile change (update Yjs arrays) +- [x] 8.3 Only auto-enable on explicit profile change, not on initial page load (respect existing Yjs state) ## 9. i18n