feat: task 2.2 tile-source switcher
- pnpm add maplibre-google-maps (runtime; lazy-imported on Google use). - src/map/core/styles.ts: BasemapDescriptor model with four entries (Esri Satellite / OpenTopoMap / OSM / Google Satellite). Google entry gated on cfg.googleMapsKey via available(cfg). styleCustom() builds a single-raster-source MapLibre style with a glyphs URL for future label rendering. ensureGoogleProtocol() lazy-loads maplibre-google-maps + addProtocol on first use; _googleRegistered guards re-registration. - src/map/core/map-pref-store.ts: Zustand + persist middleware (key: trm-map-prefs, version: 1). Holds basemapId + setBasemap. - src/map/core/basemap-switcher.tsx: floating control top-right of <MapView>. Filters by available(cfg), highlights active, applies via getMap().setStyle(). Mount-time bootstrap (no UI state) + user-driven onClick (with switching state) split to satisfy React 19's react-hooks/set-state-in-effect rule. - src/routes/_authed/monitor.tsx: renders <BasemapSwitcher /> as a child of <MapView>. - src/vite-env.d.ts: ambient module declaration for maplibre-google-maps (no .d.ts ships in the package). Deviations: - BasemapDescriptor.style is buildStyle(cfg): StyleSpecification, not the spec's StyleSpecification|string union. Google needs the cfg key at build time; uniform function shape across all entries is cleaner. - Bootstrap apply path doesn't use the switching UI state (no visual feedback needed during the silent first-paint flip from OSM to user preference). User-click path keeps setSwitching for the in-flight indicator. Bundle: MapLibre is now its own chunk (1MB / 273KB gz), shared across maplibre consumers. Main bundle 371KB / 114KB gz.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { createFileRoute } from '@tanstack/react-router';
|
||||
import { BasemapSwitcher } from '@/map/core/basemap-switcher';
|
||||
import { MapView } from '@/map/core/map-view';
|
||||
|
||||
export const Route = createFileRoute('/_authed/monitor')({
|
||||
@@ -11,7 +12,17 @@ function MonitorPage() {
|
||||
// For now the parent _authed layout has no top-bar, so the map can
|
||||
// fill the full viewport — adjust when chrome lands.
|
||||
<div className="relative h-[calc(100vh-0rem)] w-full">
|
||||
<MapView />
|
||||
<MapView>
|
||||
{/*
|
||||
BasemapSwitcher renders inside <MapView> so it remounts after
|
||||
every style swap, ensuring the persisted preference is re-applied
|
||||
if the user does something funny (e.g. force-reloads style via
|
||||
devtools). It's also a sibling of the map canvas, not on top of
|
||||
it visually — the absolute-positioned card is positioned relative
|
||||
to <MapView>'s wrapper.
|
||||
*/}
|
||||
<BasemapSwitcher />
|
||||
</MapView>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user