trails/apps/planner/app/lib/use-profile-defaults.ts
Ullrich Schäfer 60b94b5789
Extract @trails-cool/map-core package from Planner
New renderer-agnostic package with zero dependencies:
- Tile configs (base layers, overlay layers)
- Color palettes (surface, highway, smoothness, tracktype, cycleway,
  bikeroute, elevation, maxspeed) — 8 color maps + 3 color functions
- POI category definitions (9 categories with Overpass queries, icons,
  colors, profile mappings)
- Z-index layering constants
- Snap distance constant

Updated 15 consuming files to import from @trails-cool/map-core.
Deleted poi-categories.ts and z-index.ts from the Planner (fully moved).
packages/map re-exports tile configs from map-core for backwards compat.

All 117 tests pass, no user-facing changes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-12 22:28:14 +02:00

45 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";
/**
* 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 = 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]);
}